Arduino UNO Controlled Traffic Light System with Joystick Interface
Control a Red, Yellow, and Green traffic light manually using a
joystick module and Arduino UNO. Push the joystick in different directions to switch
signals — perfect for learning analog input, digital output, and conditional logic.
Includes full diagram.json,
wiring table, Arduino code, and a Wokwi simulation link.
What This Project Does
This project simulates a real-world traffic signal using three LEDs and a joystick module. By reading the joystick's X and Y analog values (0–1023), the Arduino determines which direction the stick is pushed and activates the corresponding traffic light LED. It's one of the cleanest beginner projects for understanding analog-to-digital conversion, conditional logic, and multi-output GPIO control.
(simulated)
Joystick Left
Joystick Right
Joystick Up
Button Press
Key Features
Joystick Analog Logic Explained
The joystick module outputs two analog voltages corresponding to X-axis and Y-axis
position. Arduino's analogRead()
converts these to 10-bit values (0–1023). The center position reads approximately 512 on both axes.
X-Axis (HORIZ — pin A0)
Y-Axis (VERT — pin A1)
Complete Truth Table
| Joystick Action | X Value | Y Value | Red (pin 2) | Yellow (pin 3) | Green (pin 4) | Signal |
|---|---|---|---|---|---|---|
| ⬅️ Left | < 400 | Any | HIGH | LOW | LOW | STOP |
| ➡️ Right | > 600 | Any | LOW | LOW | HIGH | GO |
| ⬆️ Up | 400–600 | < 400 | LOW | HIGH | LOW | READY |
| 🔘 Button | Any | Any | HIGH | HIGH | HIGH | ALL ON |
| ⏺ Neutral | 400–600 | 400–600 | LOW | LOW | LOW | ALL OFF |
Note: Red and Green are driven directly by digitalWrite(PIN_RED, x < 400) and digitalWrite(PIN_GREEN, x > 600), so both can technically be on simultaneously if X is very noisy. Yellow is driven by Y-axis up (y < 400). Button press overrides everything via return.
Components Required
Complete Wiring Guide
Joystick Module → Arduino
| Joystick Pin | Arduino Pin | Wire | Notes |
|---|---|---|---|
| VCC | 5V | Red | Power supply |
| GND | GND | Black | Ground |
| HORIZ | A0 | Blue | X-axis analog (left/right) |
| VERT | A1 | Green | Y-axis analog (up/down) |
| SEL | D7 | Orange | Button — INPUT_PULLUP, LOW when pressed |
LED Circuit → Arduino
| LED | Anode (+) via 220Ω | Cathode (−) | Trigger |
|---|---|---|---|
| 🔴 Red LED | Digital 2 | GND | Joystick Left (x < 400) |
| 🟡 Yellow LED | Digital 3 | GND | Joystick Up (y < 400) |
| 🟢 Green LED | Digital 4 | GND | Joystick Right (x > 600) |
Always use 220Ω resistors in series with each LED. Without them, excess current will damage the LEDs and Arduino GPIO pins. In Wokwi, resistors are included automatically when using the LED component.
Step-by-Step Wokwi Setup
-
1
Open Wokwi and Create an Arduino Uno Project
Go to wokwi.com, click "New Project", and select "Arduino Uno". The Uno board appears on the canvas ready to use.
-
2
Paste diagram.json
Click the diagram.json tab in Wokwi, select all existing text, and paste the JSON from the section below. The full circuit — Arduino, joystick, and three LEDs with resistors — loads instantly.
-
3
Paste the Arduino Sketch
Click the sketch.ino tab, select all, and paste the code from the Code section below. No extra libraries are needed.
-
4
Press Play and Test
Click the green ▶ Play button. The sketch compiles and runs. Click and drag the joystick knob left, right, or up to trigger the LEDs. Click the joystick button to turn all three on.
✅Calibration tip: If your physical joystick reads different center values, adjust the thresholds (
400and600) in the code to match your hardware.
diagram.json — Complete Circuit
Paste this into the diagram.json tab in Wokwi to instantly load the full circuit — Arduino Uno, joystick module, and three LEDs with resistors, all pre-wired.
{
"version": 1,
"author": "MakeMindz",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 100, "left": 0, "attrs": {} },
{ "type": "wokwi-analog-joystick", "id": "joystick", "top": 40, "left": -220, "attrs": {} },
{ "type": "wokwi-led", "id": "led-red", "top": -60, "left": 80, "attrs": { "color": "red" } },
{ "type": "wokwi-led", "id": "led-yellow", "top": -60, "left": 140, "attrs": { "color": "yellow" } },
{ "type": "wokwi-led", "id": "led-green", "top": -60, "left": 200, "attrs": { "color": "green" } },
{ "type": "wokwi-resistor", "id": "r1", "top": 20, "left": 80, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r2", "top": 20, "left": 140, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r3", "top": 20, "left": 200, "attrs": { "value": "220" } }
],
"connections": [
[ "joystick:VCC", "uno:5V", "red", [] ],
[ "joystick:GND", "uno:GND", "black", [] ],
[ "joystick:HORIZ", "uno:A0", "blue", [] ],
[ "joystick:VERT", "uno:A1", "green", [] ],
[ "joystick:SEL", "uno:7", "orange", [] ],
[ "uno:2", "r1:1", "red", [] ],
[ "r1:2", "led-red:A", "red", [] ],
[ "led-red:C", "uno:GND", "black", [] ],
[ "uno:3", "r2:1", "yellow", [] ],
[ "r2:2", "led-yellow:A", "yellow", [] ],
[ "led-yellow:C", "uno:GND", "black", [] ],
[ "uno:4", "r3:1", "green", [] ],
[ "r3:2", "led-green:A", "green", [] ],
[ "led-green:C", "uno:GND", "black", [] ]
]
}
How to use: In Wokwi, click the diagram.json tab at the top of the editor, select all text, and paste. The circuit loads instantly with all connections ready.
Arduino Code — Complete Sketch
Paste this into the Wokwi code editor or Arduino IDE. No additional libraries needed — just the built-in Arduino functions.
const int PIN_RED = 2;
const int PIN_YELLOW = 3;
const int PIN_GREEN = 4;
const int PIN_HORIZ = A0;
const int PIN_VERT = A1;
const int PIN_SEL = 7;
void setup() {
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_YELLOW, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_SEL, INPUT_PULLUP); // joystick button
}
void loop() {
int x = analogRead(PIN_HORIZ); // 0-1023, center ~512
int y = analogRead(PIN_VERT); // 0-1023, center ~512
bool pressed = (digitalRead(PIN_SEL) == LOW);
// Push button -> all LEDs on
if (pressed) {
digitalWrite(PIN_RED, HIGH);
digitalWrite(PIN_YELLOW, HIGH);
digitalWrite(PIN_GREEN, HIGH);
return;
}
// Left/right -> Red or Green
digitalWrite(PIN_RED, x < 400); // joystick left
digitalWrite(PIN_GREEN, x > 600); // joystick right
// Up -> Yellow
digitalWrite(PIN_YELLOW, y < 400); // joystick up
}
Extend it: Add a Serial.begin(9600) in setup and Serial.println(x) in loop to watch live joystick values in the Serial Monitor — great for calibrating thresholds.
.png)
Comments
Post a Comment