Arduino UNO R4 - Touch Sensor
A touch sensor, also known as a touch button or touch switch, is commonly used to operate devices like touchable lamps. It works just like a regular button. Many new devices use touch sensors instead of traditional buttons because they help make the product look sleeker.
In this guide, we will learn how to use the touch sensor with Arduino UNO R4.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Touch Sensor
Pinout
The touch sensor has three pins:
- GND pin: connect to GND (0V)
- VCC pin: connect to VCC (5V or 3.3V)
- SIGNAL pin: it outputs: LOW if untouched, HIGH if touched. Connect this pin to the input pin of Arduino UNO R4.
How It Works
- If the sensor is not being touched, the signal pin of the sensor is LOW.
- If the sensor is being touched, the signal pin of the sensor is HIGH.
Arduino UNO R4 - Touch Sensor
The SIGNAL pin of the touch sensor is attached to an input pin on the Arduino UNO R4.
You can find out if the touch sensor is being touched by checking the state of a pin on the Arduino UNO R4 that is set up as an input pin.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
How To Program For Touch Sensor
- Sets up the Arduino UNO R4 pin as a digital input, using the pinSerial() function. For instance, for pin 7.
- Uses the digitalRead() function to check the condition of the Arduino UNO R4 pin.
※ NOTE THAT:
There are two common scenarios:
- First scenario: When the input is HIGH, perform an action. When the input is LOW, perform the opposite action.
- Second scenario: When the input changes from LOW to HIGH (or HIGH to LOW), perform an action.
We select the appropriate scenario based on what we need. For example, when using a touch sensor to control an LED:
- To turn the LED ON when the sensor is touched and OFF when not touched, the first scenario is suitable.
- To switch the LED ON and OFF each time the sensor is touched, the second scenario is suitable.
Touch Sensor - Arduino UNO R4 Code
Reads the value from the touch sensor and print to the Serial Monitor
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the touch sensor to the Arduino Uno R4 according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the code above and open it with Arduino IDE
- Click the Upload button in Arduino IDE to send the code to Arduino UNO R4
- Place your finger on the sensor and then remove it.
- Check the outcome on the Serial Monitor.
Detects the sensor is touched or released
Detailed Instructions
- Copy the code and open it in Arduino IDE
- Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
- Place your finger on the sensor and hold it there.
- Check the results on the Serial Monitor.
- Lift your finger off the sensor.
- Check the result on the Serial Monitor.