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

Arduino UNO Controlled Traffic Light with Touch Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Traffic Light with Touch Sensor

Traffic Light Control Circuit Documentation

Summary

This circuit is designed to control a traffic light system using an Arduino UNO microcontroller and a touch sensor. The system operates in a cycle where the state of the traffic light changes upon each touch sensor activation. The cycle progresses from red to yellow to green and then turns off all lights before repeating. The Arduino UNO reads the state of the touch sensor and controls the traffic light LEDs accordingly.

Component List

Arduino UNO

Traffic Light

Touch Sensor

Wiring Details

Arduino UNO

Traffic Light

Touch Sensor

Documented Code

/*
 * This Arduino sketch controls a traffic light system using a touch sensor.
 * When the touch sensor is pressed, the red LED turns on. On the next press,
 * the yellow LED turns on. On the next press, the green LED turns on. On the
 * next press, all LEDs turn off. This cycle repeats with each subsequent press.
 */

const int redLEDPin = 2;    // Pin connected to the red LED of the traffic light
const int yellowLEDPin = 3; // Pin connected to the yellow LED of the traffic light
const int greenLEDPin = 4;  // Pin connected to the green LED of the traffic light
const int touchSensorPin = 7;// Pin connected to the touch sensor output

int currentState = 0;        // Variable to track the current state of the traffic lights
int lastTouchState = LOW;    // Variable to track the last state of the touch sensor

void setup() {
  pinMode(redLEDPin, OUTPUT);    // Set the red LED pin as an output
  pinMode(yellowLEDPin, OUTPUT); // Set the yellow LED pin as an output
  pinMode(greenLEDPin, OUTPUT);  // Set the green LED pin as an output
  pinMode(touchSensorPin, INPUT); // Set the touch sensor pin as an input
}

void loop() {
  int touchState = digitalRead(touchSensorPin); // Read the state of the touch sensor

  // If the touch sensor is pressed (and was not pressed before)
  if (touchState == HIGH && lastTouchState == LOW) {
    currentState = (currentState + 1) % 4; // Move to the next state
    updateLEDs();                           // Update the traffic light LEDs
    delay(200);                             // Debounce delay
  }

  lastTouchState = touchState; // Update the last touch sensor state
}

void updateLEDs() {
  // Set the LEDs according to the current state
  digitalWrite(redLEDPin, currentState == 1 ? HIGH : LOW);
  digitalWrite(yellowLEDPin, currentState == 2 ? HIGH : LOW);
  digitalWrite(greenLEDPin, currentState == 3 ? HIGH : LOW);
}

This code is saved in a file named sketch.ino and is intended to be uploaded to the Arduino UNO microcontroller. The setup() function initializes the pins, and the loop() function continuously checks the touch sensor state to cycle through the traffic light states. The updateLEDs() function sets the appropriate LED states based on the current state of the system.