Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TraceBack (most recent call last), and GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) errors

I have been struggling with a script that will turn a Pi's BCM pins 17, 27, and 10 on for 1 second then off for 1 second sequentially 100ish times. When I run the script LED1 will turn on and then it turns off and the program shuts down with this error:

Traceback (most recent call last):
  File "LedBlink.py", line 47, in <module>
    LED2Blink()
  File "LedBlink.py", line 27, in LED2Blink
    GPIO.setup(LED2, GPIO.OUT)
RuntimeError: Please set pin numbering mode using     GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

import RPi.GPIO as GPIO
import time

LED1 = 17
LED2 = 27
LED3 = 10

GPIO.setmode(GPIO.BCM)

def LED1Blink():
        GPIO.setup(LED1, GPIO.OUT)
        GPIO.output(LED1,True) 
        time.sleep(1)  
        GPIO.output(LED1,False)
        time.sleep(1)
        GPIO.cleanup()

def LED2Blink():
        GPIO.setup(LED2, GPIO.OUT)
        GPIO.output(LED2,True) 
        time.sleep(1)  
        GPIO.output(LED2,False)
        time.sleep(1)
        GPIO.cleanup()

def LED3Blink():
        GPIO.setup(LED3, GPIO.OUT)
        GPIO.output(LED3,True) 
        time.sleep(1)  
        GPIO.output(LED3,False)
        time.sleep(1)
        GPIO.cleanup()

i = 0
while i < 100:
       LED1Blink()
       LED2Blink()
       LED3Blink()
       i + 1
else:
       print "finished loop"
like image 725
Kris Avatar asked Apr 06 '15 19:04

Kris


People also ask

What does GPIO BCM mean?

GPIO. BCM -- Broadcom chip-specific pin numbers. These pin numbers follow the lower-level numbering system defined by the Raspberry Pi's Broadcom-chip brain.

What is the difference between GPIO board and GPIO BCM?

GPIO BOARD– This type of pin numbering refers to the number of the pin in the plug, i.e, the numbers printed on the board, for example, P1. The advantage of this type of numbering is, it will not change even though the version of board changes. GPIO BCM– The BCM option refers to the pin by “Broadcom SOC Channel.

Why is GPIO Setwarnings false?

setwarnings(False) to disable warnings. Its telling you that the gpio is already in use , that's because you have stopped the program and started it again . just add it as the next line after your gpio. setmode line.

What does GPIO cleanup () do?

GPIO provides a built-in function GPIO. cleanup() to clean up all the ports you've used. But be very clear what this does. It only affects any ports you have set in the current program.


2 Answers

The problem is that you are calling GPIO.cleanup() at the end of each methods. As stated in the documentation, Note that GPIO.cleanup() also clears the pin numbering system in use. What you want is GPIO.cleanup(channel) instead, where channel corresponds to LED1, LED2, LED3 in your script.

The best practice is to setup and cleanup the channels ONLY ONCE, e.g.

import RPi.GPIO as GPIO
import time

LED1 = 17
LED2 = 27
LED3 = 10

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)

def LED1Blink():
        GPIO.output(LED1,True) 
        time.sleep(1)  
        GPIO.output(LED1,False)
        time.sleep(1)

def LED2Blink():
        GPIO.output(LED2,True) 
        time.sleep(1)  
        GPIO.output(LED2,False)
        time.sleep(1)

def LED3Blink():
        GPIO.output(LED3,True) 
        time.sleep(1)  
        GPIO.output(LED3,False)
        time.sleep(1)

i = 0
if i < 100:
       LED1Blink()
       LED2Blink()
       LED3Blink()
       i + 1
else:
       GPIO.cleanup()
       print "finished loop"
like image 170
Thomas Hsieh Avatar answered Nov 10 '22 05:11

Thomas Hsieh


Never call GPIO.cleanup() more than once as alongwith clearing the PINS, it also clears the Pin MODE! So, if you have called it in between a program, then next statement execution wouldn't have a pin MODE and that'll give out an Error. "TraceBack (most recent call last), and GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) errors"

So, Always use it at end or wherever the program could end/break in between if certain condition is met.

like image 35
HumbleBee Avatar answered Nov 10 '22 03:11

HumbleBee