Arduino Forum

Forum 2005-2010 (read only) => General => Exhibition => Topic started by: paulb on Feb 10, 2007, 03:57 am

Title: Arduino as Capacitive Sensor
Post by: paulb on Feb 10, 2007, 03:57 am
Some experiments with the Arduino as a capacitive sensor. All it requires is a 10 M resistor and a piece of wire. I was able to sense a hand about four inches from my 1.5" sq aluminum foil sensor.

Included are some machine code and port manipulation and an easy smoothing filter.


// CapSense.pde
// Paul Badger 2007

// Fun with capacitive sensing and some machine code - for the Arduino (or Wiring Boards).
// Note that the machine code is based on Arduino Board and will probably require some changes for Wiring Board
// This works with a high value (1-10M) resistor between an output pin and an input pin.
// When the output pin changes it changes the state of the input pin in a time constant determined by R * C
// where R is the resistor and C is the capacitance of the pin plus any capacitance present at the sensor.
// It is possible when using this setup to see some variation in capacitance when one's hand is 3 to 4 inches from the sensors
// Try experimenting with larger sensors. Lower values of R will probably yield higher reliability.
// Use 1 M resistor (or less maybe) for absolute touch to activate.
// With a 10 M resistor the sensor will start to respond 1-2 inches away

// Setup
// Connect a 10M resistor between pins 8 and 9 on the Arduino Board
// Connect a small piece of alluminum or copper foil to a short wire and also connect it to pin 9

// When using this in an installation or device it's going to be important to use shielded cable if the wire between the sensor is
// more than a few inches long, or it runs by anything that is not supposed to be sensed.
// Calibration is also probably going to be an issue.
// Instead of "hard wiring" threshold values - store the "non touched" values in a variable on startup - and then compare.
// If your sensed object is many feet from the Arduino Board you're probably going to be better off using the Quantum cap sensors.

// Machine code and Port stuff from a forum post by ARP  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169088394/0#0



int  i;
unsigned int x, y;
float accum, fout, fval = .07;    // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter

void setup() {
 Serial.begin(9600);

 DDRB=B101;     // DDR is the pin direction register - governs inputs and outputs- 1's are outputs
 // Arduino pin 8 output, pin 9 input, pin 10 output for "guard pin"
 //  preceding line is equivalent to three lines below
 //  pinMode(8, OUTPUT);     // output pin
 //  pinMode(9, INPUT);      // input pin
 //  pinMode(10, OUTPUT);    // guard pin
 digitalWrite(10, LOW);  //could also be HIGH - don't use this pin for changing output though
}

void loop() {
 y = 0;        // clear out variables
 x = 0;

 for (i=0; i < 4 ; i++ ){       // do it four times to build up an average - not really neccessary but takes out some jitter

   // LOW-to-HIGH transition
   PORTB = PORTB | 1;                    // Same as line below -  shows programmer chops but doesn't really buy any more speed
   // digitalWrite(8, HIGH);    
   // output pin is PortB0 (Arduino 8), sensor pin is PortB1 (Arduinio 9)                                  

   while ((PINB & B10) != B10 ) {        // while the sense pin is not high
     //  while (digitalRead(9) != 1)     // same as above port manipulation above - only 20 times slower!                
     x++;
   }
   delay(1);

   //  HIGH-to-LOW transition
   PORTB = PORTB & 0xFE;                // Same as line below - these shows programmer chops but doesn't really buy any more speed
   //digitalWrite(8, LOW);              
   while((PINB & B10) != 0 ){            // while pin is not low  -- same as below only 20 times faster
     // while(digitalRead(9) != 0 )      // same as above port manipulation - only 20 times slower!
     y++;  
   }

   delay(1);
 }

 fout =  (fval * (float)x) + ((1-fval) * accum);  // Easy smoothing filter "fval" determines amount of new data in fout
 accum = fout;  

 Serial.print((long)x, DEC);    // raw data - Low to High
 Serial.print( "   ");
 Serial.print((long)y, DEC);    // raw data - High to Low
 Serial.print( "   ");
 Serial.println( (long)fout, DEC); // Smoothed Low to High
}


Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 16, 2007, 07:39 pm
hi, I tried to make your code work but no chance  :'(
I am using a 3.3M resistor and I don't have a clue  :-/
Here is a picture:

(http://sosobering.free.fr/ardui.jpg)

The white cable has a piece of copper ( 4x3cm ) at the other end.
I am new to arduino so you might find me quite silly. I was planning on using a qt but if your technique works it's better.

Thank you for your time and contribution to arduino.
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 16, 2007, 07:59 pm
Hi again, I am really confused  :-?

I just changed the cable position. I moved the cable connected to pin9  and the white cable on the line above. Then I linked the 2 lines with the resistor and now It works !!!!!

I put a picture for the newbies like me ( and documentation ) :)

