Raspberry Pi 4 Project 01: Difference between revisions
From ikev.in/w
No edit summary |
No edit summary |
||
Line 37: | Line 37: | ||
<pre> | <pre> | ||
import RPi.GPIO as GPIO | |||
import time | |||
# | # Set the GPIO pins for the LED bar graph | ||
for | led_pins = [21, 20, 16, 12, 7, 8, 25, 24, 23, 18] | ||
# Wait a short amount of time | # Set the GPIO mode to BCM | ||
time.sleep(0.05) | 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> | </pre> |
Revision as of 23:38, 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 LEDs
- 10 220-ohm resistors
- Jumper wires
Circuit Diagram
The circuit diagram for this project is shown below:
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 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()