Go Down

### Topic: Arduino as Capacitive Sensor (Read 64060 times)previous topic - next topic

#### mowcius

#30
##### 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

#### haukmoon269

#31
##### 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!

#### mowcius

#32
##### 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

Mowcius

#### haukmoon269

#33
##### Nov 15, 2009, 07:06 pmLast Edit: Nov 15, 2009, 07:36 pm by haukmoon269 Reason: 1
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 Sensor10.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#0int  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 filtervoid 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 pindigitalWrite(10, LOW);  //could also be HIGH - don't use this pin for changing output though}void loop() {y = 0;        // clear out variablesx = 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 foutaccum = fout;   Serial.print((long)x, DEC);    // raw data - Low to HighSerial.print( "   ");Serial.print((long)y, DEC);    // raw data - High to LowSerial.print( "   ");Serial.println( (long)fout, DEC); // Smoothed Low to High}`

what do i type in? and where?

#### mowcius

#34
##### Nov 15, 2009, 07:27 pmLast Edit: Nov 15, 2009, 07:30 pm by mowcius Reason: 1
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 filterint ledPin =  13;    // LED connected to digital pin 13`

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 HighSerial.print( "   ");Serial.print((long)y, DEC);    // raw data - High to LowSerial.print( "   ");Serial.println( (long)fout, DEC); // Smoothed Low to Highif(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

#### haukmoon269

#35
##### Nov 15, 2009, 07:46 pmLast Edit: Nov 16, 2009, 12:25 pm by haukmoon269 Reason: 1
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!

#### PaulS

#36
##### 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?

#### mowcius

#37
##### 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

#### mowcius

#38
##### Nov 15, 2009, 09:38 pmLast Edit: Nov 15, 2009, 09:38 pm by mowcius Reason: 1
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

#### haukmoon269

#39
##### 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....

#### PaulS

#40
##### Nov 16, 2009, 02:57 pm
How do you know that the IR light isn't "bright" enough? You can't see it.

#### haukmoon269

#41
##### 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

#### mowcius

#42
##### 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

#### haukmoon269

#43
##### Nov 16, 2009, 04:14 pmLast Edit: Nov 16, 2009, 04:16 pm by haukmoon269 Reason: 1
@Mowcius

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

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 worlddelay(15000) ;button(ZERO,2) ; // change channel to 03delay(100) ;button(THREE,2) ;delay(100) ;button(MUTE,2) ; // mute the TVdelay(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?

#### mowcius

#44
##### 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.

Go Up