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 40: 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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Touch Sensor Module

-

Traffic Light Module

-

Breadboard

BUY

Wiring

../_images/Lesson_40_Touch_toggle_light_esp32_bb.png

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.

  1. Define pins and initial values

    // Define pins for touch sensor and LEDs
    const int touchSensorPin = 14;  // touch sensor pin
    const int rledPin = 27;         // red LED pin
    const int yledPin = 26;         // yellow LED pin
    const int gledPin = 25;         // green LED pin
    
    int lastTouchState;     // the previous state of touch sensor
    int currentTouchState;  // the current state of touch sensor
    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.

  2. setup() function

    void setup() {
      Serial.begin(9600);              // initialize serial
      pinMode(touchSensorPin, INPUT);  // configure touch sensor pin as input
    
      // set LED pins as outputs
      pinMode(rledPin, OUTPUT);
      pinMode(yledPin, OUTPUT);
      pinMode(gledPin, OUTPUT);
    
      currentTouchState = digitalRead(touchSensorPin);
    }
    

    This function configures the initial setup for the Arduino, defining input and output modes and starting serial communication for debugging.

  3. loop() function

    void loop() {
      lastTouchState = currentTouchState;               // save the last state
      currentTouchState = digitalRead(touchSensorPin);  // read new state
    
      // check if the touch sensor was just touched
      if (lastTouchState == LOW && currentTouchState == HIGH) {
        Serial.println("The sensor is touched");
    
        turnAllLEDsOff();  // Turn off all LEDs
    
        // switch on 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.

  4. Turn off LEDs function

    // function to turn off all LEDs
    void turnAllLEDsOff() {
      digitalWrite(rledPin, LOW);
      digitalWrite(yledPin, LOW);
      digitalWrite(gledPin, LOW);
    }
    

    This auxiliary function turns off all LEDs, aiding in the cycling process.