Arduino Nano - Touch Sensor

This tutorial instructs you how to use the capacitive touch sensor with Arduino Nano. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Touch Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Touch Sensor

A capacitive touch sensor, also known as a touch button or touch switch, is a commonly used device to control other gadgets, such as a touchable lamp. It performs the same function as a button, but is often chosen over a button for modern devices in order to give them a more polished appearance.

The Touch Sensor Pinout

The touch sensor has three pins:

  • The GND pin must be connected to ground (0V).
  • The VCC pin must be connected to the voltage source (5V or 3.3v).
  • The SIGNAL pin is an output pin. It will be LOW when not touched and HIGH when touched. This pin should be connected to an Arduino Nano input pin.
touch sensor pinout

How It Works

  • When the sensor is not being touched, the SIGNAL pin of the sensor is at a LOW level.
  • Conversely, when the sensor is touched, the SIGNAL pin of the sensor is at a HIGH level.

Arduino Nano - Touch Sensor

The SIGNAL pin of the touch sensor is connected to an input pin on an Arduino Nano. By checking the status of an Arduino Nano input pin, we can determine if the touch sensor has been touched or not.

Wiring Diagram

The wiring diagram between Arduino Nano and Touch Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Touch Sensor

  • Initializes the Arduino Nano pin to digital input mode with the pinMode() function. For example, the code below sets pin 2 to input mode.
pinMode(2, INPUT_PULLUP);
  • Utilizes the digitalRead() function to read the state of the Arduino Nano pin.
int inputState = digitalRead(2);

※ NOTE THAT:

Two common scenarios exist:

  • The first: If the input is HIGH, take one action. If the input is LOW, perform the opposite action.
  • The second: If the input changes from LOW to HIGH (or vice versa), take an action.

Depending on the application, we can select one of them. For example, if we are using a touch sensor to control an LED:

  • If we want the LED to be ON when the sensor is touched and OFF when the sensor is NOT touched, we SHOULD use the first scenario.
  • If we want the LED to be toggle between ON and OFF each time we touch the sensor, we SHOULD use the second scenario.

Arduino Nano Code for Touch Sensor

We will be executing two sample codes:

  • Arduino Nano reads the value from the touch sensor and print it on the Serial Monitor.
  • Arduino Nano detects the touching or untouching events.

Arduino Nano reads the value from the touch sensor and print to the Serial Monitor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-touch-sensor */ #define TOUCH_SENSOR_PIN 2 // The Arduino Nano pin connected to the touch sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano pin as an input pinMode(TOUCH_SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(TOUCH_SENSOR_PIN); // print state to Serial Monitor Serial.println(state); }

Detailed Instructions

  • Copy the code and open it with Arduino IDE.
  • Click the Upload button on Arduino IDE to compile and upload the code to Arduino Nano.
  • Place your finger on the sensor and then take it out.
  • Check out the result on the Serial Monitor.
COM6
Send
0 0 0 1 1 1 1 1 1 0 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Nano detects the sensor is touched or released

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-touch-sensor */ #define TOUCH_SENSOR_PIN 2 // The Arduino Nano pin connected to the touch sensor int prev_state = LOW; // The previous state from the input pin int touch_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano pin as an input pinMode(TOUCH_SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: touch_state = digitalRead(TOUCH_SENSOR_PIN); if (prev_state == LOW && touch_state == HIGH) Serial.println("The sensor is touched"); else if (prev_state == HIGH && touch_state == LOW) Serial.println("The sensor is released"); // save the the last state prev_state = touch_state; }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to transfer the code to the Arduino Nano.
  • Touch your finger on the sensor.
  • Check the output on the Serial Monitor.
COM6
Send
The sensor is touched
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Take your finger away from the touch sensor.
  • Check the output on the Serial Monitor.
COM6
Send
The sensor is touched The sensor is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References