Arduino IDE: RBD_Light Example Blink Without DelayNeed help writing code? You can hire Robots + Big Data to write Arduino software for your project! Click here to get started...
Arduino

Water SensorAPI DOCUMENTATION

Measure and calibrate water level sensors.

See Example  Public Methods  Release Notes
Overview

Measure and calibrate capacitance sensors to detect change in water levels from the outside of a plastic container. The example below uses a piece of aluminum foil, but you can attach the wire to anything conductive and calibrate the sensor accordingly.

This library measures relative capacitance so it is prone to interference. You can temporarily adjust the entire scale and then reset it when the capacitance disruption has stopped by using setModifier() and resetModifier(). Look at the Capacitance Library if you would like to know more about how capacitance is calculated.

Example Wiring
  1. Connect Arduino digital pin 2 to a 1MegΩ resistor
  2. Connect the other end of the 1MegΩ resistor to Arduino digital pin 3
  3. Connect Arduino digital pin 3 to a piece of conductive material (aluminum foil)
  4. Tape or glue the conductive material to a non-conductive water tight container
  5. Load the Example Sketch onto the Arduino
  6. Open a serial connection at 115200 baud to see the reported values
  7. Calibrate the water level sensor

A piece of aluminum foil taped to a plastic cup connected to a 10 Meg Ohm resistor that is bridged across Arduino digital pins 2 and 3.

Example SketchArduinoC++
#include <RBD_Capacitance.h>
#include <RBD_Threshold.h>
#include <RBD_WaterSensor.h>

RBD::WaterSensor water_sensor(2, 3, 3); // send, receive pin, levels

void setup() {
  Serial.begin(115200);
  water_sensor.setLevel(1, 120);
  water_sensor.setLevel(2, 154);
  water_sensor.setLevel(3, 187);
  water_sensor.setMaxLevel(220);
}

void loop() {
  water_sensor.update();

  if(water_sensor.onRawValueChange()) {
    Serial.print("Active Level: ");
    Serial.print(water_sensor.getActiveLevel());
    Serial.print("  ---  ");
    Serial.print("Raw Value: ");
    Serial.println(water_sensor.getRawValue());
  }
}
Example Sketch Calibration

Follow these steps to calibrate each water level sensor before use:

  1. Affix the water level sensor and pour water into the container
  2. Stop at each level and notice the Raw Value printed to the serial console
  3. Use setLevel() to add each level and Raw Value - in the example sketch setup()
  4. After assigning setMaxLevel(); pour out all water
  5. Load the calibrated sketch back into the Arduino
  6. The water level is calibrated and ready to use
Public Methods

You must continuously call update() inside of loop() in order to use theReal-Time methods tagged below.

RBD::WaterSensor

RBD::WaterSensor constructor(send_pin, receive_pin, level_count)

Pass in integers for the send and receive pins to create a new instance of this class, along with an integer for the total number of levels the water sensor will detect. Example: if you want to detect low, medium, and high levels then level_count should equal 3.

EXAMPLE SKETCH
RBD::WaterSensor water_sensor(2, 3, 3); // send, receive pin, levels

void setup() {
  ...
}
water_sensor
.setSampleSize(value)

Pass in an integer to change the number of readings taken to calculate the moving average getRawValue(). This can be called inside of setup() or also safely at runtime inside of loop().

  • Decrease Variation in readings by making this number larger: 5000
  • Increase Variation in readings by making this number smaller: 100
  • Default Value: 1000
EXAMPLE SKETCH
void setup() {
  water_sensor.setSampleSize(250);
}
water_sensor
.setLevel(index, raw_value)

Provide an integer for the level index and a raw value from getRawValue(). The level index starts at one (it is not zero-based). You must also call setMaxLevel() at the end to set an upper bounds of the last level.

EXAMPLE SKETCH
void setup() {
  water_sensor.setLevel(1, 120);
  water_sensor.setLevel(2, 154);
  water_sensor.setLevel(3, 187);
}
water_sensor
.setMaxLevel(raw_value)

