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

This circuit is designed to control a traffic light using an Arduino UNO and a joystick module. The joystick's position determines which LED on the traffic light is illuminated. Moving the joystick up activates the red LED, left for the yellow LED, right for the green LED, and down for all LEDs. The Arduino UNO reads the joystick's analog values and drives the corresponding LEDs on the traffic light.

Component List

Arduino UNO

Traffic Light

Joystick Module

Wiring Details

Arduino UNO

Traffic Light

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

const int redLEDPin = 2;    // Red LED connected to D2
const int yellowLEDPin = 3; // Yellow LED connected to D3
const int greenLEDPin = 4;  // Green LED connected to D4
const int vrxPin = A0;      // Joystick VRX connected to A0
const int vryPin = A1;      // Joystick VRY connected to A1

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(vrxPin, INPUT);
  pinMode(vryPin, INPUT);
}

void loop() {
  int vrxValue = analogRead(vrxPin);
  int vryValue = analogRead(vryPin);

  // Center values for the joystick
  const int centerX = 512;
  const int centerY = 512;
  const int threshold = 100;

  // Reset all LEDs
  digitalWrite(redLEDPin, LOW);
  digitalWrite(yellowLEDPin, LOW);
  digitalWrite(greenLEDPin, LOW);

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

Filename: sketch.ino

This code is responsible for reading the analog values from the joystick and controlling the state of the traffic light LEDs accordingly. The setup() function initializes the LED pins as outputs and the joystick pins as inputs. The loop() function continuously reads the joystick values and turns on the appropriate LEDs based on the joystick's position.