STM32 Capacitive Touch Sensing Without TSC

In this tutorial, we’ll discuss how to implement STM32 Capacitive Touch Sensing Without TSC Peripheral. Many different techniques can be used to implement capacitive touch sensing with STM32 microcontrollers that don’t have a TSC (touch sensing controller) peripheral inside.

In this tutorial, we’ll explore a very basic technique that will help us add multiple CapTouch keys to our application that’s based on the STM32F103 blue pill board which doesn’t have TSC channels. 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 Capacitive Touch Without TSC Example
  3. STM32 Touch Sensing Without TSC Example Project
  4. Wrap Up

STM32 Capacitive Touch Sensing

As stated in the previous STM32 TSC Tutorial, all capacitive touch sensing techniques & peripherals are designed to solve the exact same problem. Which is measuring and quantifying the change in the capacitance value of an electrode (touch sensor). The TSC peripheral is a very complex and capable peripheral as we’ve seen in the previous tutorial but unfortunately, it’s not present in most of the STM32 microcontroller series.

Therefore, we’ll use a very basic technique that works as follows.

RC+Timer CapTouch Sensor Acquisition

As we’ve stated earlier in the TSC tutorial, a self-capacitance touchpad will have a stray capacitance with local ground. And when a grounded object gets into its electrostatic field’s range, it’ll couple to its magnetic field and the total sensor’s capacitance increases.

I did also mention that this capacitance increase is significant relative to the sensor’s self-capacitance. But generally speaking, it’s a very tiny amount of capacitance (in the order of 10-20pF). It depends on various parameters like the insulating layer thickness and material type, the touchpad shape and size on the PCB, and so on.

Given that we’re dealing with a very tiny capacitance value and it needs to be measured, we’ll have to make the charging process very slow in order to be able to monitor it. And here is how to do it:

We’ll use 2 GPIO pins, the first of which will be a driver pin. And the second one will be an input pin (a normal digital input pin) and will get hooked to our touchpad (electrode/sensor). We’ll also place a large value resistor (1.5MΩ) between both pins. Therefore, when we send a high signal (logic 1 or 3.3v), the other pin will read 1 but only after the capacitor charges to the level where it can be detected as a logic 1 input. This is basically how this technique works!

STM32-Capacitive-Touch-Sensing-Without-TSC-Example1
Charging Time During “No Touch” Event

Using a timer module, we can now start charging the sensor capacitor and start the timer at the same time. And stop it when the digital input pin reads logic 1. Now, you’ve a reading for the charging time that is directly proportional to the sensor’s capacitance. The higher the capacitance is, the more time it takes the input pin to reach logic 1 threshold voltage.

In other words, when a human touches the electrode (sensor), the capacitance increases. Therefore, the time measurement should show an increase in the ticks count. The software will therefore register a “user touch” event and act correspondingly.

Charging Time During “User Touch” Event

And that’s how it works! It’s easy to prototype on a breadboard and easy to implement in code. This technique requires 1x hardware timer peripheral + 2x GPIO (1 Drive & 1 Sense) pins.

STM32-TSC-Touch-Sensing-Controller-Example

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 Without TSC Example

In this example project, we’ll set up the STM32 to sample (read) two capacitive touch buttons using the (RC+Timer) technique. The CapTouch buttons will be used to control 2x output LEDs each of which will be turned ON as long as the corresponding CapTouch button is “touched” by the end 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
  • Set up 2x GPIO pins: for the output driver pins
  • Set up 2x GPIO pins: for the input sensor lines
  • Enable the Timer2 peripheral: to measure the charging time of each sensor

Example Project Wiring Diagram

STM32 Touch Sensing Without TSC Example Wiring Diagram


STM32 Touch Sensing Without TSC Example Project

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 2x GPIOs B0 & B1 as output pins (for the 2x LEDs)

Set the 2x GPIOs B10 & B11 as output pins (for the 2x output driver pins)

Set the 2x GPIOs A5 & A6 as input pins (for the 2x input touch sensors)

Step #3

Enable Timer2 by setting the internal clock as a clock source for it and set the prescaler to 24 (nothing magical about this value, we just want to slow down the timer so it doesn’t overflow when the CapTouch button is touched “high capacitance”).

STM32-CapTouch-Sensing-Without-TSC-Example-Configuration

Step #4

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

STM32 RCC External Clock Selection CubeMX

Step #5

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.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #6

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)

STM32 Capacitive Touch Without TSC Example Testing

❕ Note

Note That you may need to change the threshold value in the code to tune the application to work properly on your setup. If you’ve noticed that the timer is overflowing during the sampling process, you’ll need to increase the prescaler value to prevent such an issue.

Modifying the provided code example should be easy as I’ve kept everything as generic as possible. You may also consider optimizing it a little bit by using only 1 driver output pin instead of 1 pin per sensor which is not efficient if your system requires so many input CapTouch buttons.


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
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
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 implement capacitive touch sensing with any STM32 microcontroller that doesn’t have an internal TSC (touch sensing controller) peripheral. The given method in this tutorial is very simplistic and may not be as robust as you need but it’s definitely worth trying and tuning for your applications’ needs.

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:
(3) CapTouch Sensing Without TSC
STM32 Capacitive Touch Sensing Without TSC

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