.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _1.1.3_py_pi5: 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. .. image:: ../python_pi5/img/1.1.3_led_bar_list.png .. raw:: html
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 ============ ======== ======== === .. image:: ../python_pi5/img/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. .. image:: ../python_pi5/img/1.1.3_LedBarGraph_circuit.png **Step 2:** Go to the folder of the code. .. raw:: html .. code-block:: cd ~/davinci-kit-for-raspberry-pi/python-pi5 **Step 3**: Run the executable file. .. raw:: html .. code-block:: sudo python3 1.1.3_LedBarGraph.py After the code runs, you will see the LEDs on the LED bar turn on and off regularly. .. warning:: If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` **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. .. raw:: html .. code-block:: python #!/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** #. These lines import the necessary classes and functions. ``LED`` from ``gpiozero`` for LED control and ``sleep`` from ``time`` for delays. .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED from time import sleep #. The ``led_pins`` list contains the GPIO pin numbers. ``leds`` is a list of ``LED`` objects, each corresponding to a pin in ``led_pins``. .. code-block:: python # 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] #. Let the LED on the odd digit of the LED Bar Graph light on in turn. .. code-block:: python 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 #. Make the LED on the even digit of the LED Bar Graph light on in turn. .. code-block:: python 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 #. Let the LED on the LED Bar Graph light on one by one. .. code-block:: python 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 #. 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. .. code-block:: python 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