Skip to content

Commit 77d8da1

Browse files
committed
initial release
1 parent d88fd01 commit 77d8da1

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

README

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ADCTouch
2+
ADCTouch is a library that allows users to create a capacitive sensor without ANY external hardware.
3+
4+
##Purpose
5+
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.
6+
7+
##Usage
8+
int val = ADCTouch.read(byte analogChannel, int samples = 100);
9+
+analogChannel: the pin to use, you can just use A0 - A5
10+
+samples: (optional) the amount of samples to take, defaults to 100
11+
...range 1 - 65535
12+
...(value does not change with different amounts of samples)
13+
14+
##How it works
15+
To Acquire a reading, the library does the following:
16+
17+
+charge the test pin to 5V via pullup resistor (not directly to prevent short circuits)
18+
+discharge the internal ~14pF capacitor
19+
+set the pin to tristate
20+
+connect the 14pF capacitor with the pin so that charge distributes evenly
21+
+measure the voltage of the internal cap with analogRead()
22+
...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.
23+
24+
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.
25+
26+
A more accurate explanation can be found [here](http://tuomasnylund.fi/drupal6/content/capacitive-touch-sensing-avr-and-single-adc-pin)

examples/Buttons/Buttons.ino

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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); //send (boolean) pressed or not pressed
24+
Serial.print("\t"); //use if(value > threshold) to get the state of a button
25+
26+
Serial.print(value1 > 40);
27+
Serial.print("\t\t");
28+
29+
Serial.print(value0); //send actual reading
30+
Serial.print("\t");
31+
32+
Serial.println(value1);
33+
delay(100);
34+
}

keywords.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map For ADCTouch
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
ADCTouch KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
read KEYWORD2
16+
17+
#######################################
18+
# Constants (LITERAL1)
19+
#######################################
20+

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ADCTouch
2+
version=1.0.0
3+
author=martin2250
4+
maintainer=martin2250 <martin.225@web.de>
5+
sentence=Create Touch Sensors with a single (Analog)Pin without external Hardware
6+
paragraph=This library uses the internal wiring of AVR microcontrollers to measure capacitance as described here <http://tuomasnylund.fi/drupal6/content/capacitive-touch-sensing-avr-and-single-adc-pin>
7+
category=Sensors
8+
url=https://github.com/martin2250/ADCTouch
9+
architectures=avr

src/ADCTouch.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
ADCTouch.cpp - Library for Capacittive touch sensors using only one ADC PIN
3+
Created by martin2250, April 23, 2014.
4+
Released into the public domain.
5+
*/
6+
#include "Arduino.h"
7+
#include "ADCTouch.h"
8+
9+
int ADCTouchClass::read(byte ADCChannel, int samples)
10+
{
11+
long _value = 0;
12+
for(int _counter = 0; _counter < samples; _counter ++)
13+
{
14+
pinMode(ADCChannel, INPUT_PULLUP);
15+
16+
ADMUX |= 0b11111;
17+
ADCSRA |= (1<<ADSC); //start conversion
18+
while(!(ADCSRA & (1<<ADIF))); //wait for conversion to finish
19+
ADCSRA |= (1<<ADIF); //reset the flag
20+
21+
pinMode(ADCChannel, INPUT);
22+
_value += analogRead(ADCChannel);
23+
}
24+
return _value / samples;
25+
}
26+
27+
ADCTouchClass ADCTouch;

src/ADCTouch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* ADCTouch.h - Library for Capacittive touch sensors using only one ADC PIN Created by martin2250, April 23, 2014. Released into the public domain.*/#ifndef ADCT_h#define ADCT_h#include "Arduino.h"class ADCTouchClass{ public: int read(byte ADCChannel, int samples = 100);};extern ADCTouchClass ADCTouch;#endif

0 commit comments

Comments
 (0)