Skip to content
capacitive touch detection without external hardware for AVR and STM32
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples work with latest USBComposite library Sep 12, 2018
LICENSE Initial commit Oct 15, 2017 0.0.7 Nov 6, 2017
keywords.txt fix keywords Oct 17, 2017


ADCTouchSensorSTM is a library that allows users to create a capacitive sensor without ANY external hardware. It is based on the ideas and code in but extended to work on hardware other than AVR.


Most capacitive touch libraries require two pins and a large resistor to acquire precise readings. This library makes use of internal wiring to get decent resolution with just a single pin (plus on some MCUs one extra sacrificial pin, shared between allows the touch sensors).


  • ADCTouchSensor touchSensor = ADCTouchSensor(int analogPin, int sacrificialPin = -1, unsigned delaytime = [device dependent]);

  • touchSensor.begin(unsigned samples=500);

  • value = samples=5);

  • value = touchSensor.readRaw(unsigned samples=5);

  • analogPin: The pin to read data from

  • sacrificialPin is grounded and used for internal purposes. It shouldn't be connected to anything (beware of shorts). A single sacrificialPin can be used for many ADCTouchSensor instances. On the stm32f103c, you can omit the sacrificial pin, and an internal grounded ADC channel can be used. If your STM32F1 MCU has such a channel -- likely, the same one, namely 15 -- you can edit the library to support it (and email me)

  • delayTime is the amount of extra time to allow for charging up the touch pin; the default value is device dependent

  • samples: number of samples to take

  • begin(): reads reference data

  • read(): reads a value and corrects it via the reference data; the value can in principle range from -1024 to 1023, but in practice tends to be in the single digits (plus or minus) if there is no touch and at least 100 if there is a touch. Returns -10000 if the setup is wrong (i.e., no sacrificialPin and wrong MCU).

  • readRaw(): reads but does not correct via reference data; value can range from 0 to 1023. Returns -10000 if the setup is wrong (e.g., no sacrificialPin and MCU that requires it).

Useful define

  • ADCTOUCH_INTERNAL_GROUNDING: If this is defined, you don't need a sacrificial pin on your board.


  • CapacitivePiano: MIDI piano with capacitive sensing (USB MIDI only on stm32f1)

  • CapacitiveController: Makey-Makey style controller with capacitive sensing (only stm32f1)

You can’t perform that action at this time.