Provide an integer from getRawValue() to set the upper-bounds threshold of the last level.

EXAMPLE SKETCH
void setup() {
  water_sensor.setMaxLevel(220);
}
water_sensor
.update()Update

Keep processing the readings and move this library along in real-time.

EXAMPLE SKETCH
void loop() {
  water_sensor.update();
}
water_sensor
.getValue()Real-Time

Returns the capacitance sensor reading after being adjusted with the value given to setModifier().

This will return getRawValue() if you have not used setModifier() yet, or if you have called resetModifier().

EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onValueChange()) {
    Serial.println(water_sensor.getValue());
  }
}
water_sensor
.getRawValue()Real-Time

Returns the raw capacitance sensor reading and ignores any value that has been passed to setModifier().

EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onRawValueChange()) {
    Serial.println(water_sensor.getRawValue());
  }
}
water_sensor
.getActiveLevel()Real-Time

Returns the current water level.

  • 0 means there is no water
  • 1 means the water is touching the first level
  • 2 means the water is touching the second level
  • n means the water is touching the nth level
  • sizeof(n) + 1 means the water is above the max level
  • -1 if the value was not found
EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onActiveLevelChange()) {
    Serial.println(water_sensor.getActiveLevel());
  }
}
water_sensor
.onValueChange()Real-TimeEvent

This method will return true once the sensor getValue() changes. It will then return false until the reading changes to a different value again.

EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onValueChange()) {
    // code only runs once per event
    Serial.println(water_sensor.getValue());
  }
}
water_sensor
.onRawValueChange()Real-TimeEvent

This method will return true once the sensor getRawValue() changes. It will then return false until the reading changes to a different value again.

EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onRawValueChange()) {
    // code only runs once per event
    Serial.println(water_sensor.getRawValue());
  }
}
water_sensor
.onActiveLevelChange()Real-TimeEvent

This method will return true once the sensor getActiveLevel() changes. It will then return false until the level changes to a different value again.

EXAMPLE SKETCH
void loop() {
  water_sensor.update();

  if(water_sensor.onActiveLevelChange()) {
    // code only runs once per event
    Serial.println(water_sensor.getActiveLevel());
  }
}
water_sensor
.setModifier(value)

Provide a positive or negative integer to temporarily adjust the water sensor threshold scale. The default value is 0.

For example; if you have calibrated the scale and turn on a motor near the sensor: all readings will need to be adjusted for the increased capacitance from the motor.

  • If a running motor increases the difference in water sensor readings by a getRawValue() of +200
  • Call setModifier(-200) on the water sensor when the motor turns on
  • All of the calibrated water sensor thresholds will adjust -200
  • The water sensor getActiveLevel() and getValue() will account for motor interference
  • Call resetModifier() on the water sensor when the motor shuts off
EXAMPLE SKETCH
void loop() {
  if(...) { // adjust when motor is on
    water_sensor.setModifier(-200);
  }
  else {
    water_sensor.resetModifier();
  }

  water_sensor.update();

  // still works as expected with interference
  if(water_sensor.onActiveLevelChange()) {
    ...
  }
}
water_sensor
.resetModifier()

Changes the setModifier() back to 0 and resets calibration of the water sensor threshold scale.

For example; if you have calibrated the scale and turn on a motor near the sensor: all readings will need to be adjusted for the increased capacitance from the motor.

  • If a running motor increases the difference in water sensor readings by a getRawValue() of +200
  • Call setModifier(-200) on the water sensor when the motor turns on
  • All of the calibrated water sensor thresholds will adjust -200
  • The water sensor getActiveLevel() and getValue() will account for motor interference
  • Call resetModifier() on the water sensor when the motor shuts off
EXAMPLE SKETCH
void loop() {
  if(...) {
    water_sensor.setModifier(-200);
  }
  else { // reset when motor is off
    water_sensor.resetModifier();
  }

  water_sensor.update();

  // still works as expected with interference
  if(water_sensor.onActiveLevelChange()) {
    ...
  }
}