Skip to Content

Interfacing Arduino To A Touch Sensor

Interfacing Arduino To A Touch Sensor

This tutorial will teach you how to interface a touch button to the Arduino. You will get the step-by-step connection guide, Arduino code, and the touch sensor’s basic working principle.

Touch sensors help improve the user experience of products. You can make products 100% waterproof, replace existing mechanical switches, and provide longer button life. 

Since there are no moving parts in a touch button, there is no problem of debouncing and aging.

The touch option also lets you easily overlay the backlight to the buttons. 

We will start with the basics of a touch function.

We will see a few examples of touch controller ICs used most often. In the following sections, I will present you with a step-by-step connection guide and example projects with code, and I will also answer a few FAQs.

Let’s get started!

Components Needed To Build Arduino And A Touch Button

Hardware Components 

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

Basics of A Single-Button Touch Sensor

There are several types of touch sensors in the market, as shown in the below image.

Basics of A Single-Button Touch Sensor

The most common touch controller used in these touch sensors is TTP223. The TTP223 chip has the following features:

  • Wide operating voltage of 2 V to 5.5 V
  • Low power mode current → 1.5 uA
  • Adjustable touch sensitivity
  • Configurable outputs – Configure as Active High or Active Low
  • Continuous Auto calibration
6-pin IC on the board

You can see the small 6-pin IC on the boards. The Pinout of the TTP223 IC is given below.

Pin NumberPin NameI/O TypePin Description
1QOutputCMOS output pin. Connect this Pin to the Arduino
2VSSPowerNegative power supply / Ground
3IInputInput sensor port
4AHLBInputOutput Active High or low selection
5VDDPowerPositive Power Supply
6TOGInputOutput type option selection (toggle mode/Direct Mode)

The fundamental hardware component needed to design a touch button on a PCB is shown below.

Basics of Touch Sensing

Capacitive touch sensing is easy to use and understand. In this section, we will see the basic building blocks needed to complete the conduction of a touch sensor. 

In the below image, you can see an exploded view of a single touch button sensor.

The overlay comes on the top. It is a separation between the PCB and the user.

The thickness of the overlay affects the touch performance. 

You should choose a thinner overlay with a higher dielectric constant for better touch sensitivity.

The overlay chosen should be a non-conductor.

The sensor pad is the crucial element which is a solid copper pad. You can create it in any shape, but the circular one is the most recommended due to its symmetry and absence of a sharp end. 

The diameter of the touch sensor should be at least the diameter of the finger. In practical cases, most of the touch buttons I have seen are between 8 mm to 12 mm. 

If the buttons are smaller, the touch sensitivity reduces. If the buttons re too big, they become very sensitive.

The button might get triggered even if you just bring your finger close to the button (without actually touching it).

Hence, you should find a good tradeoff between the button size and the thickness of the overlay to achieve the best touch performance. 

The cross-sectional view looks like the below image.

The meshed pattern is the ground. The electric field from the sensor pad emerges from the sensor pad and terminates on the ground.

sensor pad and terminates on the ground

You provide an alternate path for the field lines when you touch the button. This, in turn, reflects an increase in capacitance. The touch controller senses this change in the capacitance. 

The below image is from one of the touch sensors from Infineon (formerly Cypress). The raw counts directly represent the capacitance of the touch button sensed by the touch controller.

You can see a drastic increase in the raw counts when the user touches the button. 

The single-button touch IC automatically calibrates the threshold values so that it can filter out the unwanted noise in the raw counts.

single-button touch IC

Note: The electric field lines couple very tightly if the ground pattern is solid. Hence, you will not get good touch sensitivity when you touch the button. Therefore, the ground is created in a mesh pattern.

Advantages of Touch Button Sensors

The touch buttons offer a few advantages over traditional push buttons

  • No mechanical movements (more extended life and reliable connections)
  • Integration of haptics, the backlight is easy in case of touch controllers
  • Easy to clean the surfaces as there is no slots or holes as in mechanical keys
  • Improved user experience
  • Aesthetic look

Step-By-Step Instructions To Connect A Touch Sensor To An Arduino

In this section, I will give you step-by-step instructions to complete the needed connection between the Arduino and the Touch sensor board.

In this example, we will use a touch sensor with 4 Pins.

touch sensor with 4 Pins

One of the four pins is an NC (no connect). You may have to adapt the connections based on your touch sensor.

It will be easy to complete the connection if you follow the steps below. 

Let’s get started!

How To Connect The Touch Sensor To The Arduino UNO?

Below is the step-by-step connection guide to complete the Arduino and the touch sensor. 

Step 1: Make the Ground connection

Make the Ground connection

Pin 1 of the touch sensor goes to the GND pin on the Arduino. You can choose the closest GND pin available to complete the connection.

It is an excellent practice to start the connections with the GND connection first.

Step 2: Connect the VCC pin

Connect the VCC pin

Connect the 5 V pin of the touch sensor to the Arduino UNO. The power consumed by the touch sensor is ver less.

Step 3: Connect the Touch Signal Pin

Connect the Touch Signal Pin

Connect Pin 2 of the Arduino UNO to the SIG pin of the touch sensor. The final connection should like similar to the image below.

You can always change the UNO pin to which you have to connect the SIG pin of the touch controller. 

