Traffic Light Control Circuit Documentation
Summary
This circuit is designed to control a traffic light using an Arduino UNO microcontroller and a joystick module. The traffic light has three LEDs (Red, Yellow, Green) that are controlled by the joystick's position. Moving the joystick up turns on the red LED, left turns on the yellow LED, right turns on the green LED, and down turns on all LEDs. The joystick also has a built-in switch that is read by the Arduino but not used in the current code. The circuit is powered by the Arduino's 5V output, and the ground is shared across all components.
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 device that provides two analog signals (VRX and VRY) representing the X and Y positions of the joystick, and a digital signal (SW) when the joystick is pressed.
- 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: Connected to Arduino UNO GND
Arduino UNO
- 5V: Provides power to the Joystick module VCC
- GND: Common ground for Traffic Light and Joystick module
- D4: Controls the Green LED of the Traffic Light
- D3: Controls the Yellow LED of the Traffic Light
- D2: Controls the Red LED of the Traffic Light
- A0: Reads the X position (VRX) from the Joystick module
- A1: Reads the Y position (VRY) from the Joystick module
- D7: Reads the switch (SW) from the Joystick module (unused in code)
Joystick Module
- VCC: Connected to Arduino UNO 5V
- GND: Connected to Arduino UNO GND
- VRX: Connected to Arduino UNO pin A0
- VRY: Connected to Arduino UNO pin A1
- SW: Connected to Arduino UNO pin D7 (unused in code)
Documented Code
/*
* This Arduino sketch controls a traffic light 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;
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 vrxValue = analogRead(joystickVRXPin);
int vryValue = analogRead(joystickVRYPin);
int swValue = digitalRead(joystickSWPin);
// Debugging output
Serial.print("VRX: ");
Serial.print(vrxValue);
Serial.print(" VRY: ");
Serial.print(vryValue);
Serial.print(" SW: ");
Serial.println(swValue);
// Determine LED states based on joystick position
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);
}
// Small delay to avoid bouncing
delay(100);
}
The code is written for the Arduino UNO and is responsible for reading the joystick's analog and digital signals to control the traffic light LEDs. The joystick's vertical and horizontal movements are mapped to specific LEDs, and the switch is read but not used in the current functionality. Serial communication is established for debugging purposes, allowing the user to monitor the joystick's values.
