DarkfullDante
Posts: 4
Joined: Wed Jan 15, 2020 3:19 pm

Trouble setting GPIO.OUT

Wed Jan 15, 2020 3:26 pm

First of all this is my first program from scratch so I'm sure I'm making very stupid mistake. I'm trying to use a Led bar graph to show to percentage of ads block on a pi-hole. I have the following python code at the moment:

Code: Select all

# Import Module
import requests
import time
import RPi.GPIO as GPIO
import math

# define variables
waitTime = 5
ledPins = [11, 12, 13, 15, 16, 18, 22, 3, 5, 24]

# define setup
def setup():
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(ledPins, GPIO.OUT)
        GPIO.setup(ledPins, GPIO.HIGH)

# define Infinite Loop
def loop():

        percentOld=0

        while True:
                # Get percent Blocked
                pihole = requests.get("http://127.0.0.1/admin/api.php?summaryRaw").json()
                percentNew = int(math.floor(pihole['ads_percentage_today']/10))
                print(percentNew)

                # Check if percentNew is different from percentOld
                if percentNew != percentOld :

                        # refresh percentOld
                        percentOld = percentNew
                        i = 0
                        # loop through pins
                        for pin in ledPins :

                                # do something only if Index is not 0
                                i += 1

                                # Open Led only if pin index is lower or equal to percentNew
                                if i <= percentNew :
                                        GPIO.output(pin, GPIO.LOW)
                                        print("pin " + str(i) + " on, pin no : "+ str(pin))
                                else :
                                        GPIO.output(pin, GPIO.HIGH)
                                        print("pin " + str(i) + " off, pin no : " + str(pin))
                # Wait before refresh
                time.sleep(waitTime)

# define GPIO CleanUp
def destroy():
        GPIO.cleanup()
        
if __name__ == '__main__':

        #Call Setup
        setup()
        try:
                #Call Loop
                loop()

        except KeyboardInterrupt:
                destroy()
I do run the python code in sudo. Please help!

User avatar
B.Goode
Posts: 9063
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Trouble setting GPIO.OUT

Wed Jan 15, 2020 4:59 pm

Welcome to the Raspberry Pi forums.

I do run the python code in sudo. Please help!


Help #1: don't run code via sudo unless you really need to. (And in this case you almost certainly don't need to.)

Help #2: have a look at the documentation for the gpiozero Python library, which already has the ability to create an LED Bar Graph: https://gpiozero.readthedocs.io/en/stab ... edbargraph



Otherwise, please state clearly what help you are seeking...

DarkfullDante
Posts: 4
Joined: Wed Jan 15, 2020 3:19 pm

Re: Trouble setting GPIO.OUT

Wed Jan 15, 2020 5:07 pm

Just realise there isn't a question in my post sorry about that

I'm trying to understand why gpio.setup is not able to set my channels to output mode. The error is "GPIO channel has not been setup as an OUTPUT"

Even if GPIO zero has the functionality I'm trying to learn to code and would prefer to be able to do it from scratch as it helps me learn python

pcmanbob
Posts: 7732
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Trouble setting GPIO.OUT

Wed Jan 15, 2020 5:22 pm

Edit.....
Correction got my variables mixed up you can access the global variable from within the function , its the other way round you can do it.

you are still better off doing all gpio setup at the start of your program as you only need to do it one.

Code: Select all


# define variables
waitTime = 5
ledPins = [11, 12, 13, 15, 16, 18, 22, 3, 5, 24]

# define setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPins, GPIO.OUT)
GPIO.output(ledPins, GPIO.HIGH)


Edited to correct gpio output line........

You could also replace the two setup lines with the one below...

Code: Select all

# define variables
waitTime = 5
ledPins = [11, 12, 13, 15, 16, 18, 22, 3, 5, 24]

# define setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPins, GPIO.OUT, initial=GPIO.HIGH)
https://sourceforge.net/p/raspberry-gpi ... asicUsage/
Last edited by pcmanbob on Wed Jan 15, 2020 9:02 pm, edited 4 times in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
B.Goode
Posts: 9063
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Trouble setting GPIO.OUT

Wed Jan 15, 2020 5:23 pm

DarkfullDante wrote:
Wed Jan 15, 2020 5:07 pm
Just realise there isn't a question in my post sorry about that

I'm trying to understand why gpio.setup is not able to set my channels to output mode. The error is "GPIO channel has not been setup as an OUTPUT"

Even if GPIO zero has the functionality I'm trying to learn to code and would prefer to be able to do it from scratch as it helps me learn python

The error you are seeing may be a consequence of the line:

Code: Select all

GPIO.setup(ledPins, GPIO.HIGH)

That does not seem to be covered by the Developer documentation: https://sourceforge.net/p/raspberry-gpi ... /Examples/

GPIO.HIGH is not listed as a valid argument for GPIO.setup()


gpiozero and RPi.GPIO are alternatives - RPi.GPIO is a library provided by a volunteer member of this forum community: it does not represent programming the gpio facilities of the RPi board 'from scratch.'

DarkfullDante
Posts: 4
Joined: Wed Jan 15, 2020 3:19 pm

Re: Trouble setting GPIO.OUT

Wed Jan 15, 2020 6:37 pm

Thank you for the replies, I'll try the ideas when I get home from work.

DarkfullDante
Posts: 4
Joined: Wed Jan 15, 2020 3:19 pm

Re: Trouble setting GPIO.OUT

Thu Jan 16, 2020 1:35 pm

B.Goode wrote:
Wed Jan 15, 2020 5:23 pm
GPIO.HIGH is not listed as a valid argument for GPIO.setup()
And that was the problem. My error, i should have used GPIO.output(ledPins, GPIO.HIGH). That line was there only to ensure that all leds where off during the initial setup. The code works fine now.

Return to “Python”