(http://sosobering.free.fr/ardui2.jpg)

I am sorry for all this, hope that at least it will help others. It must be really silly the way I have done it in the first place. I really need to learn the basics.

Great code by the way, thanks for that !!!
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 16, 2007, 08:24 pm
I really enjoying your technique !!! It's brilliant !!!  :D
It's going to work perfectly for our project. If you are in the UK and not far from Brighton at the beginning of November, It would be a pleasure to see you at the private view. Here is a link to the website for more info : http://www.sonicbody.co.uk

Thanks again.
Title: Re: Arduino as Capacitive Sensor
Post by: Digger450 on Sep 16, 2007, 11:06 pm
If you're curious, the reason it didn't work in your first picture is because you basically bypassed the resistor and your white wire and simply had 8 and 9 connected to each other.
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 17, 2007, 01:34 pm
Hi, I've been thinking about what you said. Is it because the two legs of the resistor are on the same line so the two legs are connected together instead of having the two legs separated?
Anyway,Thanks for your previous explanation.
Title: Re: Arduino as Capacitive Sensor
Post by: Digger450 on Sep 17, 2007, 03:53 pm
That is correct.  Good luck with your project  :)
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 24, 2007, 11:04 am
Hi

I've been trying to do this but I can't get it to work.. Wired everything up as on the photo and then ran the program but nothing happened. Can you give a description on how to do this currectly since I apperently do something wrong.

thanx

//Mads
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 25, 2007, 11:22 pm
Hi, did you find what was wrong ?
I am not a expert so I can't really help.
Are you following the second picture?
Maybe your resistor is not the good one, if it's to low it won't do anything.
Maybe your sensor is wrong. Try with copper or aluminium foil ( like in the one in your kitchen ).

Also, I used the commented part of it instead of the 'PORTB' thing.
Here is my code, it's really a quick one just for testing. The 'activation' thing with untouched and store as to be better. I am wondering between the difference between two person or a same person after a different day. It seems that somedays I have more electricity in my body then other days.
I haven't test with the original code again as I am looking into the multiplexer now.

Code: [Select]
// CapSense.pde
// Paul Badger 2007

int  i;
unsigned int x, y;
float accum, fout, fval = .07;    // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter
int ledPin = 5;   // select the pin for the LED
float nonTouched, store = 0;

void setup() {
 Serial.begin(9600);

 //DDRB=B101;     // DDR is the pin direction register - governs inputs and outputs- 1's are outputs
 // Arduino pin 8 output, pin 9 input, pin 10 output for "guard pin"
 //  preceding line is equivalent to three lines below
 pinMode(8, OUTPUT);     // output pin
 pinMode(9, INPUT);      // input pin
 pinMode(10, OUTPUT);    // guard pin
 digitalWrite(10, LOW);  //could also be HIGH - don't use this pin for changing output though
 pinMode(5, OUTPUT);

 analogWrite(ledPin,255);
 delay(10);
 analogWrite(ledPin,0);
}

void loop() {
 y = 0;        // clear out variables
 x = 0;

 for (i=0; i < 4 ; i++ ){       // do it four times to build up an average - not really neccessary but takes out some jitter

     // LOW-to-HIGH transition
   //PORTB = PORTB | 1;                    // Same as line below -  shows programmer chops but doesn't really buy any more speed
   digitalWrite(8, HIGH);    
   // output pin is PortB0 (Arduino 8), sensor pin is PortB1 (Arduinio 9)                                    

   //while ((PINB & B10) != B10 ) {        // while the sense pin is not high
   while (digitalRead(9) != 1) {    // same as above port manipulation above - only 20 times slower!                
     x++;
   }
   delay(1);

   //  HIGH-to-LOW transition
   // PORTB = PORTB & 0xFE;                // Same as line below - these shows programmer chops but doesn't really buy any more speed
   digitalWrite(8, LOW);              
   //while((PINB & B10) != 0 ){            // while pin is not low  -- same as below only 20 times faster
   while(digitalRead(9) != 0 ) {     // same as above port manipulation - only 20 times slower!
     y++;  
   }

   delay(1);
 }

 fout =  (fval * (float)x) + ((1-fval) * accum);  // Easy smoothing filter "fval" determines amount of new data in fout
 accum = fout;
 store++;
 if (store<50) {
   nonTouched = fout;
 }
 if (store>50){
   store = 50;
 }


 Serial.print((long)x, DEC);    // raw data - Low to High
 Serial.print( "   ");
 Serial.print((long)y, DEC);    // raw data - High to Low
 Serial.print( "   ");
 Serial.print( (long)fout, DEC); // Smoothed Low to High
 Serial.print( "   ");
 Serial.println( (long)nonTouched, DEC); // Smoothed Low to High
 if (fout>nonTouched+3){
   analogWrite(ledPin,255);
 }
 else{
   analogWrite(ledPin,0);
 }
}


