CapacitiveSensor library not working properly?

This is my first post on this forum, so I hope I am doing this right.

I am getting some strange behavior from the CapacitiveSensor library. My setup is just the basic capacitive sensor setup like this:


where the sensor is a piece of metal behind some cardboard and my arduino is powered by my laptop. The connection to the laptop is not ideal as it affects the capacitance.
The minimal code to reproduce my problem:

#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(7, 5);

void setup() {
  Serial.begin(9600);
}

void loop() {
  float sensor = capSensor.capacitiveSensor(20);
  Serial.print("Value: ");
  Serial.println(sensor);
}

If I look at the serial plotter, then initially this seems to work as a capacitive touch sensor. However, The problem I am having is the following:

  • Initial sensor value is ~0
  • when I hold my hand in front of the metal plate the sensor value goes up to ~45
  • after holding it in front of the sensor for about 12 seconds it automatically drops back to ~0
  • it remains ~0 for as long as I can tell even though my hand is still in the same place in front of the sensor.

If I now remove my hand and place it back in front of the sensor then it does go back to ~45, but again after about 12 seconds it drops back to ~0 for no reason I can explain.

To make things even stranger I do not always get this behavior. Sometimes it works as it should.

Does anybody have an idea what the problem can be? Is there a bug in the CapacitiveSensor library?

Can we have a link to the library please? Sometimes there are more than one library with the same function and name.

Your description sounds like some kind of auto-calibration rather than a bug. Perhaps the library is not designed (or it is not even feasible) to detect persistent states vs. changes of state.

You made the touch sensor?

Oh, right, and... near any conductive surfaces? Pictures please...

Sorry, what does that mean?

Sounds like an antenna.
Paul

With a link to the library, doubtless some comments or links there would specify the hardware requirements. It's likely that you simply didn't follow them. That's why we are asking for pictures.

But we still don't have a link.

The link to the library I used is:
https://www.arduino.cc/reference/en/libraries/capacitivesensor/
This is the library which is used in Project 13 - Touch Sensor Lamp from the starter kit.

A picture of my setup:


There is a piece of metal where I am holding my hand. Things are hold in place with scotch-tape to prevent them from moving.

As far as I understand the library measures the capacitance of the circuit (see the documentation) and this capacitance is affected by any capacitance connected to the circuit. Here connected doesn't have to be an inductive connection but it can also be just a capacitive connection (I hope that makes sense). So the capacitance is different if I run it on a battery or if I connect it to my laptop. You can even see in the serial plotter whether your hands are on the keyboard of the laptop or not.

It is as I suspected - it is autocalibrating. Try:

#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(7, 5);

void setup() {
  Serial.begin(9600);
  capSensor.capacitiveSensor(1);  // allow one calibration
}

void loop() {
  float sensor = capSensor.capacitiveSensorRaw(20);
  Serial.print("Value: ");
  Serial.println(sensor);
}

Also consider this advice from the library documentation:
"Another solution that seems to have worked well on at least one installation, is to run a foil ground plane under the sensor foil (insulated by plastic, paper, etc.), and connected by a wire to ground. This worked really well to stabilize sensor values and also seemed to dramatically increase sensor sensitivity. "

Also I think the sensor wire is much too long. That's just a feeling, I can't elaborate much without trying it myself. It's based on viewing it as an antenna, as suggested in reply #6.

The library is not designed to provide linear long term readings. It's set up to detect hand movements.

Thank you for your reply. This solves the problem I was having.

I noticed some things after having played around a little bit more. I am just mentioning them in case somebody is interested.

If the line

capSensor.capacitiveSensor(1);  // allow one calibration

is present, then both

float sensor = capSensor.capacitiveSensorRaw(20);

and

float sensor = capSensor.capacitiveSensor(20);

work. However, if capSensor.capacitiveSensorRaw is used, then this line can also be left out.

Also something to note is that if my laptop is connected to the power, then the value of the capacitor shoots up and down at somewhat irregular intervals.