Raspberry Pi 4 Project 01: Difference between revisions

From ikev.in/w
(Created page with "= LED Bar Graph Pulsating Project = This project involves creating a 10-LED bar graph that pulsates light using a Raspberry Pi 4 and Python code. The LED bar graph will have 10 bars, each controlled by a GPIO pin on the Raspberry Pi. == Required Materials == To complete this project, you will need the following materials: * Raspberry Pi 4 * Breadboard * 10 LEDs * 10 220-ohm resistors * Jumper wires == Circuit Diagram == The circuit diagram for this project is shown...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 9: Line 9:
* Raspberry Pi 4
* Raspberry Pi 4
* Breadboard
* Breadboard
* 10 LEDs
* 10 Segment LED Bar Graph | Part COM-09937 (or 10 LEDs in a pinch)
* 10 220-ohm resistors
* 10 220-ohm resistors
* Jumper wires
* Jumper wires


== Circuit Diagram ==
Each LED is connected to a GPIO pin on the Raspberry Pi through a 220-ohm resistor. The GPIO pins used for each LED are as follows:
 
The circuit diagram for this project is shown below:
 
[[File:LED Bar Graph Pulsating Circuit Diagram.png|center|500px]]
 
In this diagram, each LED is connected to a GPIO pin on the Raspberry Pi through a 220-ohm resistor. The GPIO pins used for each LED are as follows:


* LED 1: GPIO 21
* LED 1: GPIO 21
Line 35: Line 29:


The Python code for this project is shown below:
The Python code for this project is shown below:
<pre>
import RPi.GPIO as GPIO
import time
# Set the GPIO pins for the LED bar graph
led_pins = [21, 20, 16, 12, 7, 8, 25, 24, 23, 18]
# Set the GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
# Set the LED pins as output
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)
# Set the initial duty cycle for the LEDs
dc = 0
# Loop forever
while True:
    # Increase the duty cycle from 0 to 100 in steps of 5
    for dc in range(0, 101, 5):
        # Set the duty cycle for each LED
        for i in range(len(led_pins)):
            GPIO.output(led_pins[i], dc > (i*10))
            # ^^^ If the duty cycle is greater than the threshold for this LED, turn it on.
        # Wait a short amount of time
        time.sleep(0.05)
    # Decrease the duty cycle from 100 to 0 in steps of 5
    for dc in range(100, -5, -5):
        # Set the duty cycle for each LED
        for i in range(len(led_pins)):
            GPIO.output(led_pins[i], dc > (i*10))
            # ^^^ If the duty cycle is greater than the threshold for this LED, turn it on.
        # Wait a short amount of time
        time.sleep(0.05)
# Cleanup when the program is stopped
GPIO.cleanup()
</pre>
[[Category:Raspberry Pi Projects]]
[[Category:Single-board microcontrollers]]
[[Category:Raspberry Pi]]

Latest revision as of 23:52, 21 April 2023

LED Bar Graph Pulsating Project

This project involves creating a 10-LED bar graph that pulsates light using a Raspberry Pi 4 and Python code. The LED bar graph will have 10 bars, each controlled by a GPIO pin on the Raspberry Pi.

Required Materials

To complete this project, you will need the following materials:

  • Raspberry Pi 4
  • Breadboard
  • 10 Segment LED Bar Graph | Part COM-09937 (or 10 LEDs in a pinch)
  • 10 220-ohm resistors
  • Jumper wires

Each LED is connected to a GPIO pin on the Raspberry Pi through a 220-ohm resistor. The GPIO pins used for each LED are as follows:

  • LED 1: GPIO 21
  • LED 2: GPIO 20
  • LED 3: GPIO 16
  • LED 4: GPIO 12
  • LED 5: GPIO 7
  • LED 6: GPIO 8
  • LED 7: GPIO 25
  • LED 8: GPIO 24
  • LED 9: GPIO 23
  • LED 10: GPIO 18

Python Code

The Python code for this project is shown below:

import RPi.GPIO as GPIO
import time

# Set the GPIO pins for the LED bar graph
led_pins = [21, 20, 16, 12, 7, 8, 25, 24, 23, 18]

# Set the GPIO mode to BCM
GPIO.setmode(GPIO.BCM)

# Set the LED pins as output
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)

# Set the initial duty cycle for the LEDs
dc = 0

# Loop forever
while True:
    # Increase the duty cycle from 0 to 100 in steps of 5
    for dc in range(0, 101, 5):
        # Set the duty cycle for each LED
        for i in range(len(led_pins)):
            GPIO.output(led_pins[i], dc > (i*10))
            # ^^^ If the duty cycle is greater than the threshold for this LED, turn it on.

        # Wait a short amount of time
        time.sleep(0.05)

    # Decrease the duty cycle from 100 to 0 in steps of 5
    for dc in range(100, -5, -5):
        # Set the duty cycle for each LED
        for i in range(len(led_pins)):
            GPIO.output(led_pins[i], dc > (i*10))
            # ^^^ If the duty cycle is greater than the threshold for this LED, turn it on.

        # Wait a short amount of time
        time.sleep(0.05)

# Cleanup when the program is stopped
GPIO.cleanup()