Apart from that can't really help. Good luck
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 26, 2007, 05:34 pm
Looks cool.. :-)

And thanx for the help so far.. But I have one problem and that is that the file is to big to fit on the Arduino board. Why can this be??

I will tell you more about the process of the project later...

Thanx

//Mads
Title: Re: Arduino as Capacitive Sensor
Post by: westfw on Sep 26, 2007, 05:48 pm
Quote
the file is to big to fit on the Arduino board. Why can this be??


The floating point used will presumably add quite a bit of code to a sketch...
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 27, 2007, 09:37 am
So what can I do about  that? I'm really newbie to this.. Learning as I go..

Thanx for your time and help
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 27, 2007, 10:22 am
Strange, maybe you have a older version of the arduino. I looked at mine and I still have plenty of space left after uploading the code. Weird.
It could the float variable like westfw said but that I would find this strange. That would mean you are very very limited then. Try without the two variable I added, so at least you could see something in the Serial Monitor if that's the problem. It's the button next to the Upload one. Upload your code, when done click this button and wait a few second.

Code: [Select]

// CapSense.pde
// Paul Badger 2007

int  i;
unsigned int x, y;
float accum, fout, fval = .07;    // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter
int ledPin = 5;   // select the pin for the LED

void setup() {
 Serial.begin(9600);

 //DDRB=B101;     // DDR is the pin direction register - governs inputs and outputs- 1's are outputs
 // Arduino pin 8 output, pin 9 input, pin 10 output for "guard pin"
 //  preceding line is equivalent to three lines below
 pinMode(8, OUTPUT);     // output pin
 pinMode(9, INPUT);      // input pin
 pinMode(10, OUTPUT);    // guard pin
 digitalWrite(10, LOW);  //could also be HIGH - don't use this pin for changing output though
 pinMode(5, OUTPUT);

 analogWrite(ledPin,255);
 delay(10);
 analogWrite(ledPin,0);
}

void loop() {
 y = 0;        // clear out variables
 x = 0;

 for (i=0; i < 4 ; i++ ){       // do it four times to build up an average - not really neccessary but takes out some jitter

     // LOW-to-HIGH transition
   //PORTB = PORTB | 1;                    // Same as line below -  shows programmer chops but doesn't really buy any more speed
   digitalWrite(8, HIGH);    
   // output pin is PortB0 (Arduino 8), sensor pin is PortB1 (Arduinio 9)                                    

   //while ((PINB & B10) != B10 ) {        // while the sense pin is not high
   while (digitalRead(9) != 1) {    // same as above port manipulation above - only 20 times slower!                
     x++;
   }
   delay(1);

   //  HIGH-to-LOW transition
   // PORTB = PORTB & 0xFE;                // Same as line below - these shows programmer chops but doesn't really buy any more speed
   digitalWrite(8, LOW);              
   //while((PINB & B10) != 0 ){            // while pin is not low  -- same as below only 20 times faster
   while(digitalRead(9) != 0 ) {     // same as above port manipulation - only 20 times slower!
     y++;  
   }

   delay(1);
 }

 fout =  (fval * (float)x) + ((1-fval) * accum);  // Easy smoothing filter "fval" determines amount of new data in fout
 accum = fout;

 Serial.print((long)x, DEC);    // raw data - Low to High
 Serial.print( "   ");
 Serial.print((long)y, DEC);    // raw data - High to Low
 Serial.print( "   ");
 Serial.println( (long)fout, DEC); // Smoothed Low to High

}



Good Luck.
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 30, 2007, 03:23 pm
I fixed the problem with size.. Was working in an old version of Arduino - so now it's no prob.
But I don't seem to get any signal, numbers or anything out..
(http://koser.dk/mads/arduino.jpg)
What could be the problem?

Thanx for your time
Title: Re: Arduino as Capacitive Sensor
Post by: TM on Sep 30, 2007, 06:05 pm
I think you have wired correctly. Do it like on the second picture. On yours, the pin8 and 9pin are not connected as they are on 2 seperated parts. The 2 two parts are not connected. See what I mean?
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 30, 2007, 06:36 pm
I'm not sure I understand the last part you write.. But I tryed using pin13 and the small LED blinks..  :)
How would I print the exact signal/numbers that is recieved - I need to use these numbers..

Thanx again

//Mads
Title: Re: Arduino as Capacitive Sensor
Post by: madskoser on Sep 30, 2007, 06:41 pm
I got this error when I exported to the board::

Error inside Serial.<init>()

gnu.io.PortInUseException: Unknown Application

     at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)

     at processing.app.Serial.<init>(Serial.java:127)

     at processing.app.Serial.<init>(Serial.java:72)

     at processing.app.Uploader.flushSerialBuffer(Uploader.java:67)

     at processing.app.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:69)

     at processing.app.Sketch.upload(Sketch.java:1699)

     at processing.app.Sketch.exportApplet(Sketch.java:1761)

     at processing.app.Editor$42.run(Editor.java:1955)

     at java.awt.event.InvocationEvent.dispatch(Unknown Source)

     at java.awt.EventQueue.dispatchEvent(Unknown Source)

     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

     at java.awt.EventDispatchThread.run(Unknown Source)

