Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
Lesson 42: Touch toggle lightο
This project is a simple implementation of a traffic light control system utilizing a touch sensor and a traffic light LED module. Activating the touch sensor initiates a sequence where LEDs illuminate in the following order: Red -> Yellow -> Green.
Required Componentsο
In this project, we need the following components.
Itβs definitely convenient to buy a whole kit, hereβs the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
- |
|
- |
|
Wiringο
Codeο
Code Analysisο
The operation of this project is straightforward: a touch detection on the sensor triggers the illumination of the next LED in the sequence (Red -> Yellow -> Green), controlled by the currentLED variable.
Define pins and initial values
const int touchSensorPin = 2; // Touch sensor pin const int rledPin = 7; // Red LED pin const int yledPin = 8; // Yellow LED pin const int gledPin = 9; // Green LED pin int lastTouchState; // Previous touch sensor state int currentTouchState; // Current touch sensor state int currentLED = 0; // Current LED: 0->Red, 1->Yellow, 2->Green
These lines establish the pin connections for the Arduino board components and initialize the touch sensor and LED states.
setup() function
void setup() { Serial.begin(9600); // Initialize serial communication pinMode(touchSensorPin, INPUT); // Set touch sensor pin as input // Configure LED pins as outputs pinMode(rledPin, OUTPUT); pinMode(yledPin, OUTPUT); pinMode(gledPin, OUTPUT); currentTouchState = digitalRead(touchSensorPin); // Read initial touch state }
This function configures the initial setup for the Arduino, defining input and output modes and starting serial communication for debugging.
loop() function
void loop() { lastTouchState = currentTouchState; // Store the last state currentTouchState = digitalRead(touchSensorPin); // Read new touch state if (lastTouchState == LOW && currentTouchState == HIGH) { // Detect touch Serial.println("Sensor touched"); turnAllLEDsOff(); // Turn off all LEDs // Activate the next LED in sequence switch (currentLED) { case 0: digitalWrite(rledPin, HIGH); currentLED = 1; break; case 1: digitalWrite(yledPin, HIGH); currentLED = 2; break; case 2: digitalWrite(gledPin, HIGH); currentLED = 0; break; } } }
The loop continuously monitors the touch sensor, cycling through the LEDs when a touch is detected, ensuring only one LED is on at any given time.
Turn off LEDs function
void turnAllLEDsOff() { // Set all LED pins to LOW, turning them off digitalWrite(rledPin, LOW); digitalWrite(yledPin, LOW); digitalWrite(gledPin, LOW); }
This auxiliary function turns off all LEDs, aiding in the cycling process.