Arduino Mega - Touch Sensor
A touch sensor, also called a touch button or touch switch, is often used to control devices like touch lamps. It works like a regular button. Many new devices use touch sensors instead of traditional buttons because they make the product look nicer and more modern.
This guide shows how to use a touch sensor with the Arduino Mega board.

Hardware Preparation
| 1 | × | Arduino Mega | |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | |
| 1 | × | Touch Sensor | |
| 1 | × | Jumper Wires |
Or you can buy the following 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 3 pins:
- GND pin: connect to ground (0V)
- VCC pin: connect to power supply (5V or 3.3V)
- SIGNAL pin: it outputs LOW when not touched, HIGH when touched. Connect this pin to an input pin on the Arduino Mega.

How It Works
- When the sensor is not touched, the signal pin is low.
- When the sensor is touched, the signal pin is high.
Arduino Mega - Touch Sensor
The touch sensor's signal pin is connected to an input pin on the Arduino Mega.
You can tell if the touch sensor is touched by reading an input pin on the Arduino Mega.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
How To Program For Touch Sensor
- Sets up a pin on the Arduino Mega as a digital input using the pinSerial() function. For example, pin 7.
- It uses the digitalRead() function to check the state of the Arduino Mega pin.
※ NOTE THAT:
There are two common cases:
- Case 1: If the input is ON, do one thing. If the input is OFF, do the opposite.
- Case 2: If the input changes from OFF to ON (or ON to OFF), do something.
We choose the right case based on what we need. For example, using a touch sensor to control an LED:
- To turn the LED ON when the sensor is touched and OFF when it is not touched, Case 1 is suitable.
- To switch the LED ON and OFF each time the sensor is touched, Case 2 is suitable.
Touch Sensor - Arduino Mega Code
Reads the value from the touch sensor and print to the Serial Monitor
Detailed Instructions
Do these steps one at a time.
- Connect the touch sensor to the Arduino Mega as shown in the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose Arduino Mega as the board and select the right COM port.
- Copy the code above and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
- Put your finger on the sensor, then remove it.
- Check the result in the Serial Monitor.
Detects the sensor is touched or released
Detailed Instructions
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
- Put your finger on the sensor and keep it there.
- See the results in the Serial Monitor.
- Take your finger off the sensor.
- Check the result in the Serial Monitor.