java.lang.NullPointerException

     at processing.app.Serial.setDTR(Serial.java:480)

     at processing.app.Uploader.flushSerialBuffer(Uploader.java:76)

     at processing.app.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:69)

     at processing.app.Sketch.upload(Sketch.java:1699)

     at processing.app.Sketch.exportApplet(Sketch.java:1761)

     at processing.app.Editor$42.run(Editor.java:1955)

     at java.awt.event.InvocationEvent.dispatch(Unknown Source)

     at java.awt.EventQueue.dispatchEvent(Unknown Source)

     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

     at java.awt.EventDispatchThread.run(Unknown Source)



What can this mean?
Title: Re: Arduino as Capacitive Sensor
Post by: mellis on Oct 07, 2007, 06:59 pm
It could mean a few things.  Is anything else running that might be using the serial port?  Have any over-aggressive firewalls?  Did you select the right serial port from the Tools > Serial Port menu?
Title: Re: Arduino as Capacitive Sensor
Post by: drh on Apr 18, 2008, 07:23 pm
I have a question about adding more capacitive touch sensors through a multiplexer. I'd like to add a bunch more sensors providing continuous data (i.e. not digital on-off buttons). The tests I've made make me concerned that I won't be able to get smooth, consistent data when reading this through a multiplexer (MC14067). I understand that with wires dangling and a breadboard one can pick up stray capacitance so it's not the best test but my tests have led me to be concerned anyway and I thought I'd ask if anyone can confirm the possibility (or impossibility) of this technique before I go through all the effort of getting a PCB made.

Here's the situation:

1) Connecting a sensor and the resistor directly to the Arduino pins produces smooth, continuous data as my finger moves across the sensor (as long as there's some insulation over the copper - scotch tape has worked fine for my prototypes).

2) Connecting the sensor and resistor into a breadboard and then on to the Arduino also produces smooth, continuous values (though slightly different than number 1).

3) Connecting the pins through a multiplexer to read multiple sensors produces approximately accurate results but the values jump around a bit. That is, it won't be completely random but with a constant finger position it will jump back and forth within a small range. Too much change to use smoothing in the code I think.


The fact that both 1 and 2 work well leads me to believe that it's the multiplexer, not the breadboard/dangling wires that is causing the problem.

Anyone have any thought on how/if this setup could work?

Thanks.







Title: Re: Arduino as Capacitive Sensor
Post by: drh on Apr 18, 2008, 07:28 pm
P.S. Thanks for sharing this cool technique!
Title: Re: Arduino as Capacitive Sensor
Post by: drh on Apr 18, 2008, 09:08 pm
P.P.S - Well, I just figured something out. If I reverse the order of the HIGH-to-LOW and LOW-to-HIGH transition sections, I get much smoother results. Still not quite as smooth as without the multiplexer, but I'm more optimistic about getting this to work.

Any further thoughts would still be appreciated.

Thanks.
Title: Re: Arduino as Capacitive Sensor
Post by: ckiick on Apr 21, 2008, 05:09 pm
Hi,
 A guy named Terry Fritz did some work on using multiple capacitance sensors together. It's not exactly an answer to the multiplexer question, but it could be helpful.  I'm not techy enough to know exactly how, but he found a way to clean up and amplify noisy signals from the antennas.
Check it out: http://thereminvision.com/version-2/TV-II-index.html

Title: Re: Arduino as Capacitive Sensor
Post by: gennaroserra on Jul 29, 2008, 12:53 pm
:) :) :) :)

I've potentially saved a HELL of a lot of money coming across this forum! I'm wanting to toggle a switch like you would with one of those tactile lamps that you touch a few times to get different degrees of intensity. What I'd really, REALLY appreciate would be if someone pointed me in the right direction for achieving this using Max/MSP as the programming language (I'm using 'simplemessagesystem' to interface the Arduino with Max). Would it be too much of a challenge?

Thanks for your time,

Gennaro

:) :) :) :)
Title: Re: Arduino as Capacitive Sensor
Post by: sillyrabbit on Mar 11, 2009, 12:29 pm
Hi all,

