Arduino UNO Controlled Traffic Light with Joystick Interaction
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
This circuit is designed to simulate a traffic light system controlled by an Arduino UNO microcontroller and a joystick module. The joystick's button is used to cycle through the traffic light states: red, yellow, green, and off. The system operates in a sequence where each button press advances the traffic light to the next state in the cycle.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit for the circuit, controlling the traffic light states and reading input from the joystick module.
Joystick Module
- Description: An input device with a two-axis analog joystick and a push-button switch.
- Purpose: Provides user input to control the traffic light sequence. The analog inputs are unused in this circuit.
Traffic Light
- Description: A module with three LEDs (Red, Yellow, Green) representing a traffic light.
- Purpose: Displays the traffic light states as controlled by the Arduino UNO.
Wiring Details
Arduino UNO
- 5V: Provides power to the joystick module.
- GND: Common ground for the Arduino, joystick module, and traffic light.
- A0: Unused analog input.
- A1: Unused analog input.
- D2: Connected to the red LED on the traffic light.
- D3: Connected to the yellow LED on the traffic light.
- D4: Connected to the green LED on the traffic light.
- D7: Connected to the joystick button.
Joystick Module
- SW: Button switch connected to digital pin D7 on the Arduino UNO.
- VRX: Unused analog output.
- VRY: Unused analog output.
- VCC: Power input connected to 5V on the Arduino UNO.
- GND: Ground connected to GND on the Arduino UNO.
Traffic Light
- Green: Connected to digital pin D4 on the Arduino UNO.
- Yellow: Connected to digital pin D3 on the Arduino UNO.
- Red: Connected to digital pin D2 on the Arduino UNO.
- GND: Ground connected to GND on the Arduino UNO.
Documented Code
/*
* This Arduino sketch controls a traffic light system using a joystick module
* as a touch sensor. When the joystick button is pressed, the red LED turns on.
* On the next press, the yellow LED turns on. On the next press, the green LED
* turns on. On the next press, all LEDs turn off. This cycle repeats with each
* subsequent press.
*/
const int buttonPin = 7; // Joystick button connected to digital pin 7
const int redLedPin = 2; // Red LED connected to digital pin 2
const int yellowLedPin = 3; // Yellow LED connected to digital pin 3
const int greenLedPin = 4; // Green LED connected to digital pin 4
int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = 0; // Variable for tracking the LED state
void setup() {
pinMode(buttonPin, INPUT); // Initialize the button pin as an input
pinMode(redLedPin, OUTPUT); // Initialize the red LED pin as an output
pinMode(yellowLedPin, OUTPUT); // Initialize the yellow LED pin as an output
pinMode(greenLedPin, OUTPUT); // Initialize the green LED pin as an output
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
// Check if the button is pressed
if (buttonState == HIGH && lastButtonState == LOW) {
ledState = (ledState + 1) % 4; // Cycle through LED states
updateLeds(); // Update the LEDs based on the current state
}
lastButtonState = buttonState; // Save the current button state
}
void updateLeds() {
// Turn off all LEDs
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
// Turn on the appropriate LED based on the current state
if (ledState == 1) {
digitalWrite(redLedPin, HIGH);
} else if (ledState == 2) {
digitalWrite(yellowLedPin, HIGH);
} else if (ledState == 3) {
digitalWrite(greenLedPin, HIGH);
}
}
This code is responsible for the logic behind the traffic light system. It initializes the pins connected to the joystick button and the LEDs of the traffic light. In the main loop, it reads the state of the joystick button and cycles through the LED states accordingly. The updateLeds() function is used to turn on the appropriate LED based on the current state.