Arduino UNO Controlled Traffic Light with Joystick Interface
View and Edit Project In Cirkit Designer
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 in different directions, the corresponding LED lights up on the traffic light. The joystick also has a built-in switch that, when pressed, turns on all the LEDs. The Arduino UNO reads the analog values from the joystick's potentiometers to determine its position and controls the LEDs accordingly.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit of the circuit, reading input 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 simple traffic light module with three LEDs (Green, Yellow, Red).
- Purpose: Displays the traffic light status based on the joystick input.
- Pins: Green, Yellow, Red, GND.
Joystick Module
- Description: An input device with a two-axis analog joystick and a pushbutton switch.
- Purpose: Provides user input to control the traffic light LEDs.
- Pins: SW (switch), VRY (vertical potentiometer), VRX (horizontal potentiometer), VCC, GND.
Wiring Details
Arduino UNO
- 5V connected to Joystick module VCC.
- 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.
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 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;
// Reset all LEDs
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
if (yValue < 300) { // Joystick moved up
digitalWrite(redLedPin, HIGH);
} else if (xValue < 300) { // Joystick moved left
digitalWrite(yellowLedPin, HIGH);
} else if (xValue > 700) { // Joystick moved right
digitalWrite(greenLedPin, HIGH);
} else if (yValue > 700) { // Joystick moved down
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
}
delay(100); // Small delay for debounce
}
This code is saved in a file named "norlis afni 2 point 0.ino". It initializes the pins connected to the traffic light LEDs and the joystick module. In the main loop, it reads the analog values from the joystick to determine its position and sets the corresponding LED pins to HIGH to turn on the LEDs. The joystick's switch is used to turn on all LEDs when pressed. The code includes a small delay to debounce the joystick input.