henkoegema
Posts: 13
Joined: Thu Oct 17, 2013 3:26 pm

NameError: name 'GPIO' is not defined

Wed Apr 19, 2017 6:28 pm

  • # http://www.rototron.info/using-a-motion ... pberry-pi/
    # Set up input pin
    GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

    # Set up LED output
    GPIO.setup(20, GPIO.OUT)

    # Callback function to run when motion detected
    def motionSensor(channel):
    lcd.clear()
    GPIO.output(20, GPIO.LOW)
    if GPIO.input(21): # True = Rising
    global counter
    counter += 1
    lcd.message('Motion Detected\n{0}'.format(counter))
    GPIO.output(20, GPIO.HIGH)

    # add event listener on pin 21
    GPIO.add_event_detect(21, GPIO.BOTH, callback=motionSensor, bouncetime=300).
    counter = 0

    try:
    while True:
    sleep(1) # wait 1 second
    finally: # run on exit
    GPIO.cleanup() # clean up
    print "All cleaned up."
When I run this program I get following error message: :roll:

Code: Select all

[email protected]:~/i2c_lcd20x4/motion $ sudo python motion.py
Traceback (most recent call last):
  File "motion.py", line 3, in <module>
    GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
NameError: name 'GPIO' is not defined

ghp
Posts: 1456
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: NameError: name 'GPIO' is not defined

Wed Apr 19, 2017 6:37 pm

Let your python program 'know' that you want to use the rpi.gpio package and add
import RPi.GPIO as GPIO
at the beginning of the program.
Hope this helps,
Gerhard

PatrickPi
Posts: 1
Joined: Tue Dec 04, 2018 9:11 am

Re: NameError: name 'GPIO' is not defined

Tue Dec 04, 2018 9:30 am

Hello members,
I get RBP 3 B+., runing a very simple Py3 script to activate GPIO nevertheless received msg ... GPIO not defined..
Program had code import RPi.GPIO as GPIO as advice.
Pi software had upgraded as latest. and reboot Pi.
when run the moudle, still show the same message" .... GPIO not defined...
kindly advice what the problem can be? hope can get some advice, thank.
rg. Patrick

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

Re: NameError: name 'GPIO' is not defined

Tue Dec 04, 2018 10:23 am

PatrickPi wrote:
Tue Dec 04, 2018 9:30 am
Hello members,
I get RBP 3 B+., runing a very simple Py3 script to activate GPIO nevertheless received msg ... GPIO not defined..
Program had code import RPi.GPIO as GPIO as advice.
Pi software had upgraded as latest. and reboot Pi.
when run the moudle, still show the same message" .... GPIO not defined...
kindly advice what the problem can be? hope can get some advice, thank.
rg. Patrick
Hi.

Try posting your code so we can have a look at it, without seeing your code we don't have much of a chance to diagnose the problem,
might also be a good idea to post the actual error message as well.

please use code tags when posting your code

Add [code] at the top

code goes here

and [/code] at the bottom.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”