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
- STM32 Capacitive Touch Sensing
- STM32 TSC (Touch Sensing Controller)
- STM32 Touch Sensing Library (TSL)
- STM32 TSC Example Project
- STM32 TSC (Capacitive Touch Buttons) Example
- 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:
STM32L0 | STM32L1 | STM32L4 | STM32L4+ | STM32L5 | STM32F0 | STM32F3 | STM32WB | |
---|---|---|---|---|---|---|---|---|
Peripheral | TSC | Software assist | TSC | TSC | TSC | TSC | TSC | TSC |
Groups | 8 | 11 | 8 | 8 | 8 | 8 | 8 | 7 |
Channels | 32 | 48 | 32 | 32 | 30 | 32 | 32 | 28 |
Sampling capacitors | 8 | 11 | 8 | 8 | 8 | 8 | 8 | 7 |
Sensors | 24 | 37 | 24 | 24 | 22 | 24 | 24 | 21 |
Maximum number of sensors on LQFP64 packages:
STM32L0 | STM32L1 | STM32L4 | STM32L5 | STM32F0 | STM32F3 | |
---|---|---|---|---|---|---|
Peripheral | TSC | Software assist | TSC | TSC | TSC | TSC |
Groups | 8 | 10 | 4 | 8 | 6 | 6 |
Channels | 32 | 33 | 16 | 30 | 24 | 24 |
Sampling capacitors | 8 | 10 | 4 | 8 | 6 | 6 |
Sensors | 24 | 23 | 12 | 22 | 18 | 18 |
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 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:
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* * LAB Name: STM32 TSC CapTouch Buttons Example * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include "main.h" #include "touchsensing.h" TSC_HandleTypeDef htsc; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_TSC_Init(void); static void TSC_Handler(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_TSC_Init(); MX_TOUCHSENSING_Init(); tsl_user_Init(); while (1) { tsl_user_Exec(); TSC_Handler(); HAL_Delay(1); } } void TSC_Handler(void) { if(MyTKeysB[0].p_Data->StateId == TSL_STATEID_DETECT) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); } else if(MyTKeysB[0].p_Data->StateId == TSL_STATEID_RELEASE) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); } if(MyTKeysB[1].p_Data->StateId == TSL_STATEID_DETECT) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); } else if(MyTKeysB[1].p_Data->StateId == TSL_STATEID_RELEASE) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); } } |
STM32 Capacitive Touch Button Example Testing
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.
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 Name | Amazon.com | AliExpress | eBay |
1 | STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz) | Amazon | AliExpress | eBay |
1 | Nucleo-L432KC (ARM Cortex-M4 @ 80MHz) | Amazon | AliExpress | eBay |
1 | ST-Link V2 Debugger | Amazon | AliExpress | eBay |
2 | BreadBoard | Amazon | AliExpress | eBay |
1 | LEDs Kit | Amazon & Amazon | AliExpress | eBay |
1 | Resistors Kit | Amazon & Amazon | AliExpress | eBay |
1 | Capacitors Kit | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Jumper Wires Pack | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Push Buttons | Amazon & Amazon | AliExpress | eBay |
1 | Potentiometers | Amazon | AliExpress | eBay |
1 | Micro USB Cable | Amazon | AliExpress | eBay |
★ 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.
- 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