Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Traffic Light with Joystick Interface

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Traffic Light with Joystick Interface

Traffic Light Control Circuit Documentation

Summary

This circuit is designed to control a traffic light using an Arduino UNO microcontroller and a joystick module. The traffic light has three LEDs (Red, Yellow, Green) that are controlled by the joystick's position. Moving the joystick up turns on the red LED, left turns on the yellow LED, right turns on the green LED, and down turns on all LEDs. The joystick also has a built-in switch that is read by the Arduino but not used in the current code. The circuit is powered by the Arduino's 5V output, and the ground is shared across all components.

Component List

Traffic Light

Arduino UNO

Joystick Module

Wiring Details

Traffic Light

Arduino UNO

Joystick Module

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.
 */

// Pin definitions
const int redLEDPin = 2;
const int yellowLEDPin = 3;
const int greenLEDPin = 4;
const int joystickVRXPin = A0;
const int joystickVRYPin = A1;
const int joystickSWPin = 7;

void setup() {
  // Initialize LED pins as outputs
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  // Initialize joystick switch pin as input
  pinMode(joystickSWPin, INPUT);
  // Start serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Read joystick values
  int vrxValue = analogRead(joystickVRXPin);
  int vryValue = analogRead(joystickVRYPin);
  int swValue = digitalRead(joystickSWPin);

  // Debugging output
  Serial.print("VRX: ");
  Serial.print(vrxValue);
  Serial.print(" VRY: ");
  Serial.print(vryValue);
  Serial.print(" SW: ");
  Serial.println(swValue);

  // Determine LED states based on joystick position
  if (vryValue < 300) { // Joystick moved up
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  } else if (vrxValue < 300) { // Joystick moved left
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(greenLEDPin, LOW);
  } else if (vrxValue > 700) { // Joystick moved right
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, HIGH);
  } else if (vryValue > 700) { // Joystick moved down
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(greenLEDPin, HIGH);
  } else { // Joystick in neutral position
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  }

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

The code is written for the Arduino UNO and is responsible for reading the joystick's analog and digital signals to control the traffic light LEDs. The joystick's vertical and horizontal movements are mapped to specific LEDs, and the switch is read but not used in the current functionality. Serial communication is established for debugging purposes, allowing the user to monitor the joystick's values.