Kris
Kris

Reputation: 185

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"

Upvotes: 5

Views: 17416

Answers (2)

HumbleBee
HumbleBee

Reputation: 341

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.

Upvotes: 2

Thomas Hsieh
Thomas Hsieh

Reputation: 731

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"

Upvotes: 7

Related Questions