Circuit Documentation
Summary
This circuit is designed to control a traffic light using an Arduino UNO and a joystick module. The joystick's position determines which LED on the traffic light is illuminated. Moving the joystick up activates the red LED, left for the yellow LED, right for the green LED, and down for all LEDs. The Arduino UNO reads the joystick's analog values and drives the corresponding LEDs on the traffic light.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central controller for reading joystick inputs and driving the traffic light LEDs.
- Pins Used:
5V,GND,A0,A1,D2,D3,D4.
Traffic Light
- Description: A simple traffic light module with three LEDs (Red, Yellow, Green).
- Purpose: Displays the status of the traffic light based on the joystick input.
- Pins Used:
Green,Yellow,Red,GND.
Joystick Module
- Description: An input device that provides two analog signals corresponding to the X (VRX) and Y (VRY) axes and a digital signal for the switch (SW).
- Purpose: Provides user input to control the traffic light LEDs.
- Pins Used:
SW,VRY,VRX,VCC,GND.
Wiring Details
Arduino UNO
5Vconnected to Joystick ModuleVCC.GNDconnected to Joystick ModuleGNDand Traffic LightGND.A0connected to Joystick ModuleVRX.A1connected to Joystick ModuleVRY.D2connected to Traffic LightRed.D3connected to Traffic LightYellow.D4connected to Traffic LightGreen.
Traffic Light
Greenconnected to Arduino UNOD4.Yellowconnected to Arduino UNOD3.Redconnected to Arduino UNOD2.GNDconnected to Arduino UNOGND.
Joystick Module
VCCconnected to Arduino UNO5V.GNDconnected to Arduino UNOGND.VRXconnected to Arduino UNOA0.VRYconnected to Arduino UNOA1.
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.
*/
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
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(vrxPin, INPUT);
pinMode(vryPin, INPUT);
}
void loop() {
int vrxValue = analogRead(vrxPin);
int vryValue = analogRead(vryPin);
// 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);
}
}
Filename: sketch.ino
This code is responsible for reading the analog values from the joystick and controlling the state of the traffic light LEDs accordingly. The setup() function initializes the LED pins as outputs and the joystick pins as inputs. The loop() function continuously reads the joystick values and turns on the appropriate LEDs based on the joystick's position.
