Pi IoT In Python Using GPIO Zero - On/Off Devices |
Written by Harry Fairhead & Mike James | |||||||||||
Monday, 14 December 2020 | |||||||||||
Page 1 of 2 Simple on/off devices are, obviously, the simplest of IoT devices you can work with. In this extract from a new book on using GPIO Zero on the Pi in Python we look at how to get started. Raspberry Pi IoT In Python Using GPIO Zero
|
pin |
the GPIO pin you want to user |
active_high |
if True the on method sets the line high if false the off method sets the line high. |
initial_value |
if false the device is initially off if true device is initially on |
pin_factory |
the underlying pin factory to use – let GPIO Zero set this. |
So for example:
led=LED(4, active_high=True, initial_value=True)
creates an LED associated with GPIO4 that is high when switched on and hence initially on.
There are only four methods associated with LED. The most commonly used are on and off which switch the LED on and off.
If you want to make the LED flash then you can use:
blink(on_time, off_time,n, background)
which will blink the LED n times, on for on_time and off for off_time specified in seconds. The background parameter defaults to true and this allows the blinking to be performed on another thread in the background. That is, if background is false the blink method blocks and does not return until the n blinks have been completed, which brings your program to a halt. If you want to do other things while the LED is flashing then set background to True or accept the default. The LED will then flash n times after blink has returned.
For example, our original Blinky LED program given in Chapter 3:
from gpiozero import LED from time import sleep led = LED(4) while True: led.on() sleep(1) led.off() sleep(1)
can be written as:
from gpiozero import LED from signal import pause led = LED(4) led.blink(on_time=1,off_time=1,n=100) print("Program Complete") pause()
If you try this out you will discover that the LED keeps flashing for almost 200 seconds after the program has printed Program Complete. Notice that you need the pause at the end of the program because if your program comes to a halt so do any threads that the program has created. In short, without the pause() you wouldn’t see the LED flash at all. The point is that usually when you give a Python instruction you only move on to the next instruction when the current instruction has completed.
This is generally called “blocking” because the current instruction stops the next instruction executing before it is complete. The call to blink is non-blocking because it returns before it has finished everything you told it to do and the next instruction is executed while it is unfinished. Instructions that are non-blocking are very useful when working with hardware because it allows your program to get on with something else while the hardware is doing something.
Compare the behavior of the background non-blocking program with a blocking version:
from gpiozero import LED led = LED(4) led.blink(on_time=1,off_time=1,n=100,background=False) print("Program Complete")
In this case you don’t need the pause() because the program waits for the LED to have completed 100 flashes. You will only see the Program Complete message after 200 seconds.
It is interesting that in either case the blink method makes use of a new thread to run LED in the background, the only difference is that when background is false the main thread waits for the blink thread to complete.
The toggle method simply changes the LED from on to off or off to on depending on its current state. You can use it to write the Blinky program in yet another way:
from gpiozero import LED from time import sleep led = LED(4) while True: led.toggle() sleep(1) led.toggle() sleep(1)
There are also some useful properties. The is_lit property is true if the LED is currently active and value sets and gets the state of the LED as a 1 or a 0.
Finally we have the pin property which returns the pin object that the LED is connected to. The Pin object provides lower-level access to the GPIO line.