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

Arduino-Controlled Traffic Light System with Joystick Interaction

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

Traffic Light Control Circuit Documentation

Summary

This circuit is designed to control a traffic light system using an Arduino UNO microcontroller and a joystick module. The traffic light system consists of three LEDs (green, yellow, and red) that represent the traffic signals. The joystick module is used to change the state of the traffic lights: moving the joystick up turns on the red LED, moving it left turns on the yellow LED, moving it right turns on the green LED, and moving it down turns on all LEDs. The circuit is powered by the Arduino UNO, which also processes the input from the joystick to control the LEDs.

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 system with three LEDs: 
 * green, yellow, and red. The LEDs are controlled by 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 redPin = 2;
const int yellowPin = 3;
const int greenPin = 4;
const int vrxPin = A0;
const int vryPin = A1;
const int swPin = 7;

void setup() {
  // Initialize the digital pins as outputs
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  // Initialize the joystick pins
  pinMode(vrxPin, INPUT);
  pinMode(vryPin, INPUT);
  pinMode(swPin, INPUT_PULLUP);
}

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

  // Joystick moved up
  if (vryValue < 300) {
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
  }
  // Joystick moved down
  else if (vryValue > 700) {
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(greenPin, HIGH);
  }
  // Joystick moved left
  else if (vrxValue < 300) {
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(greenPin, LOW);
  }
  // Joystick moved right
  else if (vrxValue > 700) {
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, HIGH);
  }
  // Joystick in neutral position
  else {
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
  }
}

This code is responsible for reading the analog values from the joystick's VRX and VRY pins to determine the direction of the joystick's movement. It then sets the appropriate traffic light LED to on or off based on the joystick's position. The SW pin is configured with an internal pull-up resistor and can be used to detect button presses on the joystick, although this functionality is not used in the current sketch.