Pi IoT In Python Using GPIO Zero - Pins
Written by Harry Fairhead & Mike James   
Tuesday, 20 February 2024
Article Index
Pi IoT In Python Using GPIO Zero - Pins
Setting a Pin Factory
How Fast?

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 

  • The Problem of Clocks

 

Summary

  • GPIO Zero uses the idea of a pin factory to allow a variety of direct interfaces with the GPIO lines to be used.

  • You should select a single pin factory to be used at the start of your program and not change it.

  • The default pin factory, lgpio, is installed by default and is currently the best choice.

  • The previous default pin factory rpigpio is about ten times faster than lgpio but it doesn’t work on the Pi 5.

  • In most cases, pin objects are constructed by the devices you use such as LEDs and you don’t have to interact with the pin factory at all.

  • You can use the pin factory directly to obtain a pin object and work with this directly

  • Working directly with a pin object is approximately ten times faster than working with it via a device-derived class.

  • Modern Pis have a variable rate clock, which can be a problem when you are trying to run IoT programs at the fastest speed irrespective of what the CPU load is.

  • You can use operating system commands to set the speed governor to a fixed speed, usually the maximum possible. However, CPU over-temperature events will still reduce the clock speed as a safety precaution.

Raspberry Pi IoT In Python Using GPIO Zero
Second Edition

By Harry Fairhead & Mike James

GPIOZero2E360

Buy from Amazon.

Contents

  1. Why Pi for IoT?
  2. Getting Started With Python And GPIO Zero
  3. Introduction to the GPIO
  4. Python - Class and Object
  5. Simple On/Off Devices
      Extract 1: On/Off Devices *
  6. Pins And Pin Factories
      Extract 1: Pins 
  7. Some Electronics
  8. Simple Input
      Extract 1:Getting Input ***NEW!!
  9. Complex Input Devices
      Extract 1: Complex Input *
  10. Pulse Width Modulation
      Extract 1:  PWM*
  11. Controlling Motors And Servos
      Extract 1: DC Motors *
  12. Working With Compound Devices
      Extract 1: Compound Devices*
  13. The SPI Bus
  14. Custom SPI Devices
  15. Using The Lgpio Library
  16. Appendix Visual Studio Code Remote Python
    *Extracts from first edition - will be updated.

 <ASIN:1871962870>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


AI Propels Python To Top Language on GitHub
30/10/2024

This year's Octoverse Report reveals how AI is expanding on GitHub and that Python has now overtaken JavaScript as the most popular language on GitHub. The use of Jupyter Notebooks has also surged.



Go At Highest Rank Ever in TIOBE Index
20/11/2024

Go is currently in 7th place in the TIOBE Index for November 2024. Not only is this is the highest position it has ever had, it's percentage rating is almost equal to its all-time-high. Will Go contin [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 20 February 2024 )