I'm a rookie to this Arduino game but I got this code working and it suits my project really well. However, I don't need to read various degrees of pressure, I merely need an on/off register. I know I need to add a boolean but I have no idea where... Any ideas?

Any help is a plus :)
Title: Re: Arduino as Capacitive Sensor
Post by: Per_S on Mar 25, 2009, 11:26 am
I am really interested in to get touch sensiting working together with max/msp, has anyone did that?
I am using the arduino2Max patch to get Arduino talk to max/msp.
Title: Re: Arduino as Capacitive Sensor
Post by: sillyrabbit on Apr 25, 2009, 01:05 am
hey,

i'm trying to do the same. i was initally using the capsense code but abandoned it because i couldn't seem to create multiply capacitive sensors from it. i'm not that hardcore a programmer you see.

instead, i found this code http://www.arduino.cc/playground/Code/CapacitiveSensor

which works perfectly in arduino, the numbers are outputting and i'm trying to use with the arduino2max patch but no joy. as the pins are declared in the arduino code, surely the max patch should read the digital pins? any help is really appreciated.

Cheers
Title: Re: Arduino as Capacitive Sensor
Post by: MiG on Apr 28, 2009, 12:08 pm
I'm currently building a capactive measurement system as part of my PHd, I've found, depending on what accuracy you want, that the best way is to use a sigma delta modulation technique. You can find this in Analog Devices AD774x series of Capacitive to Digital Converters. These chips go from one channel to 12 channels for mounting capacitive electrodes and have resolutions between 8 and 24 bits over full scale ranges of around 25pF with a bit of calibration, obviously, that gives a lot of sensitivity.

The chips communicate with a host through the i2c bus, so you ca use Arduino's wire library to get the data out of the slave chip (I sweated over a prototype function for a PIC i2c protocol for months when I found arduino!) and use that to manipulate the data anyway you want.

the only thing is that these chips are only available in TSSOP form so can be a bit of a pain in the arse to solder, however it's quite a nice solution as I am finding out.
Title: Re: Arduino as Capacitive Sensor
Post by: ohnoezitasploded on May 21, 2009, 02:36 am
What is the purpose of the guard pin (pin 10)?  The code seems to work just as well without it...
Title: Re: Arduino as Capacitive Sensor
Post by: paulb on Jun 01, 2009, 04:39 am
You are correct that the guard pin doesn't do much and can easily be omitted. This was a klugy way to implement a technique which is used with op amps and other high impedance (low current) circuits.

