3. Chapter LED Bar Graph

We have learned how to control one LED to blink. Next, we will learn how to control a number of LEDs

3.1. Project Flowing Water Light

In this project, we use a number of LEDs to make a flowing water light.

3.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

Jumper Wires x6

jumper-wire

Bar Graph LED x1

LED-BAR

Resistor 220Ω x10

res-220R-hori

3.1.2. Component knowledge

Let us learn about the basic features of these components to use and understand them better.

3.1.2.1. Bar Graph LED

A Bar Graph LED has 10 LEDs integrated into one compact component. The two rows of pins at its bottom are paired to identify each LED like the single LED used earlier.

../../../_images/LED_BAR_NUM.png

3.1.3. Circuit

A reference system of labels is used in the circuit diagram below. Pins with the same network label are connected together.

Schematic diagram

../../../_images/LED-Graph-Sch.png

Hardware connection. If you need any support, please feel free to contact us via: support@freenove.com

../../../_images/LED-Graph-Fritzing.png

Note

If LEDbar doesn’t work, rotate LEDbar 180° to try. The label is random.

In this circuit, the cathodes of the LEDs are connected to the GPIO, which is different from the previous circuit. The LEDs turn ON when the GPIO output is low level in the program.

3.1.4. Code

This project is designed to make a flowing water lamp, which are these actions: First turn LED #1 ON, then turn it OFF. Then turn LED #2 ON, and then turn it OFF… and repeat the same to all 10 LEDs until the last LED is turns OFF. This process is repeated to achieve the “movements” of flowing water.

3.1.4.1. Python Code LightWater

First observe the project result, and then view the code.

Hint

If you have any concerns, please contact us via: support@freenove.com

  1. Use cd command to enter 03.1.1_LightWater directory of Python code.

$ cd ~/Freenove_Kit/Code/Python_GPIOZero_Code/03.1.1_LightWater
  1. Use Python command to execute Python code “LightWater.py”.

$ python LightWater.py

After the program is executed, you will see that LED Bar Graph starts with the flowing water way to be turned on from left to right, and then from right to left.

The following is the program code:

 1#!/usr/bin/env python3
 2########################################################################
 3# Filename    : LightWater.py
 4# Description : Use LEDBar Graph(10 LED) 
 5# Author      : www.freenove.com
 6# modification: 2023/05/11
 7########################################################################
 8from gpiozero import LED
 9from time import sleep
10
11ledPins = [17, 18, 27, 22, 23, 24, 25, 2, 3, 8]
12leds = [LED(pin=pin) for pin in ledPins] 
13
14def loop():
15    while True:
16        for index in range(0,len(ledPins),1):      # make led(on) move from left to right
17            leds[index].off()  
18            sleep(0.1)
19            leds[index].on() 
20        for index in range(len(ledPins)-1,-1,-1):   #move led(on) from right to left
21            leds[index].off()  
22            sleep(0.1)
23            leds[index].on() 
24
25if __name__ == '__main__':     # Program entrance
26    print ('Program is starting...')
27    try:
28        loop()
29    except KeyboardInterrupt:  # Press ctrl-c to end the program.
30        print("Ending program")
31    finally:
32        for index in range(0,len(ledPins),1): 
33            leds[index].close()  
34

Import the LED class that controls LED Bar Graph from the gpiozero library.

1from gpiozero import LED

Create the LED class for controlling the LEDBarGraph.

1ledPins = [17, 18, 27, 22, 23, 24, 25, 2, 3, 8]
2leds = [LED(pin=pin) for pin in ledPins]

The LED is turned on or off by specifying the index of the LED, if no parameter is specified, the same Settings are applied to all leds. leds.off() means that all leds are turned off.

1for index in range(0,len(ledPins),1):      # make led(on) move from left to right
2    leds[index].off()  
3    sleep(0.1)
4    leds[index].on() 
5for index in range(len(ledPins)-1,-1,-1):   #move led(on) from right to left
6    leds[index].off()  
7    sleep(0.1)
8    leds[index].on() 

In the program, first define 10 pins connected to the LED and set them to output mode. In the loop() function, two for loops are used to make the lights flow from right to left and from left to right.

 1def loop():
 2    while True:
 3        for index in range(0,len(ledPins),1):      # make led(on) move from left to right
 4            leds[index].off()  
 5            sleep(0.1)
 6            leds[index].on() 
 7        for index in range(len(ledPins)-1,-1,-1):   #move led(on) from right to left
 8            leds[index].off()  
 9            sleep(0.1)
10            leds[index].on() 

For more information about the methods used by the LEDBoard class in the GPIO Zero library,please refer to: https://gpiozero.readthedocs.io/en/stable/api_boards.html#ledboard

For more information about the methods used by the LEDBarGraph class in the GPIO Zero library,please refer to: https://gpiozero.readthedocs.io/en/stable/api_boards.html#ledbargraph

In this experiment you can use the LEDBoard and LEDBarGraph classes to control the LEDBarGraph