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 KY-023 Dual Axis Joystick Module. The traffic light has three LEDs (Red, Yellow, Green) that are controlled by the joystick's position. When the joystick is moved in different directions, the corresponding LED lights up on the traffic light. The joystick also has a built-in switch that is used in this circuit.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins Used: 5V, GND, A0, A1, D2, D3, D4, D7.
- Purpose: Acts as the central controller for the circuit, reading inputs from the joystick and controlling the output to the traffic light LEDs.
Traffic Light
- Description: A simple traffic light module with three LEDs (Red, Yellow, Green).
- Pins Used: Green, Yellow, Red, GND.
- Purpose: Displays the traffic light status based on the joystick input.
KY-023 Dual Axis Joystick Module
- Description: A module that provides two-axis analog input and a push-button switch.
- Pins Used: GND, +5V, VRx, VRy, SW.
- Purpose: Provides user input to control the traffic light LEDs.
Wiring Details
Arduino UNO
- 5V to KY-023 Joystick Module +5V
- GND to KY-023 Joystick Module GND and Traffic Light GND
- A0 to KY-023 Joystick Module VRx
- A1 to KY-023 Joystick Module VRy
- D2 to Traffic Light Red
- D3 to Traffic Light Yellow
- D4 to Traffic Light Green
- D7 to KY-023 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
KY-023 Dual Axis Joystick Module
- GND connected to Arduino UNO GND
- +5V connected to Arduino UNO 5V
- 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.
* 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;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int joystickXPin = A0;
const int joystickYPin = A1;
const int joystickButtonPin = 7;
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(joystickButtonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
bool buttonPressed = digitalRead(joystickButtonPin) == LOW;
// Center values for the joystick
const int centerX = 512;
const int centerY = 512;
const int threshold = 100;
// Turn off all LEDs initially
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
if (yValue < centerY - threshold) { // Joystick moved up
digitalWrite(redLedPin, HIGH);
} else if (xValue < centerX - threshold) { // Joystick moved left
digitalWrite(yellowLedPin, HIGH);
} else if (xValue > centerX + threshold) { // Joystick moved right
digitalWrite(greenLedPin, HIGH);
} else if (yValue > centerY + threshold) { // Joystick moved down
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
}
delay(100); // Small delay to avoid flickering
}
This code is written for the Arduino UNO and is responsible for reading the joystick's analog values to determine its position. Depending on the position, it will light up the corresponding LED on the traffic light. The joystick's switch is also monitored, and when pressed, all LEDs will light up. The code includes setup for the pins and a loop that continuously checks the joystick's position and updates the LED states accordingly.