Circuit Documentation
Summary of the Circuit
This circuit is designed to control a traffic light using an Arduino UNO and a joystick module. The traffic light has three LEDs (red, yellow, and green) that are controlled by the joystick's position. When the joystick is moved up, the red LED turns on. Moving the joystick left turns on the yellow LED, and moving it right turns on the green LED. Pushing the joystick down will turn on all LEDs. The joystick also has a built-in switch that is read by the Arduino but not used in the current code.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins Used: GND, A0, A1, D2, D3, D4, D7.
Joystick Module
- Description: An input device that provides two analog signals (one for each axis) and a digital signal from a built-in switch.
- Pins Used: SW, VRY, VRX, GND.
Traffic Light
- Description: A module with three LEDs (red, yellow, and green) representing a traffic light.
- Pins Used: Green, Yellow, Red, GND.
Wiring Details
Arduino UNO
- GND connected to Joystick Module GND and Traffic Light GND.
- A0 connected to Joystick Module VRX.
- A1 connected to Joystick Module VRY.
- D2 connected to Traffic Light Red.
- D3 connected to Traffic Light Yellow.
- D4 connected to Traffic Light Green.
- D7 connected to Joystick Module SW.
Joystick Module
- SW connected to Arduino UNO D7.
- VRY connected to Arduino UNO A1.
- VRX connected to Arduino UNO A0.
- GND connected to Arduino UNO GND.
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.
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 joystick direction
if (vryValue < 300) { // Joystick moved up
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (vryValue > 700) { // Joystick moved down
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
} 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 { // Joystick in neutral position
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
}
// Small delay to avoid bouncing
delay(100);
}
This code is saved in a file named sketch.ino. It initializes the pins connected to the traffic light LEDs and the joystick, reads the joystick's analog values to determine its position, and sets the LEDs accordingly. Serial communication is also started for debugging purposes, allowing the user to monitor the joystick's analog values and switch state.
