Push button rgb led rpi gpioimport timegpio red green blue gpio
Solved Step By Step with Explanation – Button-Controlled LED, Buzzer & RGB
Question
Solved Step By Step with Explanation –Button-Controlled LED, Buzzer & RGB
import time
GPIO.setmode(GPIO.BOARD)
while True:
button_state = GPIO.input(button_pin)
```
Explanation: This program uses the RPi.GPIO library to control the LED and read the button state. When the button is pressed, it toggles the state of the LED.
GPIO.setmode(GPIO.BOARD)
button_pin = 12
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(green_pin, GPIO.OUT)
GPIO.output(blue_pin, b)
while True:
set_rgb_color(0, 1, 0) # Green
time.sleep(0.5)
3. Push Button + Active Buzzer:
```python
buzzer_pin = 11
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
buzzer_state = not GPIO.input(buzzer_pin)
GPIO.output(buzzer_pin, buzzer_state)
```python
import RPi.GPIO as GPIO
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buzzer_pin, GPIO.OUT)
if button_state == GPIO.LOW:
buzzer.ChangeFrequency(880)
Explanation: This program plays two different frequencies on the passive buzzer when the button is pressed.
5. Push Button + RGB + Active Buzzer:
button_pin = 12
red_pin = 11
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(green_pin, GPIO.OUT)
GPIO.output(green_pin, g)
GPIO.output(blue_pin, b)
button_state = GPIO.input(button_pin)
if button_state == GPIO.LOW:
set_rgb_color(0, 1, 0) # Green
time.sleep(0.5)
Explanation: This program combines the functionalities of the previous scenarios. It toggles the buzzer and cycles through RGB colors when the button is pressed.
Remember that these programs assume a basic setup and use the RPi.GPIO library for controlling GPIO pins on a Raspberry Pi. Make sure you have the necessary components connected correctly and consider adapting the code to your specific hardware setup if needed.