My goal is to detect when I touch a piece of metal so it can function as a button. I want to do this with capacitive sensing, but am having trouble getting it to work on my Arduino Uno WiFi Rev 2 board.
I am using the CapacitiveSensor Library. I have tried other libraries (ADCTouch, ADCTouchSensor, and AnalogTouch) but their example sketches show up as being incompatible in the examples menu.
The CapacitiveSensor library uses a "send pin" to change the voltage on a "receive pin" through a high-value resistor and determines changes in capacitance on the receive pin by how long it takes to change voltage. If it takes too long (two seconds by default), the library returns "–2". In my case, it always returns "–2".
The code below returns two numbers: Time to run loop (ms), Capacitance in arbitrary units (or error code)
Troubleshooting steps:
Board is fully updated with the latest firmware.
Running the latest version of the IDE (version 1.8.14) and the CapacitiveSensor library (0.5.1) on a Mac (Catalina 10.15.6).
Upload the simplified code posted below, no errors.
Connect a 1 MΩ resistor between the send and receive pins. Touch the lead going into the receive pin. No change in output. Output is: 1367 ms, –2.
I have tried lower resistor values, as suggested on the library page, as low as 250 kΩ. No change.
The code works properly on an Arduino Uno with a 1 MΩ resistor. When not touching it, the output is 1 ms, 0. When touching lightly, it returns 1 ms, 100. When pressing harder, it returns 22 ms, 10,000.
Other code on the Arduino Uno WiFi Rev 2 (target board) using the same pins works fine.
The Uno Wifi Rev 2 has an ATMEGA4809. The Arduino IDE has an option for emulating the ATMEGA 328 register. I get the same result with this option enabled or disabled.
I have tried numerous pin combinations, with the same results: 2 and 4, 4 and 8, 2 and 8, 3 and 9, A0 and A5, A1 and A4.
I have a proper earth ground. Regardless, the setup was the same with the Uno, which worked fine.
Nothing is connected to any other pins on the WiFi board.
I would be very grateful for any suggestions! This is also my first time posting, so please let me know if I've made any mistakes. Many thanks.
#include <CapacitiveSensor.h>
// 1M resistor between pins 4 & 8, pin 8 is sense pin
CapacitiveSensor cs_test = CapacitiveSensor(4,8);
void setup()
{
Serial.begin(9600);
}
void loop()
{
long start = millis();
long touch = cs_test.capacitiveSensor(30);
Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug windown spacing
Serial.println(touch); // print sense pin 8
delay(10); // delay to limit data to serial port
}
DrDiettrich:
Do not touch the pads directly! Cover them e.g. with a plastic foil.
Or put a 22 nF capacitor in series with the touch plate.
Easier to use a chip designed for the function, specifically a TTP223 module.
You connect your metal plate (insulated, or via the capacitor) to the otherwise unconnected corner pad with the "U"-shaped mark around it.
Note that your circuit ground should be connected to either a mains power supply or a large metal object nearby.
Earth connection is not required for capacitive sensing. Looking at your sensor picture It's sufficient to have a ground plane around the sensor, so that a finger can make capacitive connection between both lines.
Thanks for your suggestion DrDiettrich. Due to the setup for my project, I prefer to touch the metal directly; however, if I am able to get the CapacitiveSensor library working, I will isolate the metal touch point from the sense pin with a series-connected capacitor and resistor.
Paul__B, I like the idea of using a dedicated chip, thanks! If I can't get the CapacitiveSensor library working, I'll go this route.
I suppose the resistor is unnecessary if I have a capacitor. I was considering the precautions taken in this example, but I think they only used a sense pin resistor since there was a chance of touching the bare metal.
If you are seriously concerned about ESD, you need to have diodes connecting from the touch plate to ground and to your supply voltage (5 V for the UNO). Preferably Schottky diodes.
Or connecting to the point where the Arduino connects to the series capacitor that isolates it from the touch plate.
Paul__B:
But it does not protect against positive voltages greater than 5 V. You need one between the same point and "5V" on the Arduino.
My apologies, I meant a Zener diode with a 5.1 V reverse breakdown voltage. That way the diode would clamp voltages lower than –0.7 V or higher than 5 V.
But yes, two Schottky diodes would provide superior protection due to the lower turn-on voltage.