Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Controlled Traffic Light with Joystick Module

Image of Arduino UNO Controlled Traffic Light with Joystick Module

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. Moving the joystick up turns on the red LED, moving it left turns on the yellow LED, and moving it right turns on the green LED. Moving the joystick down turns on all LEDs. The joystick also has a switch (SW) 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 controller for the circuit, reading inputs from the joystick and driving the traffic light LEDs.

Traffic Light

  • Description: A simple module with three LEDs representing a traffic light.
  • Purpose: Displays the traffic light status based on the joystick input.

Joystick Module

  • Description: An input device with two analog axes (VRX and VRY) and a pushbutton switch (SW).
  • Purpose: Provides user input to control the traffic light LEDs.

Wiring Details

Arduino UNO

  • 5V - Provides power to the joystick module.
  • GND - Common ground for the joystick module and traffic light.
  • A0 - Receives the X-axis analog input from the joystick (VRX).
  • A1 - Receives the Y-axis analog input from the joystick (VRY).
  • D2 - Connected to the red LED of the traffic light.
  • D3 - Connected to the yellow LED of the traffic light.
  • D4 - Connected to the green LED of the traffic light.
  • D7 - Reads the switch state from the joystick (SW).

Traffic Light

  • Red - Connected to digital pin D2 on the Arduino UNO.
  • Yellow - Connected to digital pin D3 on the Arduino UNO.
  • Green - Connected to digital pin D4 on the Arduino UNO.
  • GND - Connected to the common ground on the Arduino UNO.

Joystick Module

  • VCC - Powered by the 5V output from the Arduino UNO.
  • GND - Connected to the common ground on the Arduino UNO.
  • VRX - Connected to analog pin A0 on the Arduino UNO.
  • VRY - Connected to analog pin A1 on the Arduino UNO.
  • SW - Connected to digital pin D7 on the Arduino UNO (not used in the current code).

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;    // Red LED connected to digital pin 2
const int yellowLED = 3; // Yellow LED connected to digital pin 3
const int greenLED = 4;  // Green LED connected to digital pin 4
const int vrxPin = A0;   // Joystick VRX connected to analog pin A0
const int vryPin = A1;   // Joystick VRY connected to analog pin A1
const int swPin = 7;     // Joystick SW connected to digital pin 7

void setup() {
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(swPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int vrxValue = analogRead(vrxPin); // Read joystick X-axis
  int vryValue = analogRead(vryPin); // Read joystick Y-axis
  int swValue = digitalRead(swPin);  // Read joystick button

  // Center values for the joystick
  int centerX = 512;
  int centerY = 512;
  int threshold = 100; // Threshold to detect significant movement

  // Turn off all LEDs initially
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, LOW);

  if (vryValue < centerY - threshold) { // Joystick moved up
    digitalWrite(redLED, HIGH);
  } else if (vrxValue < centerX - threshold) { // Joystick moved left
    digitalWrite(yellowLED, HIGH);
  } else if (vrxValue > centerX + threshold) { // Joystick moved right
    digitalWrite(greenLED, HIGH);
  } else if (vryValue > centerY + threshold) { // Joystick moved down
    digitalWrite(redLED, HIGH);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, HIGH);
  }

  delay(100); // Small delay to avoid flickering
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the LEDs as outputs and the joystick switch as an input with an internal pull-up resistor. The loop function reads the joystick's analog values and turns on the corresponding LEDs based on the joystick's position. The switch state is read but not used in the current logic.