Arduino-Controlled Traffic Light System with Joystick Interface
View and Edit Project In Cirkit Designer
Traffic Light Control System Documentation
Summary
This document describes a traffic light control system that is operated using an Arduino UNO microcontroller and a joystick module. The system allows for the control of a traffic light with three LEDs (red, yellow, and green) through the manipulation of the joystick. The joystick's movements are mapped to specific traffic light states: moving the joystick up activates the red LED, left for the yellow LED, right for the green LED, and down for all LEDs. The system is designed for educational purposes to demonstrate basic digital input/output control with an Arduino.
Component List
Traffic Light
- Description: A simulated traffic light with three LEDs (Green, Yellow, Red).
- Purpose: To display the traffic light status based on joystick input.
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: To process the input from the joystick and control the traffic light LEDs.
Joystick Module
- Description: An input device with two axes of control and a pushbutton switch.
- Purpose: To provide user input for controlling the traffic light system.
Wiring Details
Traffic Light
- Green LED: Connected to Arduino UNO's Digital Pin D4.
- Yellow LED: Connected to Arduino UNO's Digital Pin D3.
- Red LED: Connected to Arduino UNO's Digital Pin D2.
- GND: Connected to Arduino UNO's Ground (GND).
Arduino UNO
- Digital Pin D4: Connected to the Green LED of the Traffic Light.
- Digital Pin D3: Connected to the Yellow LED of the Traffic Light.
- Digital Pin D2: Connected to the Red LED of the Traffic Light.
- Ground (GND): Common ground for the Traffic Light and Joystick Module.
- 5V: Provides power to the Joystick Module.
- Analog Pin A0: Reads the X-axis value from the Joystick Module.
- Analog Pin A1: Reads the Y-axis value from the Joystick Module.
- Digital Pin D7: Reads the switch state from the Joystick Module.
Joystick Module
- VCC: Connected to Arduino UNO's 5V output.
- GND: Connected to Arduino UNO's Ground (GND).
- VRX: Connected to Arduino UNO's Analog Pin A0.
- VRY: Connected to Arduino UNO's Analog Pin A1.
- SW: Connected to Arduino UNO's Digital Pin D7.
Documented Code
/*
* This Arduino sketch controls a traffic light system using a joystick module.
* - Moving the joystick up turns on the red LED.
* - Moving the joystick left turns on the yellow LED.
* - Moving the joystick right turns on the green LED.
* - Moving the joystick down turns on all LEDs.
*/
const int redLEDPin = 2;
const int yellowLEDPin = 3;
const int greenLEDPin = 4;
const int joystickSWPin = 7;
const int joystickVRXPin = A0;
const int joystickVRYPin = A1;
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(joystickSWPin, INPUT);
pinMode(joystickVRXPin, INPUT);
pinMode(joystickVRYPin, INPUT);
}
void loop() {
int vrxValue = analogRead(joystickVRXPin);
int vryValue = analogRead(joystickVRYPin);
if (vryValue < 300) { // Joystick moved up
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (vrxValue < 300) { // Joystick moved left
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
} else if (vrxValue > 700) { // Joystick moved right
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
} else if (vryValue > 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);
}
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the traffic light LEDs and the joystick module as inputs or outputs in the setup() function. The loop() function continuously reads the joystick's position and updates the state of the traffic light LEDs accordingly.