measuring capacity - water level measuring (CapitiveSense lib)


I want to create water level meter based on the measuring capacity.
Proof of concept is good, but now i have following problem:

  • when arduino is starting and sensors are connected (pins 8,9) values - sensor output is giving ~0.
  • when i disconnect the wiring to the pipes and connect it again (arduino powered up), it shows good value.

How do calibration in the code? What I want is to get correct number at the beginning with no manual disconnecting and connecting.

Hope i explained it well.


#include <stdlib.h>

#include <CapacitiveSensor.h>

 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
CapacitiveSensor   cs_8_9 = CapacitiveSensor(9,8);     // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

void setup(void) {

void loop(void) {
    long start = millis();
    long total1 =  cs_8_9.capacitiveSensor(30 );
    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

Serial.print("Capacity: ");
   Serial.print(total1);   // print sensor output 1

capacities sensor - pipe and rod inside.
1 wire to the rod, 1 wire to the pipe (on the picture are 2 more wires, but they are not connected)

ok. I have to use capacitiveSensorRaw instead of capacitiveSensor.

long total1 = cs_7_8.capacitiveSensor(30 );
long total2 = cs_8_9.capacitiveSensorRaw(30 ); //-> this will get results

or is there any better solution?

where is your resistor ??
and you need to know the full bucket value and the empty , one
don’t understand it realy ?
maybe this sort things out ,

there is still a load on your rod i think , and those wires :stuck_out_tongue_closed_eyes:
you can use them for measure
you have to explain better , what you are doing :wink:

#include <CapSense.h>
int feel =0;
int tot =0;
CapSense   cs_4_5 = CapSense(4,5);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

void setup()                    
   pinMode(13, OUTPUT);
   pinMode(8, OUTPUT);
   //cs_4_5.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example

void loop()                    
    long start = millis();
    long total1 =  cs_4_5.capSense(100);
    tot =map(total1,0,2000,10,255);
    if (total1 >= 200)feel ++;
    if (feel >=2)feel =0;   
    if (feel == 1)digitalWrite (13, LOW);
    if (feel == 0)digitalWrite(13,HIGH);
    if (feel == 0)Serial.println("LED ON"); 
    if (feel == 1)Serial.println("LED OFF");
    Serial.println (total1);
    Serial.println ("");
    Serial.println (tot);

What I’m doing is water level meter.
In closed tank i want to read level.
Ultrasonic sensor is not a solution - It’s not possible to seal sensor from water completely. So capacitance method is the choice.

Like here explained on the youtube: water level meter using a capacitive sensor - YouTube
and like here Capacitive Liquid Level Sensor « Souly Solar Rebuilds

like here
and here:

I’m using 2.5 MOhm resistor.

And about the constants - i will get empty and full capacity and set it.