0

I am following the physical computing with python tutorial from here: https://projects.raspberrypi.org/en/projects/physical-computing

I have created a python file that contains the following:

from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(2)

button.when_pressed = led.on
button.when_released = led.off

pause()

Also I have the button and led connected to the correct pins and in parallel. When I test the light and button separately directly in the command line, they work fine.

Also I get this message, but I don't know if that has anything to do with this issue:

/home/pi/local/lib/python2.7/site-packages/gpiozero/devices.py:451: PinFactoryFallback: Falling back from rpigpio: No module named RPi
  'Falling back from %s: %s' % (name, str(e))))
/home/pi/local/lib/python2.7/site-packages/gpiozero/devices.py:451: PinFactoryFallback: Falling back from rpio: No module named RPIO
  'Falling back from %s: %s' % (name, str(e))))
/home/pi/local/lib/python2.7/site-packages/gpiozero/devices.py:451: PinFactoryFallback: Falling back from pigpio: No module named pigpio
  'Falling back from %s: %s' % (name, str(e))))

Can someone please help?

Edit1: Ok, I have uninstalled gpiozero, and am left with the default version included for python3. What I am typing now in terminal is python3 gpio_control.py which includes the name of the file I posted. There isn't any output when I run it and it does not terminate. –

Edit2: I got this to work (light turns on when pressed and off when released) with the following code:

#! /usr/bin/env python3
from gpiozero import LED, Button

led = LED(17)
button = Button(2)

while True:
        button.wait_for_press()
        led.on()
        button.when_released = led.off()
1

Your question is lacking detail - specifically WHAT you are ACTUALLY typing. The error suggests you are using Python2.7 and have manually installed gpiozero (which should be included in Raspbian), but AFAIK the examples are intended for Python3.

The "examples" strike me as POOR Programming practice

Even if you are running examples from IDLE3 or Thonny there is NO REASON NOT to include a shebang at the start of the program.

#! /usr/bin/env python3

Likewise there is NO REASON for new programs to be written in Python2.

Whatever you do you NEED to install and run for a specific version of Python.

  • Ok, I have uninstalled gpiozero, and am left with the default version included for python3. What I am typing now in terminal is python3 gpio_control.py which includes the name of the file I posted. There isn't any output when I run it and it does not terminate. – Nicholas Schmidt Dec 24 '17 at 19:04
  • @NicholasSchmidt You should post additional detail in your post, not Comments. The program ACTUALLY does nothing! I probably ran, You probably want to use button.wait_for_press(), and unless you intend it to run once, include in a loop. (These were not the questions you asked.) – Milliways Dec 24 '17 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.