Arduino UNO Controlled Traffic Light System with Touch Sensor Interaction
View and Edit Project In Cirkit Designer
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
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
Touch Sensor
- Description: A capacitive touch sensor module.
- Pins: IO, VCC, GND.
Traffic Light Module
- Description: A module with three LEDs (Red, Yellow, Green) representing a traffic light.
- Pins: Green, Yellow, Red, GND.
Wiring Details
Arduino UNO
- 5V connected to Touch Sensor VCC.
- GND connected to Touch Sensor GND and Traffic Light GND.
- D2 connected to Touch Sensor IO.
- D3 connected to Traffic Light Red.
- D4 connected to Traffic Light Yellow.
- D5 connected to Traffic Light Green.
Touch Sensor
- IO connected to Arduino UNO D2.
- VCC connected to Arduino UNO 5V.
- GND connected to Arduino UNO GND.
Traffic Light Module
- Green connected to Arduino UNO D5.
- Yellow connected to Arduino UNO D4.
- Red connected to Arduino UNO D3.
- GND connected to Arduino UNO GND.
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.