Make sure that you also update the Arduino code to reflect the new connections

LED to one of the Arduino GPIO

Further, you can also add an LED to one of the Arduino GPIO. You can drive the LED high for the duration of the touch. Another suggestion is to add a buzzer.

This gives the user lovely audio feedback when they touch the button. 

Touch sensors won’t give haptic feedback on their won when compared to a mechanical switch.

You can also opt for a haptic vibration feedback motor to compensate for that.

Arduino Code Example For The Touch sensor

In this section, we will see a simple example of a touch sensor project. You need only one Arduino GPIO pin to read the touch button status. I have used Arduino Pin 2 in this example.

To use other pins, you must update the line below in the Arduino sketch.

const int touch_pin = 2;

For example, if you connect the SIG pin of the touch sensor to PIN5 of the Arduino UNO, you should change the line to

const int touch_pin = 5;

The Complete Arduino Code

Single Touch Button Project 

In this project, you read the status of the touch button. When the user touches the button, you will print a message on the terminal.

Open the Arduino IDE and click on the “File” option. Under the file options, select “New.”

//Change the pin here according to your connection
const int touch_pin = 2;
 
void setup()
{
 /* make sure that, when you open the serial terminal, 
   you set the baud rate to match the baud rate you define in the following line. 
   You can choose 115200, 9600, or any standard baud rate. 
   Make sure both baud rate matches */
 
  Serial.begin(14400);
 
  //define the touch pin as an input pin
  pinMode(touch_pin, INPUT);
}
 
//read the button status and display the message once touch is detected
void loop()
{
  if (digitalRead(touch_pin) == HIGH)
  {
    Serial.println("Touch detected!");
    delay(100);
  }
}

Multiple Touch button project

In this example, you extend the previous project into four touch buttons. You can use them to create a simple piano application, keyboard shortcuts for your PC, etc. 

The below application rads the touch status from 4 touch buttons and creates keyboard shortcuts. You can easily map the Arduino UNP pins to the touch buttons. 

You can refer to the Arduino documentation to learn more about the keyboard modifiers. 

The below code demonstrates the usage of touch buttons to create common keyboard combinations for Mac users!

Here is the complete code.

#include "Keyboard.h"
 
const int delayValue = 100;
const char cmdKey = KEY_LEFT_GUI;
 
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  Keyboard.begin();
}
 
void loop() {
  if (digitalRead(2)) {
    Keyboard.press(cmdKey);
    Keyboard.press('c');
    delay(delayValue);
    Keyboard.releaseAll();
    while (digitalRead(2));
  }
  if (digitalRead(3)) {
    Keyboard.press(cmdKey);
    Keyboard.press('v');
    delay(delayValue);
    Keyboard.releaseAll();
    while (digitalRead(3));
  }
  if (digitalRead(4)) {
    Keyboard.press(cmdKey);
    Keyboard.press('n');
    delay(delayValue);
    Keyboard.releaseAll();
    while (digitalRead(4));
  }
  if (digitalRead(5)) {
    Keyboard.press(cmdKey);
    Keyboard.press('z');
    delay(delayValue);
    Keyboard.releaseAll();
    while (digitalRead(5));
  }
}

FAQs About The Arduino Touch Sensor Projects

I have compiled a list of the most frequently asked questions regarding touch sensor ICs. I have tried to answer them briefly.

If you still have questions, I will be glad to hear them in the comments section. 

1) How do I connect a touch sensor to my Arduino?

Most of the single-button touch sensors provide you with a digital output. You must connect the power pin, which is usually 5 V, a ground connection, and the touch sensor output. 

So, to complete the connection, you only need three wires: 5V, GND, and anyone GPIO Arduino pin. Y

ou may also find touch sensors that can provide analog and digital outputs.

The analog outputs will help you to decide the strength of the touch. 

2) How do I program my Arduino Touch Sensor?

The single-button touch sensors dont need any programming. They adapt to the environment automatically. Hence, using the buttons in your projects is straightforward. 

You may also come across a few advanced touch sensors which can be programmed via I2C or SPI interface (Example: MBR series touch controllers).

3) What is the basic operating principle of a touch sensor?

The touch sensor works on detecting fast-changing capacitance. The touch sensor button comprises two parallel plates separated by an insulator (PCB, for example). The plates can be formed using copper on the PCBs. 

When you touch the button, you increase the effective area of a plate, thereby causing a rise in the capacitance value.

The touch controller IC detects this sudden change in the capacitance.

If it exceeds a predefined limit, it will produce an output, indicating the touch action. 

4) Is the touch sensor an active or a passive device?

Touch sensors work on actively charging and discharging the sensor capacitance. The Touch sensors are considered active devices.

Conclusion

I have taken you through the basics of touch sensing for a single-button application. We also looked at the connection guide and an example code.

I am confident that you were able to test the circuit you built successfully. 

Touch sensors are an excellent addition to our projects. I will be glad to read about your following Arduino Touch projects.

Please post them in the comments section. If you are stuck, kindly leave a comment. I will try to help. 

I am hoping that the article to easy to follow. If you have any suggestions to improve the article, Please share them with me in the comments. 

What would you love to read next? Which topics have interested you more? Please let me know.

Don’t forget to share this article with your friends if you find this helpful!