Friday, October 29, 2021

Build your own rain and water level sensor

 For an index of all my stories click this text.

Recently I was building a project in which I needed a water level sensor. And a water level sensor is obvious the same technique as a rain sensor. The basic difference between the two is that a rain sensor has a large surface to catch rain drops while a water level sensor just needs two small contacts.

A few years ago I build a rain sensor with an ESP8266. The program was written in LUA and the project used a ready-made sensor. You can re-read that story here if you like:
https://lucstechblog.blogspot.com/2016/08/rain-sensor-using-esp8266.html

That version worked but was rather primitive. I have now much more expertise in sending notifications when it rains. You can send a signal to Domoticz, to your Phone, to a Blynk app, or to Telegram just to name a few.

But first you need a sensor. So lets build one.

Water sensor.

Basically what I am going to show you is a water sensor. The sensor just detects water and that can be rain, a water tank level, or a leaking washing machine it is all the same.



The sensor consists of an NPN transistor, a resistor and a capacitor. That's all.

The schematic is made with the free design program EasyEDA designer which can be found here: https://easyeda.com/

The output will give normally a 0 until the two probes are connected. That connection can be made by wet-fingers, or submerged in water or a damp piece of cloth.

The transistor can be a general NPN version. I build versions with a 2N222 and a BC547 transistor that worked equally well.



Above you see how the pins of the 2N222 are arranged.


And the picture above shows the pinout of the BC547 transisitor.

The difference between these two is that the Emiitor and Collector have switched places.

The resistor should be 1K and the capacitor 100 Nf or close to that. Using a larger capacitor makes the circuit take longer switching from 0 to 1 and the reverse.

First thing was doing some tests. For that I added a current delimiting resistor of 220 Ohm and a led for monitoring the output.

For convenience I herebye give you the breadboard setup.



Above is the version with the 2N222 transistor.


And here is the version with a BC547 transistor.

As stated both work equally wel.

Testing the sensor.

At the left side of the breadboard there are two purple wires. In reality I just used breadboard (Dupont) wires.

To test the sensor just connect the wires, wet your fingers and put the wires between them, put the wires in a glass of water or put them on a piece of cloth and start dropping water on that cloth. In all cases the led will go ON and as soon as the wires are seperated or dry again the led will go OFF again.

This sensor is very sensitive. So putting both ends at one side of a bowl of water is enough to set the led ON.

Connecting to an ESP

You can use this sensor with an Arduino, AT Tiny85, ESP8266, ESP32, Raspberry Pico or any other controller you want to use in your project.

For testing I used an ESP8266.



Here is the breadboard setup for a sensor using the 2N222 with a Wemos D1 mini.



And here is the setup with a BC547 transistor.

In both versions the led is omitted and the output is direct connected to D8 of the Wemos D1.

ESP8266 program

// Rain sensor test
// The sensor is connected to D8

void setup() 
{
  Serial.begin(9600);
  delay(1000); 
  Serial.println("ESP8266 rain test");
  pinMode(D8, INPUT);
}

void loop() 
{
  Serial.println(digitalRead(D8));
  delay(1000);
}


This is just a basic program that displays the sensor reading in the Serial Monitor.

In the setup() the serial monitor is initiated at 9600 baud and D8 is defined as input pin. The loop just prints the reading of pin D8 in the serial monitor.

Pin D8 will give LOW (0) till the input detects water. Then D8 will change to HIGH (1). So beware that the input is reversed to what we normally use when connecting a button or some other sensor.

Practical uses.

This is like said above a very sensitive sensor. It can be used for a lot of purposes. Examples are a rain-sensor, water level sensor, soil moisture sensor but also as a contact sensor for doors etc. I am going to use it for a water level sensor in an aquaponics system.

You do not even need a microcontroller for using this sensor. Attach a led or relay and you can make a local alarm or control a pump.

It can be used with any microcontroller you want to use in your project like the Arduino, Attiny, ESP32, ESP8266 and Raspberry Pico.

That's it for now.
Have fun

Luc Volders