Arduino-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 by cycling through the traffic light LEDs (red, yellow, green) in response to touch sensor inputs. When the touch sensor is activated, the system transitions from one light to the next in the sequence: red, yellow, green, and then all off. This cycle repeats with each subsequent touch sensor activation.
Component List
Traffic Light
- Description: A traffic light module with three LEDs (Green, Yellow, Red).
- Pins: Green, Yellow, Red, GND
- Purpose: To visually represent the traffic light states controlled by the Arduino UNO.
Touch Sensor
- Description: A touch sensor module used to detect touch inputs.
- Pins: IO, VCC, GND
- Purpose: To provide a user interface for changing the state of the traffic light.
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
- Purpose: To control the traffic light LEDs and read inputs from the touch sensor.
Wiring Details
Traffic Light
- Green LED: Connected to Arduino UNO pin D4.
- Yellow LED: Connected to Arduino UNO pin D3.
- Red LED: Connected to Arduino UNO pin D2.
- GND: Connected to Arduino UNO GND.
Touch Sensor
- IO: Connected to Arduino UNO pin D7.
- VCC: Connected to Arduino UNO 5V.
- 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 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 touchPin = 7; // Pin connected to touch sensor IO
const int redLedPin = 2; // Pin connected to red LED
const int yellowLedPin = 3; // Pin connected to yellow LED
const int greenLedPin = 4; // Pin connected to green LED
int touchState = 0; // Current state of the touch sensor
int lastTouchState = 0; // Previous state of the touch sensor
int ledState = 0; // State of the LEDs (0: off, 1: red, 2: yellow, 3: green)
void setup() {
pinMode(touchPin, INPUT);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Initialize all LEDs to off
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
void loop() {
touchState = digitalRead(touchPin);
// Check if the touch sensor state has changed
if (touchState != lastTouchState) {
if (touchState == HIGH) {
// Cycle through LED states
ledState = (ledState + 1) % 4;
updateLeds();
}
delay(50); // Debounce delay
}
lastTouchState = touchState;
}
void updateLeds() {
// Turn off all LEDs
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
// Turn on the appropriate LED based on the current state
if (ledState == 1) {
digitalWrite(redLedPin, HIGH);
} else if (ledState == 2) {
digitalWrite(yellowLedPin, HIGH);
} else if (ledState == 3) {
digitalWrite(greenLedPin, HIGH);
}
}
The code is structured with a setup function that initializes the pins and a loop function that reads the touch sensor state and updates the LED states accordingly. The updateLeds function is responsible for turning on the appropriate LED based on the current state. The debounce delay is used to prevent false triggering of the touch sensor.