Subscribe to Blog via Email
Follow me on TwitterMy Tweets
So there are some cool features that are built into the GPIOZero library/module. One of the challenges I think many of us that have a lot of projects that we want to work on, is that we end up having to translate it to the version of RPI that we just happen to be working on or have available. This can be frustrating when the code is available without us having to rewrite it all in the libraries available for the hardware we just happen to plug-in.
So GPIO the old way with photocells is a bit involved:
GPIO.setmode(GPIO.BCM) def RCtime (RCpin): reading = 0 GPIO.setup(RCpin, GPIO.OUT) GPIO.output(RCpin, GPIO.LOW) time.sleep(0.1) GPIO.setup(RCpin, GPIO.IN) while (GPIO.input(RCpin) == GPIO.LOW): reading += 1 return reading while True: print RCtime(18) # Read RC timing using pin #18 <and on and on..and we haven't even got to the buzzer, etc....>
If we use the GPIOZero library, it’s much simpler due to a module written for physical computing with photocells:
from gpiozero import LightSensor, Buzzer from time import sleep ldr = LightSensor(18) buzzer = Buzzer(4) while True: sleep(0.1) if ldr.value < 0.5: buzzer.on() sleep(30) else: buzzer.off()
LightSensor was what I wanted and it was already available in the GPIOZero library.
Needless to say, this happened to me as I was working on a new project. I was at the Starbucks and pulled out my Raspberry Pi, B+ Release 2, that has the touchscreen built in, plugged it in and went to town. With this configuration, I only require one plug, (the monitor works off a battery) and it’s very portable when I’m out and about.
So what did I do when I saw the module to call was GPIOZero and I was on the Raspberry Pi 2? I installed the GPIOZero library, of course!
sudo apt-get install python3-gpiozero
I keep the images updated with the latest, so it was a no-brainer to add this to it.
If you don’t have what you want for the model you are working on, why not just try to install what you need?
Easy-peasy, forty pin GPIO library done. So there, Raspberry Pi Versions!