⬆️ ⬇️

Capacitive touch sensor without external strapping on STM32 Discovery



Earlier this year, a worthy citizen of Ariman wrote an article about how to fasten a capacitive touch sensor to a microcontroller. This idea seemed rather promising to me; for some devices, touch keys would be much better than mechanical ones. In this article I will talk about my implementation of this useful technology based on the STM32 Discovery debug card.



So, just starting to learn the STM32, I decided to add the ability to detect touches to the device as an exercise. Having begun to understand the theory and practice of the above article, I repeated the scheme of Comrade Ariman 'a. It worked perfectly, but I, a fan of minimalism, wanted to simplify it, getting rid of unnecessary elements. Superfluous in my opinion were the external resistor and the path to the power supply. All this is already in most microcontrollers, including AVR and STM32. I mean the pull-up resistors of the I / O ports. Why not charge the record and our fingers through them? In anticipation of a trick, I put together a scheme that, to my surprise, earned from the first time. As a matter of fact, it is even ridiculous to call this scheme, because all we need is to simply connect the contact plate to the leg of the debug board. All work will take the microcontroller.



What is the program like? First two functions:

The first one brings a logical “0” to the sensor's pin (zero pin in register C)



void Sensor_Ground (void) { GPIOC->CRL = 0x1; GPIOC->BRR |= 0x1; } 


')

The second sets up the same output pin, with a power pull.



 void Sensor_InPullUp (void) { GPIOC->CRL = 0x8; GPIOC->BSRR |= 0x1; } 




Now at the beginning of the polling cycle, we will call Sensor_Ground (), and wait a while to discharge all residual charge on the sensor to the ground. Then, we reset the count variable, which we will consider as the sensor charging time, and call Sensor_InPullUp ().



 Sensor_Ground(); Delay(0xFF); //   count = 0; Sensor_InPullUp(); 




Now the sensor starts charging through the internal pull-up resistor with a nominal value of the order of dozens of Kohms (30..50K ohm at STM32). The time constant of such a circuit will be equal to the read cycles, so I changed the quartz resonator on the debug board to a faster 20 MHz (by the way, I did not immediately notice that it turns out on STM32 Discovery the quartz changes without soldering). So, consider the processor clock cycles until a logical unit appears at the input:



 while(!(GPIOC->IDR & 0x1)) { count++; } 




After exiting this cycle, a count variable will be stored in proportion to the capacity of the sensor plate. In my case with a 20 MHz chip, the count value equals 1 with no pressing, 7-10 with the lightest touch, 15-20 with normal touch. It remains only to compare it with the threshold value and do not forget to call Sensor_Ground () again, so that by the next polling cycle the sensor has already been discharged.

The resulting sensitivity is enough for confident determination of touching the bare metal pads. When the sensor is covered with a sheet of paper or plastic, the sensitivity drops by three to four times, only confident pressures are well defined. To increase the sensitivity in the case when the sensor needs to be covered with protective material, you can increase the clock frequency of the microcontroller. With the chip series STM32F103, capable of operating at frequencies up to 72 MHz, millimeter obstacles between the finger and the sensor will not interfere.

Compared to the implementation of Ariman 'a, my approach works much faster (about a dozen cycles of polling one sensor), so I did not complicate the program by setting up timer interrupts.



Finally, a video demonstrating the work of the sensor.





Main.c test program.



Reference Manual on the microcontroller



Thanks ALPINE63rus user for a very useful article ARM-microcontrollers STM32F. Quick start with STM32-Discovery , Ariman user for the idea and clear theoretical description.



UPD. After ew1abz 'a comment, I decided to figure out the timing and found that the default STM32 Discovery is tuned to the clock frequency.

(HSE / 2) * 6 = 24 MHz, where HSE is the frequency of external quartz. Accordingly, changing the quartz from 8 to 20 MHz, I forced the poor STM to operate at 60 MHz. So, firstly, some of the findings are obviously not entirely correct, and secondly, what I was doing could lead to chip malfunctions. In case of such failures in the microcontroller there is a HardFault interrupt, using them, I checked the higher frequencies. So, the chip starts to fail only at 70 MHz. But although the controller digests this particular program at 60 MHz, it can behave unpredictably when using peripherals or working with Flash memory. Conclusion: treat this topic as an experiment, repeat only at your own peril and risk.

Source: https://habr.com/ru/post/132739/



All Articles