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
- Description: A microcontroller board based on the ATmega328P.
- Purpose: Acts as the central processing unit for the circuit, reading the touch sensor input and controlling the traffic light LEDs.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Traffic Light
- Description: A module with three LEDs (Green, Yellow, Red) representing a traffic light.
- Purpose: Displays the traffic light status as controlled by the Arduino UNO.
- Pins: Green, Yellow, Red, GND
Touch Sensor
- Description: A capacitive touch sensor module.
- Purpose: Serves as the user input to change the state of the traffic light.
- Pins: IO, VCC, GND
Wiring Details
Arduino UNO
- 5V connected to the VCC pin of the Touch Sensor.
- GND connected to the GND pins of the Traffic Light and Touch Sensor.
- D2 connected to the Red pin of the Traffic Light.
- D3 connected to the Yellow pin of the Traffic Light.
- D4 connected to the Green pin of the Traffic Light.
- D7 connected to the IO pin of the Touch Sensor.
Traffic Light
- Green connected to D4 on the Arduino UNO.
- Yellow connected to D3 on the Arduino UNO.
- Red connected to D2 on the Arduino UNO.
- GND connected to GND on the Arduino UNO.
Touch Sensor
- IO connected to D7 on the Arduino UNO.
- VCC connected to 5V on the Arduino UNO.
- GND connected to GND on the Arduino UNO.
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.