You can now use the [[http://www.arduino.cc/playground/Main/CapSense| capSense library]]  and use any pins you want. The results will vary a bit from pin to pin though.

But if you do want to make the wire insensitive and only the foil at the end sensitive, use shielded audio wire and ground the shield on the duino end only. (Make sure the shield on the other end doesn't touch the sensor, or it won't work.

Paul
Title: Re: Arduino as Capacitive Sensor
Post by: dentrado on Jun 29, 2009, 03:11 pm
What is the difference between this and http://www.arduino.cc/playground/Code/CapacitiveSensor? Is the one wire method only for direct touch, while this can sense a longer distance?
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Sep 18, 2009, 12:04 pm
bump*

This post is a bit old now but I have a question...

I have used this circuit and because I didn't have a large enough resistor, I connected up an LDR.

I then got a smoothed value of 0 and 4 when I touched it. When I covered up the LDR, producing a massive resistance it would give me values of 100-200 when I was ~1m away and then increase to just over 1000 when I walked up to it.

I am wanting to use this setup to sense people close to a project but I would like a normal resistor. Anyone got any ideas as to what kind of size/value I should be looking at?

Thanks,

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 15, 2009, 06:23 pm
hi! this code is fantastic!!  I am actually thinking of using this for my project so thanks!

i was wondering what the coding would be if i wanted to put an LED and have it blink when the wire is touched. Im not very code-savvy, so any suggestions would be awesome!
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 15, 2009, 07:00 pm
Ok, since I posted that, I have learnt a lot about all this!

It will be easy to sense touching a wire. Use the tutorial and serial read to see what values you get when you touch the wire. Then use 'if' commands for if it is this value or higher then light up the LED.

If you need any more help then just say :D

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 15, 2009, 07:06 pm
ok, this is going to sound a bit dumb, but i dont EXACTLY know what the coding should look like....and i have tried looking at the page that helps with coding, but i just get so nervous...

so i guess my next question would be what exactly is the right "lingo" of coding that i should enter? So working off the code here for instance:


Code: [Select]
Arduino as Capacitive Sensor
10.02.2007 at 03:57:38   Some experiments with the Arduino as a capacitive sensor. All it requires is a 10 M resistor and a piece of wire. I was able to sense a hand about four inches from my 1.5" sq aluminum foil sensor.

Included are some machine code and port manipulation and an easy smoothing filter.


// CapSense.pde
// Paul Badger 2007

// Fun with capacitive sensing and some machine code - for the Arduino (or Wiring Boards).
// Note that the machine code is based on Arduino Board and will probably require some changes for Wiring Board
// This works with a high value (1-10M) resistor between an output pin and an input pin.
// When the output pin changes it changes the state of the input pin in a time constant determined by R * C
// where R is the resistor and C is the capacitance of the pin plus any capacitance present at the sensor.
// It is possible when using this setup to see some variation in capacitance when one's hand is 3 to 4 inches from the sensors
// Try experimenting with larger sensors. Lower values of R will probably yield higher reliability.
// Use 1 M resistor (or less maybe) for absolute touch to activate.
// With a 10 M resistor the sensor will start to respond 1-2 inches away

// Setup
// Connect a 10M resistor between pins 8 and 9 on the Arduino Board
// Connect a small piece of alluminum or copper foil to a short wire and also connect it to pin 9

// When using this in an installation or device it's going to be important to use shielded cable if the wire between the sensor is
// more than a few inches long, or it runs by anything that is not supposed to be sensed.
// Calibration is also probably going to be an issue.
// Instead of "hard wiring" threshold values - store the "non touched" values in a variable on startup - and then compare.
// If your sensed object is many feet from the Arduino Board you're probably going to be better off using the Quantum cap sensors.

// Machine code and Port stuff from a forum post by ARP  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169088394/0#0



int  i;
unsigned int x, y;
float accum, fout, fval = .07;    // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter

void setup() {
Serial.begin(9600);

DDRB=B101;     // DDR is the pin direction register - governs inputs and outputs- 1's are outputs
// Arduino pin 8 output, pin 9 input, pin 10 output for "guard pin"
//  preceding line is equivalent to three lines below
//  pinMode(8, OUTPUT);     // output pin
//  pinMode(9, INPUT);      // input pin
//  pinMode(10, OUTPUT);    // guard pin
digitalWrite(10, LOW);  //could also be HIGH - don't use this pin for changing output though
}

void loop() {
y = 0;        // clear out variables
x = 0;

for (i=0; i < 4 ; i++ ){       // do it four times to build up an average - not really neccessary but takes out some jitter

 // LOW-to-HIGH transition
 PORTB = PORTB | 1;                    // Same as line below -  shows programmer chops but doesn't really buy any more speed
 // digitalWrite(8, HIGH);    
 // output pin is PortB0 (Arduino 8), sensor pin is PortB1 (Arduinio 9)                                  

 while ((PINB & B10) != B10 ) {        // while the sense pin is not high
   //  while (digitalRead(9) != 1)     // same as above port manipulation above - only 20 times slower!                
   x++;
 }
 delay(1);

 //  HIGH-to-LOW transition
 PORTB = PORTB & 0xFE;                // Same as line below - these shows programmer chops but doesn't really buy any more speed
 //digitalWrite(8, LOW);              
 while((PINB & B10) != 0 ){            // while pin is not low  -- same as below only 20 times faster
   // while(digitalRead(9) != 0 )      // same as above port manipulation - only 20 times slower!
   y++;  
 }

 delay(1);
}

fout =  (fval * (float)x) + ((1-fval) * accum);  // Easy smoothing filter "fval" determines amount of new data in fout
accum = fout;  

Serial.print((long)x, DEC);    // raw data - Low to High
Serial.print( "   ");
Serial.print((long)y, DEC);    // raw data - High to Low
Serial.print( "   ");
Serial.println( (long)fout, DEC); // Smoothed Low to High
}


what do i type in? and where?





Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 15, 2009, 07:27 pm
Ok first thing, please just modify your post and put the code in # (code brackets - takes up less room)

Have you tried that code on your arduino? I suggest you conect up the circuit and try that code. Just copy everything from '// CapSense.pde' downwards straight into the arduino IDE and upload it.

The resistor doesn't have to be 10M, anything near will do if you don't have a 10M.

Then use the serial monitor to work out what values you get 'normally' and when you are touching it.

Then at the start of the code you need to define your LED. Use pin 13 as it has an inbuilt LED (on newer boards).

So the code should look like this at the top:

Code: [Select]
int  i;
unsigned int x, y;
float accum, fout, fval = .07;    // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter

int ledPin =  13;    // LED connected to digital pin 13


[edit]You also need to define the pin, just stick it after defining serial in setup:
Code: [Select]
void setup() {
Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
    [/edit]

Then after
Code: [Select]
Serial.println( (long)fout, DEC); // Smoothed Low to High
you want to put an 'if' command.

Eg:

Code: [Select]
Serial.print((long)x, DEC);    // raw data - Low to High
Serial.print( "   ");
Serial.print((long)y, DEC);    // raw data - High to Low
Serial.print( "   ");
Serial.println( (long)fout, DEC); // Smoothed Low to High

if(x > 50)
 digitalWrite(ledPin, HIGH);
else
 digitalWrite(ledPin, LOW);
delay(20) //Delay for good measure! (not strictly necessary but might as well have it)
}//Last line of code - nothing after this!


Put the 'if' value as just below the lowest value you get when you touch the wire.

There may be a stupid error in that code but it should work.
Say if it doesn't work and i'll fix it  :)

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 15, 2009, 07:46 pm
alright! First off, thank you thank you so much for at least putting the effort! I really really appreciate it! So i have run the code with the revisions, and i get a small light up from the LED...

i believe the serial monitor is giving me the highest value of 4 when i touch the wire...and so i tried putting that code
Code: [Select]
if(x > 3)
 digitalWrite(ledPin, HIGH);
else
 digitalWrite(ledPin, LOW);


out of curiosity, how can i make the light more visible?

i really really appreciate all your help!
Title: Re: Arduino as Capacitive Sensor
Post by: PaulS on Nov 15, 2009, 09:02 pm
The LED should light up bright when the digitalWrite(ledPin, HIGH) function is called. If the led is dim, you either have too large a resistor in series with the LED, or you have not declared that the ledPin is an output pin.

Can you post your code, and describe (or show) the hardware setup?
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 15, 2009, 09:36 pm
Quote
alright! First off, thank you thank you so much for at least putting the effort! I really really appreciate it! So i have run the code with the revisions, and i get a small light up from the LED...

i believe the serial monitor is giving me the highest value of 4 when i touch the wire...and so i tried putting that code

Ok, yes my value was a bit high then but it changes with the resistor. If you put in a larger value resistor then you get much bigger numbers and you can sense proximity to the sensor. This varies quite bit though due to where your body is/who else is in the room!

Glad the code works for you.

Quote
out of curiosity, how can i make the more visible?

i really really appreciate all your help!


If you change the ledPin to another pin, 8 for example then you can put in your own LED with a resistor from that pin to ground (Gnd - next to pin 13). The on board LED cannot be any brighter, it is just there as an indicator for testing.

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 15, 2009, 09:38 pm
Quote
The LED should light up bright when the digitalWrite(ledPin, HIGH) function is called. If the led is dim, you either have too large a resistor in series with the LED, or you have not declared that the ledPin is an output pin.

Can you post your code, and describe (or show) the hardware setup?


Oh come on, RTT (read the thread). It has been stated clearly that the on board LED (13) is being used and therefore it cannot be made brighter.

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 16, 2009, 01:10 pm
thanks for all the help!

hopefully this will work to turn on a projector! I was planning on using an IR light in place of the LED and when the wire was touched, the IR light would flash, and would turn on the pico 2 projector....

just wish the IR light was stronger....
Title: Re: Arduino as Capacitive Sensor
Post by: PaulS on Nov 16, 2009, 02:57 pm
How do you know that the IR light isn't "bright" enough? You can't see it.
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 16, 2009, 03:27 pm
the IR LED usually is a bright bright flash...but when i touch the wire, there is a dimmer glow
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 16, 2009, 04:03 pm
Quote
the IR LED usually is a bright bright flash...but when i touch the wire, there is a dimmer glow


if you are still using it on pin 13 then move it to a different pin. Pin 13 already has resistor on it which may not be suitable for the LED you are using/have. Make sure you put in a suitable resistor for the LED if you do move it to another pin though.

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: haukmoon269 on Nov 16, 2009, 04:14 pm
@Mowcius

OMG IT WORRRRKS!!! The light is soo much stronger!!! Thank you thank you!!!
:D

now the next obstacle i must tackle is the following:

I need to make this IR light able to turn on a Pico 2 projector. Now i discovered that the projector comes with an IR remote control. So i intend to replace that remote and use this device i just built for that.  I also discovered that the IR remote that comes with the projector is MP4....would that be something of use?

also, i think i found code for the "IR" tv translation  and looks like this:
Code: [Select]
brand(PHILIPSTV) ;
button(POWER,2) ; // hello world
delay(15000) ;
button(ZERO,2) ; // change channel to 03
delay(100) ;
button(THREE,2) ;
delay(100) ;
button(MUTE,2) ; // mute the TV
delay(15000) ;
button(POWER,2) ; // over and out


I know the brand isnt right, but does arduino have a code for MP4? and if so, where would i put it?
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Nov 16, 2009, 04:50 pm
Quote
OMG IT WORRRRKS!!! The light is soo much stronger!!! Thank you thank you!!!

Well I wouldn't have said it if I thought it wouldn't work  ;)

Quote
I need to make this IR light able to turn on a Pico 2 projector. Now i discovered that the projector comes with an IR remote control. So i intend to replace that remote and use this device i just built for that.  I also discovered that the IR remote that comes with the projector is MP4....would that be something of use?

also, i think i found code for the "IR" tv translation  and looks like this:

Ok, I'm afraid that my knowledge does not extend very far into IR stuff.
Title: Re: Arduino as Capacitive Sensor
Post by: Chris_D. on Dec 15, 2009, 12:57 am
I would like to try this, but a bit about my application.

I'm trying to create a midi bend up and midi bend down plate on either side of the right thumb for an "Electronic Wind Instrument" application.

What I need is for each of those sensors to return a value of 0 - 128) smoothly, as a function of how much skin is touching a piece of copper about a 1/2" square.

