Download Capacitive Sensor Worksheet

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Power electronics wikipedia , lookup

Transistor–transistor logic wikipedia , lookup

Surge protector wikipedia , lookup

Spark-gap transmitter wikipedia , lookup

Power MOSFET wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Valve RF amplifier wikipedia , lookup

Test probe wikipedia , lookup

Analog-to-digital converter wikipedia , lookup

Current mirror wikipedia , lookup

Operational amplifier wikipedia , lookup

Schmitt trigger wikipedia , lookup

Oscilloscope history wikipedia , lookup

Trionic T5.5 wikipedia , lookup

Integrating ADC wikipedia , lookup

Charlieplexing wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Rectiverter wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Capacitive Touch Sensor (Experimental)
CBPrice March 2015.
Constructing a Sensor Pad
1. Take a thin piece of card and cut out a square of ali-foil, say 3cm x 3cm. Glue the foil to the paper. Now
bare a wire and lay the bare end over the foil. Tape this in place. Here’s a diagram if you really get lost; the
view through the sensor is shown on the left and the top view on the right.
2. Connect the wire on the protoboard to one end of a 1MegOhm resistor. Connect the other end of the
resistor to your 5V column on the protoboard. Connect the first end of the resistor to the Arduino analog
input number 2. Here’s the circuit diagram.
How the Sensor Works
When a capacitor is charged through a resistor, it charges quickly if the capacitance is small and it charges
slowly if the capacitance is large.
When you put your finger near the pad, you really increase its capacitance.
Here’s the details of how the sensor works. Let’s assume the capacitor has been charged by current flowing
down through the resistor. So it looks like this, shown in Fig.1
1. First we must discharge the capacitor. To do this we configure pin 2 as an output and drive it LOW
effectively connecting the capacitor to ground. The capacitor then discharges as shown in Fig.2(a) charge
flowing (“current”) from the top plate of the capacitor to ground. Fig 2(b) shows the capacitor totally
discharged.
2. Now we configure pin 2 as an input. The capacitor will start to charge up through the resistor, as current
flows down from 5 Volts to ground. This is shown in Fig.3. since pin 2 is now an input it will have no effect
on the charging up, it can only measure the charge (and therefore voltage) on the capacitor. Fig.3(a) shows
the uncharged capacitor starting to charge, Fig.3(b) shows the capacitor partially charged and Fig. 3(c)
shows the capacitor fully charged.
3. As the capacitor charges up, the charge on the capacitor (and therefore its voltage) increases. This is
monitored by the input pin 2, and the Arduino progam is able to sense when the capacitor is fully charged.
So how does this make the capacitive sensor work?
4. Well look at Fig. 4 which shows how the charge and the voltage on the capacitor changes with time. In
Fig.4(a) you can see what happens when the value of the capacitor is small. This corresponds to your finger
not close to the sensor. Fig.4(b) shows what happens when your finger is close; the value of the capacitor is
large so the speed of charging is slower
You can clearly see the difference in rate of the voltage rise (the red line). When the capacitor is nearly fully
charged, its voltage is close to 5 Volts (green dotted line). But when the finger is not close, this happens at
an earlier time than when the finger is close. So by measuring this time we can decide if the finger is close
or it isn’t.
We can measure this time using a simple for-loop. We just keep on reading the voltage on pin 2 and when it
is close to 5V we store the index of the for loop when this happens which gives us the time. Details are
presented in the section below, but here’s some pseudo-code for starters:
set for loop index to 0
start a loop
measure the voltage on pin 2
if this voltage is close to 5V then break out of the loop and return the loop index at this point
increment the loop index
end the loop
So when we break out of the loop, the returned value (which is the loop index when the voltage is close to
5V) gives us a measure of the time taken for the capacitor to charge.
Details of Programming the Sensor
1. First Set Arduino bits 2 to 7 as inputs and leave bits 1 and 0 untouched. Remember these two bits are used
by the USB interface to the PC so they must not be changed. Setting bits 2 to 7 is a safety feature in case you
make an incorrect connexion to a pin.
To do this you have to write some bits to Port D’s Data Direction Register (DDRD). Here is the code,
DDRD = DDRD & B00000011;
Here is the effect on DDRD. Due to the AND operation, only bit 2 is set to zero. This follows from (oh no)
Boolean Algebra expressions X.0 = 0 and X.1 = X.
Bit
7
6
5
4
3
2
1
0
DDRD
X
X
X
X
X
X
X
X
Constant
1
1
1
1
1
0
1
1
DDRD (new)
X
X
X
X
X
0
X
X
AND
2. Now set pin 2 to an output without changing the other pins. Again you need to change the DDR Here is
the code
DDRD = DDRD | B00000100;
Here is the effect on the DDRD. Due to the OR operation only bit 2 is actually set. This follows from the
Boolean expressions X + 0 = X, X + 1 = 1
Bit
7
6
5
4
3
2
1
0
DDRD
X
X
X
X
X
0
X
X
Constant
0
0
0
0
0
1
0
0
DDRD (new)
X
X
X
X
X
1
X
X
OR
3. Now set pin 2 to a LOW value. This will discharge the capacitor. To do this you need to write to the
PORTD register which will output to the PORTD pins:
PORTD = PORTD & B11111011;
The effect on PORTD follows on from the Boolean Expressions X.0 = 0 and X.1 = X and here’s the result
Bit
7
6
5
4
3
2
1
0
PORTD
X
X
X
X
X
X
X
X
Constant
1
1
1
1
1
0
1
1
PORTD (new) X
X
X
X
X
0
X
X
AND
4. Now we must make pin 2 an input. Again we use the DDRD with the following code
DDRD = DDRD & B11111011;
The effect on DDRD is as follows
Bit
7
6
5
4
3
2
1
0
DDRD
X
X
X
X
X
X
X
X
Constant
1
1
1
1
1
0
1
1
DDRD (new)
X
X
X
X
X
0
X
X
AND
5. Now the capacitor starts charging and we loop until it is charged, breaking out of the loop as soon as the
capacitor is charged
for(int i=0;i<10000;i++) {
if(PIND & B00000100) {
cycles = i;
break;
}
}
6. Finally we must set pin 2 to LOW without changing the others. Here is the code which requires two lines,
first to set the port pin 2 as an output and then to set pin 2 low
DDRD = DDRD | B00000100;
PORTD = PORTD & B11111011;
Adding more sensor pads
The code to do this is identical to the above, but you must modify the bit in the values Bxxxxxxxx to
select the input pin you wish to use. For example the input pin 3 is associated with bit 3 and so on.