STM32 Capacitive Touch Sensor (Switch) TTP223

In this tutorial, we’ll discuss the STM32 Capacitive Touch Sensor (Button/Switch) Interfacing, how the capacitive touch button (TTP223) works, and how to configure the STM32 GPIO to interface with the external capacitive touch button (switch).

The practical example we’ll implement in this tutorial will be a very good starting point for your touch-enabled STM32 project. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 Capacitive Touch Sensor (Switch)
  2. STM32 Capacitive Touch Sensor Example Project
  3. STM32 Touch Sensor (Switch) Example
  4. Wrap Up

STM32 Capacitive Touch Sensor (Switch)

Using a capacitive touch sensor (switch) module like the one shown below is the easiest way to add capacitive touch sensing to your STM32 project. This module handles the touch sensing for you and gives you a clean digital signal whenever a user touch event is detected.

Capacitive Touch Sensor (Switch) TTP223

When & Why To Use External Touch Sensors Like TTP223

Not all STM32 target microcontrollers come with an internal TSC (Touch Sensing Controller) which is a peripheral designed to automate the process of sampling external touch sensors (electrodes). Therefore, it can be a good option to use a standalone external touch sensor (switch) such as the TTP223.

Using an external standalone touch sensor (like the TTP223) is much easier for prototyping a project idea. However, if your target application requires touch sensing, it’s advisable to go for an STM32 microcontroller that has a TSC hardware peripheral.

Pros & Cons of Using External Touch Sensors Like TTP223

Easier to use from a software perspectiveTakes up a lot of space if you’d like to use multiple units compared to what you could achieve by designing your own custom CapTouch PCB solution
Easier to use from a hardware perspectiveLittle to no controllability over the sensor’s sensitivity or touch direction. It will register touch events from the top & bottom sides

This article will give you more in-depth information about the STM32 TSC (Touch Sensing Controller) hardware peripheral and how to use it with your custom designs of touch sensors (electrodes).

STM32 Capacitive Touch Sensor Example Project

In this example project, we’ll set up the STM32 to read the digital input coming from the touch sensor (switch) and use it to control the state of an output LED that turns ON while the button is touched and vice versa.

Example Project Steps Summary:

  • Set up a new project with a system clock @ 72MHz
  • Set up 2x GPIO pins: an output pin for an LED, and an input pin for the touch button (switch).

Example Project Wiring Diagram


STM32 Touch Sensor (Switch) Example

And now, let’s build this system step-by-step

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller.

Step #2

Set the GPIO B12 as an output pin (for the LED)

Set the GPIO B10 as an input pin (for the touch sensor)

Step #3

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

STM32 RCC External Clock Selection CubeMX

Step #4

Go to the clock configurations page, select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.


Step #5

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Touch Sensor (Switch) Example Code

Here is The Application Code For This LAB (main.c)

Code Explanation

This is a very simple project in terms of what’s going on in the code. Which reads the input GPIO pin and checks its state, if it’s HIGH, the LED pin will be driven HIGH as well, and vice versa. That’s all about it.

STM32 Capacitive Touch Button Example Testing

Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1Capacitive Touch Sensor (Button / Switch)AmazonAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.

Wrap Up

In conclusion, we’ve explored how to use the TTP223 capacitive touch sensor button (switch) with an STM32 microcontroller without needing to have an internal TSC (touch sensing controller) unit. It needs only one digital GPIO pin and it’s so easy to interface from both perspectives (software & hardware).

You can build on top of the example provided in this tutorial and/or explore the other parts of the STM32 capacitive touch sensing tutorials series for more information about the other STM32 capacitive touch sensing techniques.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment