Raspberrypi pico Traffic lights project with buzzer
Building a Traffic Light System with MicroPython and Raspberrypi pico
import machineimport utimeimport _threadled_red = machine.Pin(15, machine.Pin.OUT)led_amber = machine.Pin(14, machine.Pin.OUT)led_green = machine.Pin(13, machine.Pin.OUT)button = machine.Pin(16, machine.Pin.IN,machine.Pin.PULL_DOWN)buzzer = machine.Pin(12, machine.Pin.OUT)global button_pressedbutton_pressed = Falsedef button_reader_thread():global button_pressedwhile True:if button.value() == 1:button_pressed = True_thread.start_new_thread(button_reader_thread, ())while True:if button_pressed == True:led_red.value(1)for i in range(10):buzzer.value(0)utime.sleep(0.2)buzzer.value(1)utime.sleep(0.2)global button_pressedbutton_pressed = Falseled_red.value(1)utime.sleep(5)led_red.value(0)led_amber.value(1)utime.sleep(3)led_amber.value(0)led_green.value(1)utime.sleep(5)led_green.value(0)led_amber.value(1)utime.sleep(3)led_amber.value(0)
This code is written for the Raspberry Pi Pico, a microcontroller board based on the RP2040 chip. Let's break down the code step by step:
1. **Imports**:
- `machine`: This module provides low-level control over the hardware peripherals on the Raspberry Pi Pico.
- `utime`: This module provides functions for dealing with time-related tasks.
- `_thread`: This module provides support for multithreading in MicroPython.
2. **Pin Setup**:
- Pins are initialized for various components:
- `led_red`, `led_amber`, `led_green`: These pins are used to control LEDs connected to the Raspberry Pi Pico. They are set as output pins.
- `button`: This pin is connected to a button and is set as an input pin with a pull-down resistor configuration.
- `buzzer`: This pin is connected to a buzzer and is set as an output pin.
3. **Global Variables**:
- `button_pressed`: This variable is used to track whether the button has been pressed or not. It's initially set to `False`.
4. **Button Reader Thread**:
- A thread (`button_reader_thread`) is created to continuously monitor the state of the button.
- Inside the thread, if the button is pressed (`button.value() == 1`), `button_pressed` is set to `True`.
5. **Main Loop**:
- The main loop continuously runs and performs the following actions:
- If `button_pressed` is `True`, it turns on the red LED, activates the buzzer for 10 cycles of 0.2 seconds each, and then resets `button_pressed` to `False`.
- It then proceeds to cycle through a traffic light pattern:
- Red LED on for 5 seconds.
- Amber LED on for 3 seconds.
- Green LED on for 5 seconds.
- Amber LED on for 3 seconds.
- This pattern repeats indefinitely.
6. **Explanation**:
- The code sets up the Raspberry Pi Pico to control LEDs, read from a button, and produce sounds using a buzzer.
- It uses multithreading to continuously monitor the button state while the main loop controls the LEDs and buzzer based on the button state and a predefined traffic light pattern.
Overall, the code creates a simple traffic light system with an added feature that triggers a buzzer when a button is pressed.
Comments
Post a Comment