Capacitive Sensor

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

  1. Attach the shield onto the top of the Arduino board.
  2. Using a separate breadboard, connect a 1M ohm resistor between digital pin 3 and digital pin 2.
  3. 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.
  4. Connect the loose jumper wire to digital pin 3 through the breadboard.
  5. Connect the board to the computer and upload example ‘CapacitiveSwitchTest’ or ‘CapacitiveSwitch’.
Note: You can use multiple capacitive sensors on one Arduino. The sender pin (digital pin 2 in this case) can be shared.

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 a CapacitiveSwitch 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.

  • 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 than minValue will be read as 0. Defaults to 0.
    • Return
      • (long) – Sensor reading

Troubleshooting

  1. 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.
  2. 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.
Note: Make sure the two digital pins you use are between 2 and 8. With the Education shield on, digital pins 9, 10, 11, 12 or 13 are not usable for capacitive sensors.

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 a CapacitiveSwitch 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.

  • config( threshold ) – If the capacitive sensor is to be used as a switch, you need to use this function to configure the threshold. Use test() 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.

  • 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. If timeout is not used, the program pauses here until the sensor is pressed.

  • 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. If timeout is not used, the program pauses here until the sensor is released.

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.
Note: Make sure the two digital pins you use are between 2 and 8. With the Education shield on, digital pins 9, 10, 11, 12 or 13 are not usable for capacitive sensors.