STM32 TSC Tutorial + Examples (Touch Sensing Controller)

In this tutorial, we’ll discuss the STM32 TSC Capacitive Touch Sensing Peripheral, how the STM32 TSC capacitive touch works, and how to configure the TSC and TSL (touch sensing library) to implement a capacitive touch sensing example application.

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 Sensing
  2. STM32 TSC (Touch Sensing Controller)
  3. STM32 Touch Sensing Library (TSL)
  4. STM32 TSC Example Project
  5. STM32 TSC (Capacitive Touch Buttons) Example
  6. Wrap Up & References

STM32 Capacitive Touch Sensing

The capacitive touch sensing is a huge topic on its own but let’s abstract it down to two topics: first is designing the capacitive touch sensor (electrode) with which the end user will be interacting. The second topic is sampling (reading) this capacitive touch electrode by the microcontroller, which can achieve this task by a variety of techniques (hardware-based or software-based).

In this section, I’ll give you a brief introduction to each topic putting more emphasis on what can be achieved using the STM32 TSC (touch sensing controller) which is available in “some” product lines of the STM32 microcontrollers family.

1. Capacitive Touch Sensor (Electrode) Design

At the first stage of capacitive touch sensing application design, we have to make some design decisions to finalize the process of designing the capacitive touch sensor (electrode). This will impact the next stage which is developing firmware for the microcontroller to be able to capture (read or sample) the sensor we’ve designed earlier.

Here are some design decisions that we typically consider while designing capacitive touch sensing applications: sensor’s functionality, arrangement, shape, type (self or mutual), use a guard ring or not, passive or active guarding, ground or not to ground, grounding type, and much many others.

To keep this tutorial’s length manageable, I’ll briefly list some of the important design decisions that you can further research and learn more about (at least until I’ve some time to prepare a training for that topic):

CapTouch Sensor’s Functionality: There are many applications for CapTouch sensors that include (Touch Buttons, Sliders, Wheels, etc). So we need to decide on what’s required by the end user to have a good UI/UX with our application.

CapTouch Sensor’s Type: A capacitive touch sensor can be designed as a self-capacitance or mutual-capacitance sensor, each option has its own pros and cons, and it’s up to us (system designers) to choose which route to take.

CapTouch Sensor’s Arrangement: Multiple capacitive touch sensors can be arranged into an array or matrix for example which can lead to a great reduction in the count of channels required to sample a given number of CapTouch sensors.

CapTouch Sensor’s Shape: The shape of the sensor (electrode) has a noticeable impact on the performance of the target application, again, each shape has its pros and cons. The electrode can be circular, square, or any polygon. Round buttons are preferred for their robustness and being less noisy from an EMI perspective.


2. Capacitive Touch Sampling Process

After designing the capacitive touch sensor (electrode), now it’s time to prepare the target microcontroller to read (sample) the capacitive touch sensor. The problem statement can be abstractly illustrated as follows: the user’s finger when he/she approaches the CapTouch sensor will disrupt (couple into) the magnetic field of the charges on the surface of the sensor, essentially changing the magnitude of the capacitance of the sensor.

By measuring the CapTouch sensor’s capacitance continuously, we can easily identify a “user touch” event when it happens. So it comes down to measuring a capacitor’s value and deciding if it has significantly changed or not to register a “user touch”.

MCU manufacturers have different hardware peripherals dedicated to solving this very specific problem. At the core, they’re essentially doing the same thing but the technique of each peripheral is different. Some require minimal digital filtering and tuning while others can be a bit harder to tune. However, carefully designed sensors are much easier to tune.

NXP: TSI (touch sensing interface)

Renesas: CTSU (capacitive touch sensing unit)

STMicroelectronics: TSC (touch sensing controller)

Microchip: PTC, CPS, CVD, and CTMU

In the rest of this tutorial, we’ll focus on the STM32 TSC (touch sensing controller) peripheral to discuss how it works and how to use it in practice. The TSC is a great peripheral for handling capacitive touch-sensing applications but I have to say that Microchip is very creative in this area and their solutions to this very problem are really impressive if you’ve had the chance to work with them.

STM32 TSC (Touch Sensing Controller)

The STM32 TSC (Touch Sensing Controller) peripheral performs the touch sensor sampling process by charging the (Cx) sensor’s capacitance from the Vdd line and transferring this charge to a known fixed sampling capacitor (Cs). As the charge builds up on the plates of the sampling capacitor, its voltage goes up as well. At Vth, the sampling process stops and the count of charge transfer operations is saved.

A high count of charge transfers means that the measured capacitance is high, and vice versa. The delta (difference) can be compared against a programmable threshold value to decide if a “touch” event has occurred.


Approaching a self-capacitance sensor with your finger will increase the total capacitance of the sensor because the sensor’s channel pin will be measuring (Cx + Cn), where Cx is the sensor’s capacitance & Cn is the human’s finger capacitance. Note that: Cn gets larger the closer your finger is to the sensor and vice versa, so the Cn is variable, unlike the constant Cx.


STM32 TSC Channels

