Arduino-Controlled Traffic Light System with Joystick Interface
View and Edit Project In Cirkit Designer
Traffic Light Control Circuit Documentation
Summary
This circuit is designed to control a traffic light system using an Arduino UNO microcontroller and a joystick module. The traffic light has three LEDs (Red, Yellow, Green) that are controlled based on the joystick's position. The joystick's movements in different directions control which LED is turned on. The circuit is powered by the Arduino UNO, which also processes the input from the joystick to control the traffic light.
Component List
Traffic Light
- Pins: Green, Yellow, Red, GND
- Description: A module with three LEDs representing a traffic light.
Arduino UNO
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
- Description: A microcontroller board based on the ATmega328P, used for controlling the traffic light and reading the joystick input.
Joystick Module
- Pins: SW, VRY, VRX, VCC, GND
- Description: An input device that provides analog signals based on the X (VRX) and Y (VRY) axes movement and a digital signal when the joystick is pressed (SW).
Wiring Details
Traffic Light
- Green LED: Connected to Arduino UNO's D4
- Yellow LED: Connected to Arduino UNO's D3
- Red LED: Connected to Arduino UNO's D2
- GND: Connected to Arduino UNO's GND
Arduino UNO
- 5V: Provides power to the Joystick module's VCC
- GND: Common ground for Traffic Light and Joystick module
- D2: Connected to Traffic Light's Red LED
- D3: Connected to Traffic Light's Yellow LED
- D4: Connected to Traffic Light's Green LED
- A0: Reads the X-axis value from Joystick module's VRX
- A1: Reads the Y-axis value from Joystick module's VRY
- D7: Reads the button press from Joystick module's SW (not used in the current code)
Joystick Module
- VCC: Connected to Arduino UNO's 5V
- GND: Connected to Arduino UNO's GND
- VRX: Connected to Arduino UNO's A0
- VRY: Connected to Arduino UNO's A1
- SW: Connected to Arduino UNO's D7 (not used in the current code)
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 joystickVRXPin = A0;
const int joystickVRYPin = A1;
void setup() {
// Initialize LED pins as outputs
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
// Initialize joystick pins as inputs
pinMode(joystickVRXPin, INPUT);
pinMode(joystickVRYPin, INPUT);
}
void loop() {
// Read joystick values
int xValue = analogRead(joystickVRXPin);
int yValue = analogRead(joystickVRYPin);
// Determine joystick direction
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);
}
}
The code is written in Arduino sketch format (.ino) and is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the traffic light LEDs as outputs and the joystick's analog pins as inputs. The loop function continuously reads the joystick's position and turns on the corresponding LED(s) based on the direction the joystick is moved.