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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Touch Sensor Module

-

Traffic Light Module

-

Breadboard

BUY

Wiring

../_images/Lesson_42_Touch_toggle_light_uno_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

    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.

  2. 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.

  3. 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.

  4. 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.