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

Arduino UNO Controlled Traffic Light System with Touch Sensor Interaction

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

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 touch sensor's activation toggles between the red, green, and yellow LEDs of a traffic light module. The cycle progresses through the following states: all LEDs off, red LED on, green LED on, yellow LED on, and then repeats.

Component List

Arduino UNO

Touch Sensor

Traffic Light Module

Wiring Details

Arduino UNO

Touch Sensor

Traffic Light Module

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 green LED turns on. On the next press, the yellow LED turns on. On the
 * next press, all LEDs turn off. The cycle then repeats.
 */

const int touchPin = 2; // Pin connected to the touch sensor
const int redPin = 3;   // Pin connected to the red LED
const int yellowPin = 4; // Pin connected to the yellow LED
const int greenPin = 5;  // Pin connected to the green LED

int touchState = 0; // Variable to store the touch sensor state
int ledState = 0;   // Variable to store the current LED state

void setup() {
  pinMode(touchPin, INPUT); // Set touch sensor pin as input
  pinMode(redPin, OUTPUT);  // Set red LED pin as output
  pinMode(yellowPin, OUTPUT); // Set yellow LED pin as output
  pinMode(greenPin, OUTPUT);  // Set green LED pin as output
  digitalWrite(redPin, LOW);  // Ensure red LED is off
  digitalWrite(yellowPin, LOW); // Ensure yellow LED is off
  digitalWrite(greenPin, LOW);  // Ensure green LED is off
}

void loop() {
  touchState = digitalRead(touchPin); // Read the touch sensor state

  if (touchState == HIGH) { // If touch sensor is pressed
    delay(200); // Debounce delay
    ledState = (ledState + 1) % 4; // Cycle through LED states
    updateLEDs(); // Update LEDs based on the current state
    while (digitalRead(touchPin) == HIGH); // Wait for touch release
  }
}

void updateLEDs() {
  switch (ledState) {
    case 0: // All LEDs off
      digitalWrite(redPin, LOW);
      digitalWrite(yellowPin, LOW);
      digitalWrite(greenPin, LOW);
      break;
    case 1: // Red LED on
      digitalWrite(redPin, HIGH);
      digitalWrite(yellowPin, LOW);
      digitalWrite(greenPin, LOW);
      break;
    case 2: // Green LED on
      digitalWrite(redPin, LOW);
      digitalWrite(yellowPin, LOW);
      digitalWrite(greenPin, HIGH);
      break;
    case 3: // Yellow LED on
      digitalWrite(redPin, LOW);
      digitalWrite(yellowPin, HIGH);
      digitalWrite(greenPin, LOW);
      break;
  }
}

The code is structured to initialize the pins connected to the touch sensor and LEDs in the setup() function. The loop() function continuously checks the state of the touch sensor and updates the LED states accordingly. The updateLEDs() function controls the on/off state of each LED based on the current ledState. The cycle of LED states is managed by incrementing the ledState variable and using the modulo operator to wrap around to the beginning of the cycle.