The TSC capacitive touch sensing channels are grouped into multiple groups depending on the number of available CapTouch channels in your target microcontroller. Each group has 4 channels, 3 of which are usable with sensors, and the last one is dedicated to the external sampling capacitor (Cs) as shown in the diagram below.


STM32 Microcontrollers With TSC

Unfortunately, not all STM32 microcontrollers have TSC peripherals inside, only some limited product lines have TSC integrated with a different number of available channels. The tables below summarize the STM32 microcontrollers that have a hardware TSC (touch sensing controller) and the number of available channels for each series of microcontrollers.

Maximum number of sensors on STM32 Series products:

PeripheralTSCSoftware assistTSCTSCTSCTSCTSCTSC
Sampling capacitors811888887

Maximum number of sensors on LQFP64 packages:

PeripheralTSCSoftware assistTSCTSCTSCTSC
Sampling capacitors8104866

If your STM32 microcontroller doesn’t have an internal TSC peripheral, you can check the tutorials below which will help you add capacitive touch sensing to your STM32-based project even if the target MCU doesn’t have an internal TSC peripheral.

STM32 Capacitive Touch Sensor (Switch) TTP223

STM32 CapTouch Sensing Without TSC

STM32 Touch Sensing Library (TSL)

The STM32 touch sensing library (TSL) provides a high-level abstraction for all functionalities that you may ever need to implement for the TSC peripheral. Therefore, we won’t need to start from scratch while implementing different capacitive touch sensing applications. The features of this library includes:

  • Proximity, touch keys, linear and rotary touch sensors are supported
  • Environment change system (ECS)
  • Detection time out (DTO)
  • Detection exclusion system (DXS)
  • Noise filtering
  • Unlimited number of sensors
  • Modular architecture allowing easy addition of new acquisitions or sensors
  • Each sensor has its own state machine
  • Simplified timing management
  • Management of error during acquisition

For complete reference, you need to check the UM1913 user manual for the STM32 TSL library. However, you can still proceed with this tutorial to create our first capacitive touch-sensing application using the TSC peripheral with STM32’s touch-sensing library (TSL).

STM32 TSC Example Project

In this example project, we’ll set up the STM32 to read 2x capacitive touch buttons using the TSC peripheral & TSL library. We’ll add 2x LEDs as outputs, each LED will turn ON while the corresponding touch button is “touched” by the user.

Example Project Steps Summary:

  • Set up a new project, system clock, and enable SWD (serial wire debug)
  • Set up 2x GPIO pins: for the output LEDs
  • Enable the TSC peripheral: 2x Channels + 1x Channel for the sampling capacitor
  • Enable the TSL library & add 2 basic touch keys
  • Call the tsl_user_Exec() function every 1ms
  • Read the MyTKeysB[] to get the state of the touch keys, then turn the LEDs On/Off accordingly

Example Project Wiring:


❕ Note

Please, be advised that the pin names on the Nucleo-32 boards are not always consistent with the actual physical pin name of the microcontroller. For example, pins A2 & A3 on the board silkscreen are the physical pins PA3 & PA4 on the microcontroller.

STM32 TSC (Capacitive Touch Buttons) 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

Configure The System clock.

Set the GPIO A3 & A4 as output pins (for the 2x LEDs)

Step #3

Go to the TSC configurations and enable 2x channels of group2 (G2_IO1 & G2_IO2) + enable a third channel for the sampling capacitor (G2_IO3).

The TSC Settings (configurations) in the bottom tab can be left as is on the default values.


Step #4

Go to the TSL library and enable it. Then edit the number of touch buttons to be 2 and assign the channels to the touch keys.


The other configurations can be left as is on the default values as shown below.


Step #5

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

STM32 TSC Capacitive Touch Sensing Example Code

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

STM32 Capacitive Touch Button Example Testing

❕ Note

Note That you don’t have to have a capacitive touch buttons PCB like the one I’m using in this demo, it’s just an old board that I made many years ago. You can use aluminum sheet paper, a wire, or even a potato!

As you’ve seen in the demo video above, it’s working as expected. To make sure the sampling process of the capacitive touch sensor is going on properly, you can hook your DSO’s probe to the sensor’s pin. You should see the charging/discharging waveform, here is how it looks on my oscilloscope with the zoom function enabled.

STM32 TSC (Touch Sensing Controller) Example Sampling Waveform

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
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 & References

In conclusion, we’ve explored how the STM32 TSC peripheral works, and how to use it with the TSL (touch sensing library) to implement a capacitive touch button (key) functionality with minimal steps. The final application can be tuned even more by adjusting the TSL parameters, reducing the Cs capacitor’s value to accelerate the sampling time, and so on.

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:

Helpful References:
  • AN5105: Getting Started with Touch Control
  • UM1913: Developing applications on STM32Cube with STMTouch touch sensing library
  • AN4316: Tuning a STMTouch-based application
  • AN4312: Guidelines for designing touch sensing applications with surface sensors
  • AN4299: Guidelines to improve conducted noise robustness on STM32 series touch sensing applications
  • AN4310: Sampling capacitor selection guide for MCU-based touch sensing applications
  • AN3960: ESD considerations for touch sensing applications
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