Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

ADCTouch

ADCTouch Library for Arduino
Author:  martin2250

Introduction

ADCTouch is a library that allows users to create a capacitive sensor without ANY external hardware.


Navigation


Purpose

Most capacitive touch libraries require two pins and a large resistor to acquire precise readings. This library makes use of the AVRs internal wiring to get decent resolution with just a single pin.


Usage

int ADCTouch.read(byte analogChannel, int samples = 100);

  • analogChannel: the pin to use, you can just use A0 - A5
  • samples: (optional) the amount of samples to take, defaults to 100
    • range 1 - 65535
    • (value does not change with different amounts of samples)

the returned integer can have the same value as analogRead(), since it uses it to read the sensor. (0 - 1023)


Example Sketch

This sketch will print the offset compensated readings of A0 and A1 to the serial monitor, as well as a binary value if it passes the threshold value (=is the button pressed?)

  1. #include <ADCTouch.h>
  2.  
  3. int ref0, ref1;       //reference values to remove offset
  4.  
  5. void setup()
  6. {
  7.     // No pins to setup, pins can still be used regularly, although it will affect readings
  8.  
  9.     Serial.begin(9600);
  10.  
  11.     ref0 = ADCTouch.read(A0, 500);    //create reference values to
  12.     ref1 = ADCTouch.read(A1, 500);      //account for the capacitance of the pad
  13. }
  14.  
  15. void loop()
  16. {
  17.     int value0 = ADCTouch.read(A0);   //no second parameter
  18.     int value1 = ADCTouch.read(A1);     //   --> 100 samples
  19.  
  20.     value0 -= ref0;       //remove offset
  21.     value1 -= ref1;
  22.  
  23.     Serial.print(value0 > 40);    //return pressed or not pressed
  24.     Serial.print("\t");
  25.     Serial.print(value1 > 40);
  26.     Serial.print("\t\t");
  27.     Serial.print(value0);             //return value
  28.     Serial.print("\t");
  29.     Serial.println(value1);
  30.     delay(100);
  31. }

How it works

To Acquire a reading, the library does the following:

  1. charge the test pin to 5V via pullup resistor (not directly to prevent short circuits)
  2. discharge the internal ~14pF capacitor
  3. set the pin to tristate
  4. connect the 14pF capacitor with the pin so that charge distributes evenly
  5. measure the voltage of the internal cap with analogRead()

If the pin has a low capacitance, the stored charge will be small as will the resulting voltage, if the external capacitance is equal to 14pF, the volatage should be ~2.5V. Even higher capacitances will result in volatges > 2.5V. The chip and arduino board already have stray capacitances that will produce an output of ~390 and just a single external wire can boost that up to 500, so you really need offset compensation.

The accuracy is really good, most often even the LSB/smalles digit can still yield usable data and just vary by a single unit between readings (at only 100 samples, more will mean even less variation). The sensitivity is phenomenal, with a large enough surface, it can sense a person in more than 2 feet distance.

A more accurate explanation can be found here

Tested Devices

Here you can enter all devices that you have confirmed to work with this library, theoretically all boards that have an AVR chip inside them should work, but I'm not sure if the Mega also does. Also some boards have more than A0-A5 analog pins. If your device is not listed here, you can still try it out, there is absolutely no risk of damaging your board, even if you connect the sensor directly to +5V

Boards:

  • Arduino UNO -> A0 - A5
  • Arduino NANO -> A0 - A7 (Atmega328)

Download & Installation

Use the library manager or download from github: https://github.com/martin2250/ADCTouch