banner
News center
The loyalty of our customers is a testament to the quality of our merchandise.

Turn Any Pico Pin Into a Capacitive Touch Sensor with This Simple Code

Apr 11, 2023

While other touch-sensing technologies exist, capacitive touch sensing is by far the most popular today. Your smartphone uses a capacitive touchscreen, your laptop uses a capacitive touch pad, and many devices use capacitive buttons in favor of conventional mechanical buttons. If you've ever looked into adding capacitive touch to one of your projects, then you've probably seen hardware designed for that purpose. But you may not need any special hardware at all. If you're using a Raspberry Pi Pico development board, then you can use James Brown's simple code to turn any pin into a capacitive touch sensor.

As the name suggest, a capacitive touch sensor relies on capacitance. That refers to a system's ability to store an electric charge, like in a capacitor. But simpler circuits exhibit a tiny amount of capacitance and that changes based on the proximity of conductive objects, such as fingers. You may have noticed that some touchscreens register a touch even before your finger makes contact. That's because their sensitivity is high and just moving your finger nearby triggers a detectable change in capacitance. Brown's code uses the Raspberry Pi Pico's built-in hardware to perform the same function and it works with any GPIO pin.

The code first sets a pin to an output and pulls it low. Then it immediately switches the same pin to an input with the internal pull-up resistor active. There is a tiny time delta between the two events and that changes based on a finger affecting the capacitance. The Pico's PIO (Programmable Input/Output) measures the delta and the whole process repeats about 60 times a second. The code returns a value between 0 and 1 based on the delta, with 0 being no detectable difference from the norm and 1 being a very strong difference. Users can choose a threshold to trigger an action, which should correspond to someone touching a piece of conductive material connected to that particular GPIO pin.

Brown's code is only meant to work for capacitive buttons and won't be suitable for 2D capacitive touch pads or touchscreens. But it should be very useful for anyone using a Raspberry Pi Pico that would like to add capacitive buttons to their next project.