Arduino UNO Controlled Traffic Light with Joystick Interface
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a traffic light using an Arduino UNO and a joystick module. The joystick's movements dictate the state of the traffic light LEDs (Red, Yellow, Green). Moving the joystick up activates the red LED, left for yellow, right for green, and down for all LEDs. The joystick also has a 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.
- Purpose: Acts as the central processing unit of the circuit, reading inputs from the joystick and controlling the traffic light LEDs.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Traffic Light
- Description: A module with three LEDs (Green, Yellow, Red) representing a traffic light.
- Purpose: Displays the traffic light status based on the joystick input.
- Pins: Green, Yellow, Red, GND
Joystick Module
- Description: An input device with two analog axes (VRX, VRY) and a pushbutton switch (SW).
- Purpose: Provides user input to control the traffic light LEDs.
- Pins: SW, VRY, VRX, VCC, GND
Wiring Details
Arduino UNO
- 5V connected to Joystick module VCC
- GND connected to Traffic Light GND and Joystick module 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
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
Joystick Module
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
- VRX connected to Arduino UNO A0
- VRY connected to Arduino UNO A1
- SW connected to Arduino UNO D7
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 redLED = 2;
const int yellowLED = 3;
const int greenLED = 4;
const int joyX = A0;
const int joyY = A1;
const int joySW = 7;
void setup() {
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(joySW, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int xValue = analogRead(joyX);
int yValue = analogRead(joyY);
int swValue = digitalRead(joySW);
if (yValue < 300) { // Joystick moved up
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
} else if (xValue < 300) { // Joystick moved left
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
} else if (xValue > 700) { // Joystick moved right
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, HIGH);
} else if (yValue > 700) { // Joystick moved down
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, HIGH);
} else { // Joystick in neutral position
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
}
delay(100); // Small delay for debounce
}
This code 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 switch as an input with an internal pull-up resistor. The loop function continuously reads the joystick's analog values to determine its position and sets the LEDs accordingly. The switch value is read but not used in the logic.