Pi IoT In Python Using GPIO Zero - Compound Devices
Written by Harry Fairhead & Mike James   
Monday, 07 February 2022
Article Index
Pi IoT In Python Using GPIO Zero - Compound Devices
An Example
  • LEDBarGraph


This treats the LEDs connected to the specified pins as a bar graph. Setting a value between -1 and 1 lights up the corresponding proportion of the LEDs with negative values, working from the top of the list of pins. So:


sets the first half of the LEDs on and:


sets the last half of the LEDs on.

It is worth knowing that you can get packaged LED Bars with 10 or 20 segments. However, you need to keep in mind that allocating even as few as 10 GPIO lines to drive such a device might leave you short for other a


  • TrafficLights

TrafficLights(red, amber, green, *, yellow=None,
pwm=False, initial_value=False, pin_factory=None)

This controls a simple traffic light device by specifying the pins that the red, amber and green LEDs are connected to. You can control the lights using the red, amber, green attributes. The attribute yellow can be used as an alternative name for amber. You have to provide the code that changes the lights, e.g. green, red/amber, red and so on. You can also use PWM to vary the brightness, but it isn’t clear why you might want to do this.

There are also some classes that work with off-the-shelf boards made by a number of different companies.

In Chapter but not in this extract

  • PiHutXmasTree
  • LedBorg
  • PiLiter
  • PiLiterBarGraph
  • SnowPi
  • PiTraffic
  • PiStop
  • StatusZero
  • PumpkinPi
  • General I/O Composites
  • The Robots
  • Refactoring the Stepper Motor Class



  • Inheritance isn’t the only way classes can be reused. Composition, or containment, placing instances of other classes in a class, is also very useful.

  • The CompositeDevice class makes creating compound devices very easy and more organized than an ad hoc approach.

  • The CompositeOutputDevice class adds some methods that are appropriate for a compound device that contains nothing but output devices.

  • Driving multiple LEDs is so common a task that there is a special class, LEDCollection, designed to do just this.

  • There are a large number of off-the-shelf compound devices based on LEDCollection. Even though they are proprietary devices, it is easy to make your own from basic components.

  • There are also some more general, off-the-shelf, compound devices that use LED, Buttons and sound devices.

  • Finally, there are a set of robot classes which essentially consist of motor objects and methods to allow control in terms of direction and speed.

  • The stepper motor class created in earlier chapters is easy to refactor using CompositeDevice and you can even add a background task to make it rotate without the involvement of the main program.


Raspberry Pi IoT In Python Using GPIO Zero
Second Edition

By Harry Fairhead & Mike James


Buy from Amazon.


  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.


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.


Udacity Offers More AWS Scholarships

Udacity has announced it is accepting applications for the next wave of 1,000 AWS AI & ML Scholarships. Any student over the age of 16 who self-identifies as under-served or under-represented in t [ ... ]

Apache Releases NetBeans 22

Apache NetBeans 22 has been released. This release has a new Gradle Wizard, upgrades to Java 22 javac, and a number of UI changes.

More News

kotlin book



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

Last Updated ( Monday, 07 February 2022 )