{"id":12402,"date":"2024-03-27T12:19:56","date_gmt":"2024-03-27T10:19:56","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=12402"},"modified":"2024-03-28T06:57:04","modified_gmt":"2024-03-28T04:57:04","slug":"stm32-tsc-tutorial-examples-touch-sensing-controller","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/stm32-tsc-tutorial-examples-touch-sensing-controller\/","title":{"rendered":"STM32 TSC Tutorial + Examples (Touch Sensing Controller)"},"content":{"rendered":"\n

In this tutorial, we’ll discuss the STM32 TSC Capacitive Touch Sensing Peripheral<\/strong>, 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.<\/p>\n\n\n\n

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!<\/p>\n\n\n

Table of Contents<\/h2>\n
    \n
  1. STM32 Capacitive Touch Sensing<\/a>\n\n\n<\/li>\n\n
  2. STM32 TSC (Touch Sensing Controller)<\/a>\n\n\n<\/li>\n\n
  3. STM32 Touch Sensing Library (TSL)<\/a>\n\n<\/li>\n
  4. STM32 TSC Example Project<\/a>\n\n<\/li>\n
  5. STM32 TSC (Capacitive Touch Buttons) Example<\/a>\n\n\n<\/li>\n\n
  6. Wrap Up & References<\/a>\n<\/li><\/ol>\n\n\n
    \n\n\n

    STM32 Capacitive Touch Sensing<\/strong><\/h2>\n\n\n

    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).<\/p>\n\n\n\n

    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.<\/p>\n\n\n

    1. Capacitive Touch Sensor (Electrode) Design<\/strong><\/h3>\n\n\n

    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.<\/p>\n\n\n\n

    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.<\/p>\n\n\n\n

    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):<\/p>\n\n\n\n

    CapTouch Sensor’s Functionality<\/mark><\/strong>: 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.<\/p>\n\n\n\n

    CapTouch Sensor’s Type<\/mark><\/strong>: 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.<\/p>\n\n\n\n

    CapTouch Sensor’s Arrangement<\/mark><\/strong>: 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.<\/p>\n\n\n\n

    CapTouch Sensor’s Shape<\/mark><\/strong>: 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.<\/p>\n\n\n\n

    \"Capacitive-Touch-Sensors-Design-Buttons\"<\/figure>\n\n\n\n

    <\/p>\n\n\n

    2. Capacitive Touch Sampling Process<\/strong><\/h3>\n\n\n

    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. <\/p>\n\n\n\n

    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”.<\/p>\n\n\n\n

    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.<\/p>\n\n\n\n

    NXP<\/strong>: TSI (touch sensing interface)<\/p>\n\n\n\n

    Renesas<\/strong>: CTSU (capacitive touch sensing unit)<\/p>\n\n\n\n

    STMicroelectronics<\/strong>: TSC (touch sensing controller)<\/p>\n\n\n\n

    Microchip<\/strong>: PTC, CPS, CVD, and CTMU<\/p>\n\n\n\n

    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.<\/p>\n\n\n\n


    \n\n\n

    STM32 TSC (Touch Sensing Controller)<\/strong><\/h2>\n\n\n

    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.<\/p>\n\n\n\n

    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.<\/p>\n\n\n\n

    \"STM32-TSC-Touch-Sensing-Controller-Diagram\"<\/figure>\n\n\n\n

    <\/p>\n\n\n\n

    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.<\/p>\n\n\n\n

    \"STM32-Capacitive-Touch-Sensing-Figure\"<\/figure>\n\n\n\n

    <\/p>\n\n\n

    STM32 TSC Channels<\/strong><\/h3>\n\n\n

    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.<\/p>\n\n\n\n

    \"STM32-TSC-Groups\"<\/figure>\n\n\n\n

    <\/p>\n\n\n

    STM32 Microcontrollers With TSC<\/strong><\/h3>\n\n\n

    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.<\/p>\n\n\n\n

    Maximum number of sensors on STM32 Series products:<\/p>\n\n\n\n

    <\/td>STM32L0<\/th>STM32L1<\/th>STM32L4<\/th>STM32L4+<\/th>STM32L5<\/th>STM32F0<\/th>STM32F3<\/th>STM32WB<\/th><\/tr>
    Peripheral<\/th>TSC<\/td>Software assist<\/td>TSC<\/td>TSC<\/td>TSC<\/td>TSC<\/td>TSC<\/td>TSC<\/td><\/tr>
    Groups<\/th>8<\/td>11<\/td>8<\/td>8<\/td>8<\/td>8<\/td>8<\/td>7<\/td><\/tr>
    Channels<\/th>32<\/td>48<\/td>32<\/td>32<\/td>30<\/td>32<\/td>32<\/td>28<\/td><\/tr>
    Sampling capacitors<\/th>8<\/td>11<\/td>8<\/td>8<\/td>8<\/td>8<\/td>8<\/td>7<\/td><\/tr>
    Sensors<\/th>24<\/td>37<\/td>24<\/td>24<\/td>22<\/td>24<\/td>24<\/td>21<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

    Maximum number of sensors on LQFP64 packages:<\/p>\n\n\n\n

    <\/td>STM32L0<\/th>STM32L1<\/th>STM32L4<\/th>STM32L5<\/th>STM32F0<\/th>STM32F3<\/th><\/tr>
    Peripheral<\/th>TSC<\/td>Software assist<\/td>TSC<\/td>TSC<\/td>TSC<\/td>TSC<\/td><\/tr>
    Groups<\/th>8<\/td>10<\/td>4<\/td>8<\/td>6<\/td>6<\/td><\/tr>
    Channels<\/th>32<\/td>33<\/td>16<\/td>30<\/td>24<\/td>24<\/td><\/tr>
    Sampling capacitors<\/th>8<\/td>10<\/td>4<\/td>8<\/td>6<\/td>6<\/td><\/tr>
    Sensors<\/th>24<\/td>23<\/td>12<\/td>22<\/td>18<\/td>18<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

    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.<\/p>\n\n\n

    \n
    \n
    \n
    \n
    \n
    \n\n
    \"STM32<\/a><\/figure>\n\n<\/div><\/div>\n\n
    \n\n

    STM32 CapTouch Sensor (TTP223)<\/a><\/p>\n\n<\/div><\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n\n

    \n
    \n
    \n
    \n\n
    \"STM32<\/figure>\n\n<\/div><\/div>\n\n
    \n\n

    STM32 CapTouch Sensing Without TSC<\/p>\n\n<\/div><\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n<\/div>\n\n\n


    \n\n\n

    STM32 Touch Sensing Library (TSL)<\/strong><\/h2>\n\n\n

    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:<\/p>\n\n\n\n