Arduino-Controlled Traffic Light System with Joystick Interaction
View and Edit Project In Cirkit Designer
Traffic Light Control System Documentation
Summary
The circuit described herein is designed to control a traffic light system using an Arduino UNO microcontroller and a joystick module. The traffic light system consists of three LEDs (red, yellow, and green) that represent the traffic signals. The joystick module is used to change the state of the traffic lights: moving the joystick up activates the red light, left for the yellow light, right for the green light, and down for all lights. The system is programmed to respond to the joystick's position and update the traffic lights accordingly.
Component List
Traffic Light
- Description: A module with three LEDs (Green, Yellow, Red) representing a traffic light.
- Pins: Green, Yellow, Red, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Joystick Module
- Description: An input module with a two-axis joystick and a pushbutton.
- Pins: SW, VRY, VRX, VCC, GND
Wiring Details
Traffic Light
- Green LED: Connected to Arduino UNO pin D4.
- Yellow LED: Connected to Arduino UNO pin D3.
- Red LED: Connected to Arduino UNO pin D2.
- GND: Common ground with Arduino UNO.
Arduino UNO
- D4: Connected to the Green LED of the Traffic Light.
- D3: Connected to the Yellow LED of the Traffic Light.
- D2: Connected to the Red LED of the Traffic Light.
- A0: Connected to the VRX pin of the Joystick Module.
- A1: Connected to the VRY pin of the Joystick Module.
- D7: Connected to the SW pin of the Joystick Module.
- D5: Provides VCC to the Joystick Module.
- GND: Common ground with Traffic Light and Joystick Module.
Joystick Module
- VRX: Connected to Arduino UNO pin A0.
- VRY: Connected to Arduino UNO pin A1.
- SW: Connected to Arduino UNO pin D7.
- VCC: Powered by Arduino UNO pin D5.
- GND: Common ground with Arduino UNO.
Documented Code
/*
* This Arduino sketch controls a traffic light system using a joystick module.
* When the joystick is moved up, the red LED turns on.
* When the joystick is moved left, the yellow LED turns on.
* When the joystick is moved right, the green LED turns on.
* When the joystick is moved down, all LEDs turn on.
*/
// Pin definitions
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int joystickXPin = A0;
const int joystickYPin = A1;
const int joystickSwPin = 7;
void setup() {
// Initialize LED pins as outputs
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Initialize joystick switch pin as input
pinMode(joystickSwPin, INPUT);
// Start serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read joystick values
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
int swValue = digitalRead(joystickSwPin);
// Debugging output
Serial.print("X: ");
Serial.print(xValue);
Serial.print(" Y: ");
Serial.print(yValue);
Serial.print(" SW: ");
Serial.println(swValue);
// Determine LED states based on joystick position
if (yValue < 300) { // Joystick moved up
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
} else if (xValue < 300) { // Joystick moved left
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
} else if (xValue > 700) { // Joystick moved right
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
} else if (yValue > 700) { // Joystick moved down
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
} else { // Joystick in neutral position
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
// Small delay to avoid bouncing
delay(100);
}
This code is responsible for reading the joystick's position and updating the state of the traffic lights accordingly. It includes serial communication for debugging purposes, allowing the user to monitor the joystick's values in real-time.