1.1.3 LED Bar Graph

Introduction

In this project, we sequentially illuminate the lights on the LED Bar Graph.

Required Components

In this project, we need the following components.

../_images/1.1.3_led_bar_list.png

Schematic Diagram

T-Board Name

physical

wiringPi

BCM

GPIO18

Pin 12

1

18

GPIO23

Pin 16

4

23

GPIO24

Pin 18

5

24

GPIO25

Pin 22

6

25

SPICE0

Pin 24

10

8

SPICE1

Pin 26

11

7

GPIO12

Pin 32

26

12

GPIO16

Pin 36

27

16

GPIO20

Pin 38

28

22

GPIO21

Pin 40

29

21

../_images/1.1.3_LedBarGraph_schematic.png

Experimental Procedures

Step 1: Build the circuit.

Note

Pay attention to the direction when connecting. If you connect it backwards, it will not light up.

../_images/1.1.3_LedBarGraph_circuit.png

Step 2: Go to the folder of the code.

cd ~/davinci-kit-for-raspberry-pi/python-pi5

Step 3: Run the executable file.

sudo python3 1.1.3_LedBarGraph_zero.py

After the code runs, you will see the LEDs on the LED bar turn on and off regularly.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like davinci-kit-for-raspberry-pi/python-pi5. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3
from gpiozero import LED
from time import sleep

# Define GPIO pins where LEDs are connected
led_pins = [18, 23, 24, 25, 8, 7, 12, 16, 20, 21]

# Create LED objects for each pin
leds = [LED(pin) for pin in led_pins]

def odd_led_bar_graph():
    # Sequentially light up odd-numbered LEDs (index 0, 2, 4, etc.)
    for i in range(5):
        j = i * 2  # Calculate odd index
        leds[j].on()  # Turn on odd-numbered LED
        sleep(0.3)    # Delay for visual effect
        leds[j].off() # Turn off LED

def even_led_bar_graph():
    # Sequentially light up even-numbered LEDs (index 1, 3, 5, etc.)
    for i in range(5):
        j = i * 2 + 1  # Calculate even index
        leds[j].on()   # Turn on even-numbered LED
        sleep(0.3)     # Delay for visual effect
        leds[j].off()  # Turn off LED

def all_led_bar_graph():
    # Sequentially light up all LEDs one by one
    for led in leds:
        led.on()       # Turn on LED
        sleep(0.3)     # Delay for visual effect
        led.off()      # Turn off LED

def turn_off_all_leds():
    # Turn off all LEDs at once
    for led in leds:
        led.off()

try:
    # Main loop to cycle through LED patterns
    while True:
        odd_led_bar_graph()   # Activate odd-numbered LEDs
        sleep(0.3)            # Pause between patterns
        even_led_bar_graph()  # Activate even-numbered LEDs
        sleep(0.3)            # Pause between patterns
        all_led_bar_graph()   # Activate all LEDs
        sleep(0.3)            # Pause before restarting

except KeyboardInterrupt:
    # Handle interruption (Ctrl+C) gracefully
    turn_off_all_leds()      # Ensure all LEDs are turned off on exit
    pass

Code Explanation

  1. These lines import the necessary classes and functions. LED from gpiozero for LED control and sleep from time for delays.

    #!/usr/bin/env python3
    from gpiozero import LED
    from time import sleep
    
  2. The led_pins list contains the GPIO pin numbers. leds is a list of LED objects, each corresponding to a pin in led_pins.

    # Define GPIO pins where LEDs are connected
    led_pins = [18, 23, 24, 25, 8, 7, 12, 16, 20, 21]
    
    # Create LED objects for each pin
    leds = [LED(pin) for pin in led_pins]
    
  3. Let the LED on the odd digit of the LED Bar Graph light on in turn.

    def odd_led_bar_graph():
        # Sequentially light up odd-numbered LEDs (index 0, 2, 4, etc.)
        for i in range(5):
            j = i * 2  # Calculate odd index
            leds[j].on()  # Turn on odd-numbered LED
            sleep(0.3)    # Delay for visual effect
            leds[j].off() # Turn off LED
    
  4. Make the LED on the even digit of the LED Bar Graph light on in turn.

    def even_led_bar_graph():
        # Sequentially light up even-numbered LEDs (index 1, 3, 5, etc.)
        for i in range(5):
            j = i * 2 + 1  # Calculate even index
            leds[j].on()   # Turn on even-numbered LED
            sleep(0.3)     # Delay for visual effect
            leds[j].off()  # Turn off LED
    
  5. Let the LED on the LED Bar Graph light on one by one.

    def all_led_bar_graph():
        # Sequentially light up all LEDs one by one
        for led in leds:
            led.on()       # Turn on LED
            sleep(0.3)     # Delay for visual effect
            led.off()      # Turn off LED
    
  6. The while True loop continuously cycles through the LED patterns. The except block handles a KeyboardInterrupt (Ctrl+C), ensuring all LEDs are turned off on exit.

try:
    # Main loop to cycle through LED patterns
    while True:
        odd_led_bar_graph()   # Activate odd-numbered LEDs
        sleep(0.3)            # Pause between patterns
        even_led_bar_graph()  # Activate even-numbered LEDs
        sleep(0.3)            # Pause between patterns
        all_led_bar_graph()   # Activate all LEDs
        sleep(0.3)            # Pause before restarting

except KeyboardInterrupt:
    # Handle interruption (Ctrl+C) gracefully
    turn_off_all_leds()      # Ensure all LEDs are turned off on exit
    pass