No skin contact needs to equal 0, and then as the thumb is rolled slowly onto the capacitance sensor, I need a smooth rise up to 128.

Is this even possible?  Because I will not investigate this further if it is not even possible.

CD

Thanks.

Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Dec 15, 2009, 01:21 pm
It is relatively easy to measure the distance of someone's hand away from the sensor and map that value from 0-128 but it is not very easy to do it for the amount of skin touching the 'pad'. The capacitance measured would be very small differences so it would need to be very accurate. This would then change from person to person and humidity etc.

I think you may be better off with a force sensor...

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: slimendee on Aug 04, 2010, 04:49 pm
Hi,
I was to ask please how do I get the output to an LED or how do I observe the output
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Aug 04, 2010, 10:16 pm
This looks remarkably like spam. Maybe we'd better watch it.

If not, then I don't understand the question and this isn't the right place to post it.
Title: Re: Arduino as Capacitive Sensor
Post by: slimendee on Aug 10, 2010, 06:03 pm
Hi,
I have done this experiment on capacitive sensing using the available codes on this page. Can anyone tell me how to convert the signals to the motion of a cursor on my computer so that when I move my hand around the cooking foil (sensors) the cursor will also move. Same as  a mouse motion :)
Title: Re: Arduino as Capacitive Sensor
Post by: Bobnova on Aug 19, 2010, 06:04 am
I just want to say thank you!
I've been playing with this, it's quite interesting taking measurements of various things and testing different sized sensors.
Eventually it'll find it's way into a paranoid robot that runs away when you try to pick it up :D
Title: Re: Arduino as Capacitive Sensor
Post by: slimendee on Aug 19, 2010, 07:09 pm
Bobnova,
please can you give me an idea i don't have the slightest idea on how to make the magic work ;)
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Aug 19, 2010, 09:25 pm
Quote
please can you give me an idea i don't have the slightest idea on how to make the magic work


