A capacitive sensor is made up of a high resistance resistor (1M ohm in this example) and a piece of conductive material (aluminum foil in this case). It can be used as a touch sensor. You can either use it as something similar to an analog sensor and read a range of values from it, or as a switch. The sensor needs to be connected through a separate breadboard, otherwise electronic components on the shield can interfere with readings.
To create a project with a capacitive sensor you will need to use the CapacitiveSensor library.
Materials
- 1 Arduino Uno board
- 1 Education shield
- 1 breadboard
- 1 1M ohm resistor
- 3 jumper wires
- Aluminum foil
Instructions
- Attach the shield onto the top of the Arduino board.
- Using a separate breadboard, connect a 1M ohm resistor between digital pin 3 and digital pin 2.
- Make a capacitive sensor by cutting a 5cm x 5cm square of aluminum foil. Wrap one corner of a foil square to a loose jumper wire – the metal of the wire must contact the foil.
- Connect the loose jumper wire to digital pin 3 through the breadboard.
- Connect the board to the computer and upload example ‘CapacitiveSwitchTest’ or ‘CapacitiveSwitch’.
Read the values
Code
Find the code in Example>EducationShield>Help>CapacitiveSwitchTest
You should now open the serial monitor. The values printed should be close to 0 when you are not touching the capacitive sensor, and they should go a lot higher when you touch it.
Commands
-
#include <CapacitiveSensor.h>
– includes the CapacitiveSensor library. CapacitiveSwitch( senderPin, receiverPin )
– Creates aCapacitiveSwitch
object.- Parameters
- (
int
)senderPin
– The digital pin that acts as the sender pin. When using several capacitive sensors, the same sender pin can be used for all. -
(
int
)receiverPin
– The digital pin that acts as the receiver pin. When using several capacitive sensors, each sensor must be connected to an indivdiual receiver pin.
- (
- Parameters
-
test()
– Prints the sensor readings to the serial monitor. getValue(int minValue)
– Returns the read sensor value without printing it to the serial port.- Parameters
- (
int
)minValue
– The minimum value you want to be returned. Any value lower thanminValue
will be read as 0. Defaults to 0.
- (
- Return
- (
long
) – Sensor reading
- (
- Parameters
Troubleshooting
- If the reading is not normal, double check your connection. Take extra care of the connection between aluminum foil and the loose jumper wire, they lose contact easily.
- Make sure you have connected the sensor to the same digital pins as stated in your code. In CapacitiveSwitch(2,3) 2 is the sending pin and 3 the receiving pin. This means that the loose end should be connected to pin 3.
Switch
If you want to use the capacitive sensor as a switch, you need a threshold value. Write down the value you get when nothing touches the aluminum foil and the value you get when you touch it with a desired object.
Code
Find the code in Example>EducationShield>Help>CapacitiveSwitch
You should now open the serial monitor and follow the printed instructions by touching the capacitive sensor. Each time you execute an instruction the serial monitor should print ‘1’.
Commands
-
#include <CapacitiveSensor.h>
– includes the CapacitiveSensor library. CapacitiveSwitch( senderPin, receiverPin )
– Creates aCapacitiveSwitch
object.- Parameters
- (
int
)senderPin
– The digital pin that acts as the sender pin. When using several capacitive sensors, the same sender pin can be used for all. -
(
int
)receiverPin
– The digital pin that acts as the receiver pin. When using several capacitive sensors, each sensor must be connected to an indivdiual receiver pin.
- (
- Parameters
config( threshold )
– If the capacitive sensor is to be used as a switch, you need to use this function to configure the threshold. Usetest()
to check the sensor readings first. Then decide on a threshold value to use.- Parameters
-
(
int
)threshold
– Threshold for the capacitive switch to register a press.
-
- Parameters
pressed( timeout )
– checks if the sensor is pressed.- Parameters
- (
int
)timeout
– milliseconds within which the sensor should be pressed. Default is 0.
- (
- Return
-
(
boolean
) – Returns 1 if pressed within time, otherwise 0. Iftimeout
is not used, the program pauses here until the sensor is pressed.
-
- Parameters
released( timeout )
– checks if the sensor is released.- Parameters
- (
int
)timeout
: milliseconds within which the sensor should be released. Default is 0.
- (
- Return
-
(
boolean
) – Returns 1 if released within time, otherwise 0. Iftimeout
is not used, the program pauses here until the sensor is released.
-
- Parameters
Troubleshooting
- If the reading is not normal, double check your connection. Take extra care of the connection between aluminum foil and the loose jumper wire, they lose contact easily.
- Make sure you have connected the sensor to the same digital pins as stated in your code. In CapacitiveSwitch(2,3) 2 is the sending pin and 3 the receiving pin. This means that the loose end should be connected to pin 3.