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.

Arduino UNO R4 touch sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Touch Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.
Touch Sensor Pinout

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

The wiring diagram between Arduino UNO R4 Touch Sensor

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.
pinMode(7, INPUT_PULLUP);
  • Uses the digitalRead() function to check the condition of the Arduino UNO R4 pin.
int inputState = digitalRead(7);

※ 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

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-touch-sensor */ #define SENSOR_PIN 7 // The Arduino UNO R4 pin connected to the touch sensor's SIGNAL pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); // print state to Serial Monitor Serial.println(state); }

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.
COM6
Send
0 0 1 1 1 1 1 0 0 0 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Detects the sensor is touched or released

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-touch-sensor */ #define SENSOR_PIN 7 // The Arduino UNO R4 pin connected to the touch sensor's SIGNAL pin int touch_state; // the current reading from the input pin int prev_touch_state = LOW; // the previous state from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: touch_state = digitalRead(SENSOR_PIN); if(prev_touch_state == LOW && touch_state == HIGH) Serial.println("The sensor is touched"); else if(prev_touch_state == HIGH && touch_state == LOW) Serial.println("The sensor is is released"); // save the the last state prev_touch_state = touch_state; }

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.
COM6
Send
The sensor is touched
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Lift your finger off the sensor.
  • Check the result on the Serial Monitor.
COM6
Send
The sensor is touched The sensor is is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!