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

Arduino UNO Controlled Traffic Light with Joystick Module

View and Edit Project In Cirkit DesignerImage 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. When the joystick is moved up, the red LED turns on. Moving the joystick left turns on the yellow LED, and moving it right turns on the green LED. Pushing the joystick down will turn on all LEDs. The joystick also has a built-in switch that is read by the Arduino but not used in the current code.

Component List

Arduino UNO

Joystick Module

Traffic Light

Wiring Details

Arduino UNO

Joystick Module

Traffic Light

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 joystick direction
  if (vryValue < 300) { // Joystick moved up
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  } else if (vryValue > 700) { // Joystick moved down
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(greenLEDPin, HIGH);
  } 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 { // Joystick in neutral position
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  }

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

This code is saved in a file named sketch.ino. It initializes the pins connected to the traffic light LEDs and the joystick, reads the joystick's analog values to determine its position, and sets the LEDs accordingly. Serial communication is also started for debugging purposes, allowing the user to monitor the joystick's analog values and switch state.