Simplest capacitive touch implementation

Dependencies:   mbed

Capacitive touch is widely used in our dairly life. Curious about how it works? Let's build a capacitive button.

/media/uploads/yihui/capacitive_touch.jpg

The capacitance of a capacitive button will be changed when with a human touch. There are several ways to measure the change of the capacitance. Here we use the charging/discharging time of a simple RC circuit to correlate the capacitance.

Hardware

  • An mbed board (Arch is used here)
  • An Apple with a bite
  • A wire

Use the wire to connect the Arch's P0_11 with the apple.

Software

The pull-down feature of a general porpuse I/O is used to discharging the capacitor of the touch button. A Ticker is used to measure the discharging time of the capacitor every 1/64 seconds.

Import program

00001 #include "mbed.h"
00002 
00003 DigitalOut led(LED1);
00004 DigitalInOut touch(P0_11);       // Connect a wire to P0_11
00005 Ticker tick;
00006 
00007 uint8_t touch_data = 0;         // data pool
00008 
00009 void detect(void)
00010 {
00011     uint8_t count = 0;
00012     touch.input();              // discharge the capacitor
00013     while (touch.read()) {
00014         count++;
00015         if (count > 4) {
00016             break;
00017         }
00018     }
00019     touch.output();
00020     touch.write(1);             // charge the capacitor
00021     
00022     if (count > 2) {
00023         touch_data = (touch_data << 1) + 1;
00024     } else {
00025         touch_data = (touch_data << 1);
00026     }
00027     
00028     if (touch_data == 0x01) {
00029         led = 1;                // touch
00030     } else if (touch_data == 0x80) {
00031         led = 0;                // release
00032     }
00033 }
00034 
00035 int main()
00036 {
00037     touch.mode(PullDown);
00038     touch.output();
00039     touch.write(1);
00040     
00041     tick.attach(detect, 1.0 / 64.0);
00042     
00043     while(1) {
00044         // do something
00045     }
00046 }
Committer:
yihui
Date:
Mon May 05 11:43:57 2014 +0000
Revision:
1:a1a6d30c41d1
Parent:
0:090d8059cc10
use P0_11 instead of P1_5; ; P1_5 of Seeeduino Arch V1.0 is with dual diode and wouldn't work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:090d8059cc10 1 #include "mbed.h"
yihui 0:090d8059cc10 2
yihui 0:090d8059cc10 3 DigitalOut led(LED1);
yihui 1:a1a6d30c41d1 4 DigitalInOut touch(P0_11); // Connect a wire to P0_11
yihui 0:090d8059cc10 5 Ticker tick;
yihui 0:090d8059cc10 6
yihui 0:090d8059cc10 7 uint8_t touch_data = 0; // data pool
yihui 0:090d8059cc10 8
yihui 0:090d8059cc10 9 void detect(void)
yihui 0:090d8059cc10 10 {
yihui 0:090d8059cc10 11 uint8_t count = 0;
yihui 0:090d8059cc10 12 touch.input(); // discharge the capacitor
yihui 0:090d8059cc10 13 while (touch.read()) {
yihui 0:090d8059cc10 14 count++;
yihui 0:090d8059cc10 15 if (count > 4) {
yihui 0:090d8059cc10 16 break;
yihui 0:090d8059cc10 17 }
yihui 0:090d8059cc10 18 }
yihui 0:090d8059cc10 19 touch.output();
yihui 0:090d8059cc10 20 touch.write(1); // charge the capacitor
yihui 0:090d8059cc10 21
yihui 0:090d8059cc10 22 if (count > 2) {
yihui 0:090d8059cc10 23 touch_data = (touch_data << 1) + 1;
yihui 0:090d8059cc10 24 } else {
yihui 0:090d8059cc10 25 touch_data = (touch_data << 1);
yihui 0:090d8059cc10 26 }
yihui 0:090d8059cc10 27
yihui 0:090d8059cc10 28 if (touch_data == 0x01) {
yihui 0:090d8059cc10 29 led = 1; // touch
yihui 0:090d8059cc10 30 } else if (touch_data == 0x80) {
yihui 0:090d8059cc10 31 led = 0; // release
yihui 0:090d8059cc10 32 }
yihui 0:090d8059cc10 33 }
yihui 0:090d8059cc10 34
yihui 0:090d8059cc10 35 int main()
yihui 0:090d8059cc10 36 {
yihui 0:090d8059cc10 37 touch.mode(PullDown);
yihui 0:090d8059cc10 38 touch.output();
yihui 0:090d8059cc10 39 touch.write(1);
yihui 0:090d8059cc10 40
yihui 0:090d8059cc10 41 tick.attach(detect, 1.0 / 64.0);
yihui 0:090d8059cc10 42
yihui 0:090d8059cc10 43 while(1) {
yihui 0:090d8059cc10 44 // do something
yihui 0:090d8059cc10 45 }
yihui 0:090d8059cc10 46 }