I'm sure you told me you had it working with the one sensor?

Then just add another, get it to send serial and then use some software on your computer to convert it into very innacurate mouse movements.

Mowcius
Title: Re: Arduino as Capacitive Sensor
Post by: slimendee on Aug 19, 2010, 10:11 pm
Ok thanks mowcius. where do i connect the second sensor to
Title: Re: Arduino as Capacitive Sensor
Post by: slimendee on Aug 19, 2010, 10:12 pm
sorry, i mean what pin on the ardiuno do i connect the second sensor
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Aug 19, 2010, 10:47 pm
Which ever pin you like.

You then just have to add it in the code.
Title: Re: Arduino as Capacitive Sensor
Post by: julcsi20 on Dec 05, 2010, 04:45 pm
Hello all,

I am currently working with the CapSense library to create capacitive sensors with Arduino. I managed to make some sensors (made of copper wire) work but I will need 16 sensors (there are no enough digital pins for that). Does anyone have experience with using a 4051 or 4067 analog multiplexer with capacitive sensing?

Thanks in advance.

PS. The sensors do not have to output super-precise values.
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Dec 05, 2010, 05:57 pm
Quote
there are no enough digital pins for that

There are. You can use the analog input pins as digital pins too.
Numbered 14-19
Title: Re: Arduino as Capacitive Sensor
Post by: julcsi20 on Dec 05, 2010, 09:36 pm
First of all, thanks for the fast answer.

It might be a solution, I'll try it with the analog pins.
There is only one drawback left: I still have to run a led driver that takes up 5 digital pins (13, 11, 20, 9, 4). Anyhow, I cannot avoid using a multiplexer, I hope someone has experience with that.
Title: Re: Arduino as Capacitive Sensor
Post by: mowcius on Dec 05, 2010, 09:45 pm
Quote
I hope someone has experience with that.

They're not too tricky but I would suggest using the LED driver through the multiplexer as the capsense library may be a pain to use through one.
Title: Re: Arduino as Capacitive Sensor
Post by: julcsi20 on Dec 05, 2010, 09:57 pm
Thanks, that might be worth a try:)