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

Arduino UNO Controlled Traffic Light with Joystick Interface

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

Circuit Documentation

Summary

This circuit is designed to control a traffic light using an Arduino UNO and a KY-023 Dual Axis Joystick Module. The traffic light has three LEDs (Red, Yellow, Green) that are controlled by the joystick's position. When the joystick is moved in different directions, the corresponding LED lights up on the traffic light. The joystick also has a built-in switch that is used in this circuit.

Component List

Arduino UNO

Traffic Light

KY-023 Dual Axis Joystick Module

Wiring Details

Arduino UNO

Traffic Light

KY-023 Dual Axis Joystick Module

Documented Code

/*
 * This Arduino sketch controls a traffic light using a joystick.
 * 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;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int joystickXPin = A0;
const int joystickYPin = A1;
const int joystickButtonPin = 7;

void setup() {
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(joystickButtonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int xValue = analogRead(joystickXPin);
  int yValue = analogRead(joystickYPin);
  bool buttonPressed = digitalRead(joystickButtonPin) == LOW;

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

  // Turn off all LEDs initially
  digitalWrite(redLedPin, LOW);
  digitalWrite(yellowLedPin, LOW);
  digitalWrite(greenLedPin, LOW);

  if (yValue < centerY - threshold) { // Joystick moved up
    digitalWrite(redLedPin, HIGH);
  } else if (xValue < centerX - threshold) { // Joystick moved left
    digitalWrite(yellowLedPin, HIGH);
  } else if (xValue > centerX + threshold) { // Joystick moved right
    digitalWrite(greenLedPin, HIGH);
  } else if (yValue > centerY + threshold) { // Joystick moved down
    digitalWrite(redLedPin, HIGH);
    digitalWrite(yellowLedPin, HIGH);
    digitalWrite(greenLedPin, HIGH);
  }

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

This code is written for the Arduino UNO and is responsible for reading the joystick's analog values to determine its position. Depending on the position, it will light up the corresponding LED on the traffic light. The joystick's switch is also monitored, and when pressed, all LEDs will light up. The code includes setup for the pins and a loop that continuously checks the joystick's position and updates the LED states accordingly.