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

你。 提交于 2019-12-01 04:36:21

问题


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"

回答1:


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"



回答2:


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.



来源:https://stackoverflow.com/questions/29478216/traceback-most-recent-call-last-and-gpio-setmodegpio-board-or-gpio-setmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!