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 22: Touch Sensor Module
In this lesson, you will learn how to integrate a touch sensor with an Arduino Uno. We’ll focus on reading inputs from the touch sensor connected to the Arduino and how these inputs affect the program’s flow. You’ll discover how to use conditional statements to detect touch events and respond with appropriate actions and messages. This project is excellent for beginners, providing a clear understanding of working with digital inputs and basic Arduino programming concepts.
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
Setting up the necessary variables. We start by defining the pin number where the touch sensor is connected.
const int sensorPin = 7;
Initialization in the
setup()function. Here, we specify that the sensor pin will be used for input, the built-in LED will be used for output, and we start the serial communication to allow messages to be sent to the serial monitor.void setup() { pinMode(sensorPin, INPUT); pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); }
Continuously, the Arduino checks if the touch sensor is activated. If touched, it turns on the LED and sends a “Touch detected!” message. If not touched, it turns off the LED and sends a “No touch detected…” message. A delay is introduced to prevent the sensor from being read too quickly.
void loop() { if (digitalRead(sensorPin) == 1) { digitalWrite(LED_BUILTIN, HIGH); Serial.println("Touch detected!"); } else { digitalWrite(LED_BUILTIN, LOW); Serial.println("No touch detected..."); } delay(100); }