Arduino water tank sensor

All new Belgian homes are outfitted with a rain water tank (een regenwaterput, as we call it) these days. Which is a really neat way to save on water, since you can use it for things like flushing toilets or even washing clothes. However, at some point after a dry period, that water tank runs out and in our case that means our toilets are flushed with mud (I know, I should clean the bottom of my pit). Knowing when it’s close to running out on beforehand is quite handy and with an Arduino and ultrasonic sensor, you don’t have to go through the trouble of opening the water tank and looking inside for a rough estimate.

WARNING: These days, I updated a lot of what is written below. I’m now running this from a Wemos D1 Mini and sending results to a Home Assistant Server over MQTT. Maybe I’ll write that up sometime as well…

The way I did it, was using an ultrasonic sensor that sends a signal out and catches it again, measuring the time between it to measure distance. I mounted that to the top of the water tank to measure the distance to the water surface and then calculate how full the tank is. For starters, we’ll need a couple of components together with our Arduino. I’m a huge cheap-skate, so I ordered mine from DealExtreme, I got everything I needed for about 30 dollars. Here’s what I ordered:

Total price at the time of ordering: $ 33,35

Delivery to Belgium from China using EU Direct DX took 9 days, your results may vary I guess. I was happy to receive my padded envelope (part of the cheapskate experience) and started unpacking.

Here are the parts you’ll need for this tutorial:

2015-04-30 20.55.33

You’ll see the few items I’ve named earlier. Now, step one is to solder the headers on the display. If you bought a display that already has headers, good for you! If not, have fun!

After that, the next step is to start plugging everything for our first prototype. There’s various ways to do this, but I’ll go through the steps I took. First off, here’s the complete wiring schematic:

What’s good to know about the breadboard shown above here (the white thing), is that it’s 2 top and bottom rows are horizontally connected and are usually used for + and – power poles. You’ll see in the schematic I attached the 5V port of the Arduino to the red row, and the GND port to the blue row once. That row then powers all the other components by attaching them to that row. The other ports are vertically connected, so for example the 4 wires I connected above the ultrasonic sensor are connected with the 4 pins of the sensor.

But let’s run through it step by step, to make it easy to follow for anyone (even beginners like myself). Step one is setting up power to your breadboard (for now with the Arduino not attached to USB). So start with connecting 2 wires, one from the 5V port to one row, and the other from GND to another row. I also inserted the display with it’s pins in the bottom row of the upper part of my breadboard.

2015-04-30 20.58.34

Then, take the potentiometer and hook it up to the positive and negative poles, the middle pin goes to the pin marked ‘V0’ on the display (it’s the third one from the left).

2015-04-30 21.00.42

To power the display, you need to hook up 5 pins to the power lines. The two outer ones and the fifth one from the left go to the negative line. The two besides the outer ones go to the positive lines.

2015-04-30 21.04.14

To show data on the display, you need to connect a few data wires too. It’s hard to explain, so use the schematic above for more clarification. You’ll be using data ports 2, 3, 4, 5, 11 and 12 on the Arduino.

2015-04-30 21.06.57

Almost done now, just add the sensor to 4 pins in a row on the breadboard. Then make sure the GND pin is connected to the negative line, VCC to the positive, Trig to data pin 8 and Echo to data pin 9.

2015-04-30 21.10.58

Now the prototype is pretty much done, all that’s left to do is programming the Arduino to what you want it to do. Download the Arduino IDE (the piece of software we’ll use to upload code to it) on their website and open up the app. You’ll be looking at a screen like this:

Schermafbeelding 2015-05-01 om 08.48.01

To understand what’s going on in here: you see 2 sections to start off, a setup and a loop. The setup is the part that’ll run once on startup, the loop is what it’ll be running through constantly once it’s powered. Now you should plug in your Arduino and find your Arduino Uno in the app (Tools > Port > /dev/cu.usbmodem411). Just opening this file gives you all the necessary code:

Arduino Water tank sensor file

Or check out the code here:

#define trigPin 8
#define echoPin 9
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
      int duration,distance,percentage,heightTank;
      Serial.begin (9600);
      pinMode(trigPin,OUTPUT);
      pinMode(echoPin,INPUT);
      lcd.begin(16,2);
      lcd.print("HELLO");
}

void loop(){
      int duration,distance,percentage,heightTank,deviation;
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=151;
      deviation=29;
  
      digitalWrite(trigPin,HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin,LOW);
      duration=pulseIn(echoPin,HIGH);
      distance=(duration/2)/29.1;
      percentage=100-(((distance-deviation)*100)/heightTank);
      Serial.println(distance);
      Serial.println(percentage);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Current tank");
      //lcd.print(distance);
      //lcd.print(" cm");
      lcd.setCursor(0,1);
      lcd.print("level: ");
      lcd.print(percentage);
      lcd.print(" %");
      delay(1000);
  }

Important note for our American friends: all distances are in centimeters! So you’ll probably need to do some conversion to get the distance you need!

Now, if you hit the little button with the arrow to the right above your text field, it’ll upload the code to your Arduino device. And if all is alright, your display should suddenly start displaying something like this:

IMG_20150502_200521

That means your prototype is pretty much done! Now comes the installation part that’ll vary for each and every water tank. I’ll gladly show how I did it, maybe it’ll get you some inspiration. First of all, you probably need to waterproof the sensor. Not because it’s going to be submerged, but because there will be some condensation. I used Sugru, a sort of Play-doh that hardens after a few hours in contact with air. So I unpacked a bit of it and pushed it all over the sensor. But your solutions may vary (and are probably much more professional anyway)! This was what I ended up with:

2015-05-03 13.25.14

I added some tape to make it a bit more… safe, but feel free to experiment I guess. I taped the sensor and wires to a little wooden board and used a screw terminal (Belgians will know this as a ‘lusterklem’ or ‘suikerklontje’) to connect the sensor to the ethernet cable. Setup can be seen here:

2015-05-04 18.32.19

And then it’s a matter of remembering what colors you connected with which (yes, I am aware I could have matched the colors on beforehand… Shut up…) and completing your setup indoors. My result ended up looking like this after fixing up the code a little bit and sticking it to the wall.

2015-05-04 19.32.29

So that’s it, you’re done! Feel free to share pictures of your setup below, or give me tips on how to improve mine. This was my first experience with Arduino, so I’m sure the pro’s can tell me more. If you’re also experimenting with Arduino and following this guide, good luck!

Also, if you liked this tutorial, you’re always welcome to buy me a beer! Not that you owe me or anything, but I can only try, right? https://www.paypal.me/dennisjanssen