Touch sensor using CapSense library in D1 mini

Hello,

I’m trying to use the capsense library to make a touch sensor in D1 mini.
I have 2 problems :

1- When I use high value resistors, like 1 mega, I always get -2, i.e., timeout.
2- When I use lower resistors, let’s say between 10k and 50k I’m able to sense the touching event (that’s all I need, touched or not touched). However, that work’s fine the the D1 mini is connected to the computer, but when I use a lipo battery I can’t detect the touch event, when I touch the surface (a conductive textile) the returned value doesn’t change, or if it does it, the difference is too short and unestable.

I know that using bigger resistors I should get more resolution, but as I said before, using big resistors ends up in a timeout…

The setup is this:
D7 —> resistors → conductive textile <---- D5

I also tried putting a small capacitor (100pf) to ground, its get more stable but I still can’t
get the desired resolution

The code:

#include <CapacitiveSensor.h>
CapacitiveSensor   cs_d7_d5 = CapacitiveSensor(13,14);      // pin d7 = 13 (arduino) : Sender;  pin d5 = 14 (arduino) : Receiver

int touched = 0;
int threshold;
int analogSensorValues[5];
int analogSensorValue = 0;
int openedSwitches = SWITCHES;


void initTouchSensor() {
  cs_d7_d5.set_CS_AutocaL_Millis(0xFFFFFFFF);
  threshold = TOUCH_SENSOR_THRESHOLD;
  touched = 0;
}

void resetAnalogSensorValues(){
  for (int i=0;i<PROXIMITY_SENSOR_SAMPLES;i++){
    analogSensorValues[i]=0;  
  }
  analogSensorValue = 0;
  openedSwitches = SWITCHES;
}

int updateAnalogSensorValues(int analogSensorValue){
  int average = 0;
  for (int i=0;i<PROXIMITY_SENSOR_SAMPLES-1;i++){
    analogSensorValues[i]=analogSensorValues[i+1];
    average+= analogSensorValues[i];
  }
  analogSensorValues[PROXIMITY_SENSOR_SAMPLES-1]=analogSensorValue;
  average+= analogSensorValues[PROXIMITY_SENSOR_SAMPLES-1];
  return average/PROXIMITY_SENSOR_SAMPLES;
}

int senseTouching() {
  long start = millis();
  long total = cs_d7_d5.capacitiveSensorRaw(30); //cs_d7_d5.capacitiveSensor(30);

  if(debug_touch_serial){
   Serial.print(millis() - start);        // check on performance in milliseconds
   Serial.print("\t");                    // tab character for debug windown spacing
   Serial.println(total);                  // print sensor output 1
  }
  
  int ret = TOUCH_FREE;
  if(total > threshold){
    ret = TOUCHED;
    if(!touched){
      touched = 1;
    }
  }else{
    if(touched){
      touched=false;
      ret = TOUCH_RELEASED; //was just released
    }
  }
  if(debug_touch_osc){
    sendDebugToController("capSense",total);  
  }
  return ret; 
 
}

Can you help me to improve the performance of this sensor? Is there any easier option to build a contact sensor (boolean) rather than using the capsense library?
Thank you!

Capacitive sensors stand and fall with proper grounding, as the ground is your reference.

A computer (often even a laptop, especially when connected to a power supply) provides a proper ground. A battery dpes not.

Thank you for your answer! So, it is not possible to use this kind of sensors with a battery? Do you know any other strategy to make a touch sensor that might work with a battery?

thanks

Will have to wait for others to chip in - I don't know if it is not possible with battery, but insufficient grounding is a very common problem with these sensors. Somehow you have to find a reference to measure your capacitance against.