Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touch: add capacitive touch sensing on normal GPIO pins #709

Merged
merged 1 commit into from
Oct 25, 2024

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Oct 21, 2024

It is possible to make a capacitive touch button out of any GPIO pin on most MCUs. The input may be somewhat unreliable compared to dedicated hardware, but it probably works well enough for most people. The way to do this is by connecting the GPIO pin to ground through a 1MΩ resistor, charging the pin by setting it high, switching to input, and measuring how long it takes to discharge (through the 1MΩ resistor).

Most of this is handled inside the driver. All you need to do to make this work is to connect an 1 mega-ohm resistor to the GPIO pin and to ground, and to connect some sort of metal surface to the GPIO pin. To avoid ESD issues, it's probably best to cover the metal surface with a thin non-conducting layer (Scotch tape for example).

Tested on the following chips/boards:

There are loads more I could test, but with this list I'm fairly confident it's going to work on nearly any MCU that's running at a high enough clock rate.

The sensitivity threshold in the example may need to be adjusted per board though, the default value of 100 typically recognizes when a cable is being touched but the RP2040 for example is capable of doing much more precise measurements if the power supply is sufficiently noise-free (it can even detect a finger at ~1cm with a large enough pad!)

This capacitive touch driver does detect the amount of noise on the input and tries to adjust for it, but the algorithm can probably be improved still. I'm not all that familiar with DSP algorithms. I might make a PR in the future to improve this if I get around to it.

Tested on the following chips/boards:

  * RP2040 (Raspberry Pi Pico)
  * ATSAMD21 (Adafruit PyBadge)
  * NRF52840 (PCA10056 developer board)
  * ESP8266 (NodeMCU)
  * ATmega328p (Arduino Uno)
  * ESP32C3 (WaveShare ESP-C3-32S-Kit)
  * FE310 (SiFive HiFive1 rev B)

The sensitivity threshold in the example may need to be adjusted per
board though, the default value of 100 typically recognizes when a cable
is being touched but the RP2040 for example is capable of doing much
more precise measurements if the power supply is sufficiently
noise-free.
@deadprogram
Copy link
Member

This PR is basically a better version of https://github.com/tinygo-org/drivers/tree/release/makeybutton which I am certainly willing to deprecate.

@aykevl
Copy link
Member Author

aykevl commented Oct 23, 2024

Oh I didn't realize we already had a capacitive touch driver!
This new driver is probably much more sensitive, with all the autocalibration built in. And of course with a configurable sensitivity. The one thing it doesn't have is the equivalent of HighMeansPressed (though it could be added of course).

@deadprogram
Copy link
Member

This new driver is probably much more sensitive, with all the autocalibration built in. And of course with a configurable sensitivity.

I like that a lot better for sure.

The one thing it doesn't have is the equivalent of HighMeansPressed (though it could be added of course).

Not that big a feature missing, plus I dislike the name I gave that feature anyhow.

Let's merge and mark the other as deprecated?

@aykevl
Copy link
Member Author

aykevl commented Oct 24, 2024

Let's merge and mark the other as deprecated?

Sure, sounds good :)
We might even keep both because I think they serve a somewhat different purpose.

@deadprogram
Copy link
Member

Now merging, thanks for the addition @aykevl

@deadprogram deadprogram merged commit 30f540c into dev Oct 25, 2024
1 check passed
@deadprogram deadprogram deleted the capacitive-gpio branch October 25, 2024 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants