Pi IoT In Python Using GPIO Zero - Pins |
Written by Harry Fairhead & Mike James | ||||
Tuesday, 20 February 2024 | ||||
Page 3 of 3
How Fast?A key question for any IoT system is how fast can it operate? In this context we need a rough idea how fast GPIO Zero can toggle GPIO lines. Notice that this question depends not just on GPIO Zero, but also on the pin factory in use. For simplicity, we will look at only lgpio, the default and rpigpio, the previous default. We can answer this question immediately with a simple program: from gpiozero import LED led = LED(4) while True: led.on() led.off() If you run this on a Pi Zero using rpigpio, which is the fastest pin factory, the output is 3.6kHz, which means you can switch an LED on for 136µs. For a Pi 4 the output is 51kHz and just short of 10µs. In other words, the Pi 4 is around ten times faster. As LGPIOFactory is the only pin factory that works with the Pi 5 we cannot try this with rpigpio, but the same program, using lgpio, produces 20µs pulses with a frequency of 24kHz. You might think that an apparently simpler program: from gpiozero import LED led = LED(4) while True: led.toggle() would get the job done faster, but no. The toggle operation is more complex and the toggle method simply hides this. In this case a Pi Zero runs at 1.8kHz and a Pi 4 at 24kHz, i.e. this method takes twice as long. What about working with the pin directly? This should be faster as it avoids using additional code in GPIO Zero. Using the default pin factory: from gpiozero import Device Device() pin = Device.pin_factory.pin(4) pin._set_function("output") while True: pin.state=1 pin.state=0 If you try this out using rpigpio on a Pi Zero you will find that you get 25kHz and on a Pi 4 300kHz, i.e. more than ten times faster than using the LED object. Different pin factories will give you different upper limits, but the speed increase using the pin object directly should be similar. On a Pi 5, the frequency is 38kHz, which is a consequence of the LGPIOFactory being about ten times slower than rpigpio. Roughly speaking, GPIO Zero running on a Pi Zero with rpigpio is of the order of 100µs to 20µs and on a Pi 4 10µs to 3µs is its characteristic time. On a Pi 5 with LGPIOFactory the characteristic time is 40µs to 20µs. On the same machine rpigio is about ten time faster than LGPIOFactory. Compare this to the same task programmed directly in C, which has a characteristic time of around 0.01us, i.e. 10ns, i.e. at least 100 times faster than the fastest pin factory on a Pi 4 and 1000 times faster than lgpio on a Pi 5. There is no doubt if you need speed for this sort of task then Python is not the language to use. C is a much better choice as outlined in Raspberry Pi IoT in C, Second Edition, ISBN:9781871962635. This said, there are many applications that operate at almost human timescales and for this Python is very adequate and much simpler. If you can, use Python and unless speed is an issue use the default pin factory. In book but not in this extract
Summary
Raspberry Pi IoT In Python Using GPIO Zero
|
||||
Last Updated ( Tuesday, 20 February 2024 ) |