Arduino-Controlled Traffic Light System with Joystick Interaction
View and Edit Project In Cirkit Designer
Traffic Light Control System Documentation
Summary
The circuit described in this documentation 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 are controlled based on the movement of the joystick. The joystick's vertical and horizontal movements are used to switch between the traffic light states, while a button press on the joystick can activate all lights simultaneously.
Component List
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
- Purpose: Acts as the central processing unit for the traffic light control system, reading inputs from the joystick and driving the traffic light LEDs.
Traffic Light
- Description: A simple traffic light module with three LEDs (Green, Yellow, Red).
- Pins: Green, Yellow, Red, GND
- Purpose: Represents a traffic light, with each LED corresponding to a traffic signal.
Joystick Module
- Description: An input module with a two-axis joystick and a pushbutton switch.
- Pins: SW, VRY, VRX, VCC, GND
- Purpose: Provides user input to control the traffic light system. The joystick's axes are used to select which LED to illuminate, and the switch can be used to turn on all LEDs.
Wiring Details
Arduino UNO
- D4 connected to Traffic Light Green LED
- D3 connected to Traffic Light Yellow LED
- D2 connected to Traffic Light Red LED
- A0 connected to Joystick Module VRX
- A1 connected to Joystick Module VRY
- D7 connected to Joystick Module SW
Traffic Light
- Green connected to Arduino UNO D4
- Yellow connected to Arduino UNO D3
- Red connected to Arduino UNO D2
- GND connected to Arduino UNO GND
Joystick Module
- SW connected to Arduino UNO D7
- VRY connected to Arduino UNO A1
- VRX connected to Arduino UNO A0
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
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.
*/
const int redLEDPin = 2; // Red LED connected to D2
const int yellowLEDPin = 3; // Yellow LED connected to D3
const int greenLEDPin = 4; // Green LED connected to D4
const int vrxPin = A0; // Joystick VRX connected to A0
const int vryPin = A1; // Joystick VRY connected to A1
const int swPin = 7; // Joystick SW connected to D7
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(swPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int vrxValue = analogRead(vrxPin);
int vryValue = analogRead(vryPin);
int swValue = digitalRead(swPin);
// Center values for the joystick
const int centerX = 512;
const int centerY = 512;
const int threshold = 100;
// Reset all LEDs
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
if (vryValue < centerY - threshold) {
// Joystick moved up
digitalWrite(redLEDPin, HIGH);
} else if (vrxValue < centerX - threshold) {
// Joystick moved left
digitalWrite(yellowLEDPin, HIGH);
} else if (vrxValue > centerX + threshold) {
// Joystick moved right
digitalWrite(greenLEDPin, HIGH);
} else if (vryValue > centerY + threshold) {
// Joystick moved down
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
}
delay(100); // Small delay to avoid flickering
}
This code is responsible for reading the joystick's position and button state and controlling the traffic light LEDs accordingly. The setup() function initializes the pins connected to the LEDs and the joystick switch. The loop() function continuously reads the joystick's analog values to determine its position and sets the state of the LEDs based on the joystick's movement.