PDA

View Full Version : Water level sensor using SR04 Ultrasonic Sensor


d0ughb0y
09/03/2013, 02:45 PM
This is another feature I use in my DIY controller. I figure this can be used with any diy controller so I'm sharing this.

I've always had the problem with my top off container running dry, sometimes to the point where the sump return goes below the return pump input. So I figure I needed a way to measure my top off water container water level and send me an alert when the water level is low.

I experimented a solution to this problem using the cheap SR04 (or SRF05) ultrasonic sensor to measure the water level. These sensors sell on eBay for under $2 each. I've only used it for a month and so far it's been working great. No more empty top off water tank! I don't think it can be used as replacement for ATO switches yet (perhaps with some refinement in software).

I currently get the reading 60 times per second (may be overkill, but that is my arduino main loop cycle time) and take the running average over the last 60 readings. I use a pin change interrupt to get the echo line time measurement so it does not interfere with the main loop. My DIY controller will sound the alarm and send me an email when water level is at 5% or less.

pictures.

my top off tank, with holes for SR04 sensor
http://i1193.photobucket.com/albums/aa352/jerrysy/controller/2013-09-03T12-51-05_0480x360_zps5abfe82f.jpg~original

sensor installed (ignore the rubber band).

http://i1193.photobucket.com/albums/aa352/jerrysy/controller/2013-09-03T12-51-06_1480x360_zpsb9ffe8a7.jpg~original

this is the display on my iphone app

http://i1193.photobucket.com/albums/aa352/jerrysy/controller/2013-08-21T09-35-08_2_zps52f0addb.png~original

for those not familiar with the sensor, this is what it looks like. you can google SR04 ultrasonic sensor for more info.
http://i1193.photobucket.com/albums/aa352/jerrysy/controller/sr04_zpsd3e08843.jpg~original

camblonie
09/03/2013, 04:49 PM
No corrosion issues?

d0ughb0y
09/03/2013, 05:14 PM
No corrosion issues?

it's only been a month, time will tell. I have two more of the sensors for replacement just in case (got them for $1.30 each). I'm thinking of either using hot glue gun or aquarium silicone to seal the sensor electronics. The front part, I do not expect to corrode since top off water is RODI, and only the sender and receiver are exposed to the top off water the way it is set up. As for the electronics side of the sensor, that may be exposed to some humidity (evaporation would be pure h2o, no salt), but may be exposed to occasional accidental salt water splash, that could be protected by sealing the electronics. For now, I'm not too concerned with it. The functionality of getting water level for me is more important.

sleepydoc
09/25/2013, 06:20 PM
What about a plain ol' float switch? If it's just in the ATO tank, there shouldn't be many issues with algae/critters gumming it up.

dartier
09/25/2013, 07:25 PM
That is cool. I never thought of using those for water level sensing.

I also made a DIY water sensor based on trapped air in a PVC tube hooked to a pressure sensor.

Automating the water level sensing of my Salt, RO/DI and sump was the best change I ever added to may tank.

Dennis

d0ughb0y
09/25/2013, 07:50 PM
What about a plain ol' float switch? If it's just in the ATO tank, there shouldn't be many issues with algae/critters gumming it up.

I suppose if all you need is an alert when the water level is low, then a float switch will work.

Using the ultrasonic sensor gives you a good estimate of the amount of water in the container. Costwise, it's about the same $1.30-$1.40 for ultrasonic sensor or a float switch on eBay.

Arkayology
10/29/2013, 08:46 PM
This is a really cool idea. I had never thought about this as a feature.

Chrisrush
10/29/2013, 08:55 PM
I definitely need to figure out how to add one of these to my salt water reserve. I can't see into the tank and so I have to guess.

I have several parts for a diy arduino controller. Could this read out be display on an lcd screen?

d0ughb0y
10/30/2013, 07:01 PM
I definitely need to figure out how to add one of these to my salt water reserve. I can't see into the tank and so I have to guess.

I have several parts for a diy arduino controller. Could this read out be display on an lcd screen?

I think if you google search, you can find sample arduino sketch that uses the ultrasonic sensor and displays the reading to an LCD shield.

Chrisrush
10/30/2013, 08:09 PM
I think if you google search, you can find sample arduino sketch that uses the ultrasonic sensor and displays the reading to an LCD shield.

Does the instructions show how to hook up the sensor to the shield?

Edit: Nevermind, found it. I'll order a sensor tonight.

27techguy
12/24/2013, 06:38 PM
Hey d0ughb0y, cool project. What app are you using for phone?

d0ughb0y
12/24/2013, 06:58 PM
I wrote the mobile webpage.

djmx2002
12/25/2013, 09:30 PM
have you checked out working with pressure sensors? they ran at around 11-13 on mouser.

d0ughb0y
12/25/2013, 10:18 PM
have you checked out working with pressure sensors? they ran at around 11-13 on mouser.


I have.

djmx2002
12/25/2013, 10:20 PM
Any luck? I have messed around with the low pressure one, probably good to around a feet of water or so. Planning to use 3 of them to monitor the tanks in my new system

d0ughb0y
12/25/2013, 10:28 PM
Any luck? I have messed around with the low pressure one, probably good to around a feet of water or so. Planning to use 3 of them to monitor the tanks in my new system


I have only considered them but went with ultrasonic sensors instead.

synapse2
12/26/2013, 07:52 AM
Could you share your experience on how good the sensor is in reading the level of the water container say at empty, 1/4th full, 1/2 full etc?

Is sound echo an issue if you have a more empty tank than full?

Have you experimented with a different size of the water container. Do you have to change or calibrate it for that?

synapse2

hugorsf
12/26/2013, 08:15 AM
Does this sensor work in darkness?

I never use one of these cause i thought it dosent work in darkness!

Thanks, i have a few of those at home, so late night project! :)

reefwiser
12/26/2013, 08:44 AM
Ultra Sonic means sound. High frequency sound. I use tunable ones here at work for sensing packages.

Milfordguy
03/07/2014, 08:32 AM
Do you have a build thread for your DIY controller?

d0ughb0y
03/07/2014, 12:04 PM
Could you share your experience on how good the sensor is in reading the level of the water container say at empty, 1/4th full, 1/2 full etc?

Is sound echo an issue if you have a more empty tank than full?

Have you experimented with a different size of the water container. Do you have to change or calibrate it for that?

synapse2


it basically gives you the distance from the sensor (mounted at the top of your container) to the top of your water. You need to determine what value means full (smaller distance) and what value means empty (further distance) and you just interpolate from there. Only the vertical distance matter, of course we are assuming the container has the same cross section area from bottom to top. e.g., this won't work if you water container is in the shape of a pyramid, actually it can, but you have to do more math calculations, its probably going to be unusable for really irregular shaped containers.

I think it works fine, except your resolution is 1cm.

d0ughb0y
03/07/2014, 12:05 PM
Do you have a build thread for your DIY controller?

build thread is here

http://reefcentral.com/forums/showthread.php?t=2332822

karimwassef
06/13/2014, 05:54 PM
Any idea how to make it work with an apex?

Charlene
06/18/2014, 09:26 PM
Can you post your arduino code?

sleepydoc
06/19/2014, 07:12 AM
There are 2 similar sensors - one by Parallax (http://www.parallax.com/product/28015) and one by Sainsmart (http://www.sainsmart.com/ultrasonic-ranging-detector-mod-hc-sr04-distance-sensor.html). The Sainsmart sensor is generally somewhat cheaper. The only difference I've seen is that the Parallax model only uses one pin (plus +5V & Gnd,) while the Sainsmart sensor uses 2. I haven't actually used both to compare accuracy, reliability, etc.

They both work by returning a pulse whose duration is proportional to the measured distance after receiving a triggering pulse from the controller.

The basic code for the Parallax sensor is posted here (http://learn.parallax.com/kickstart/28015). There are many sites that have instructions on the Sainsmart sensor. Here (http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/) is one. There's also an Arduino Playground page (http://playground.arduino.cc/Code/SR04) that has a link to a library that someone wrote.

If you're looking for for the complete controller code, d0ughb0y posted it in his original post. The thread is usually in the first couple of pages in the DIY section.

karimwassef
06/19/2014, 07:25 AM
I'm thinking this could be turned into a voltage, impedance matched and then connected to a probe input (temperature?) on the Apex?

d0ughb0y
06/23/2014, 04:05 PM
Can you post your arduino code?

as sleepydoc pointed out, you can use the test sketch that works for the ultrasonic sensor you are using. The idea is you are getting the distance from the top of your container to the water surface. You just need to determine that distance when the container is full, and empty.

The code I use in my controller project is tightly integrated into the controller code. I use timer interrupt routine to periodically get the reading and use a simple smoothing function to smooth out sporadic bad distance reading.

If you search on arduino.cc forum, someone there wrote a library that uses timer interrupt routine code as well. the idea is the same, it periodically gets the distance measurement outside of the main arduino loop.

d0ughb0y
06/23/2014, 04:14 PM
I'm thinking this could be turned into a voltage, impedance matched and then connected to a probe input (temperature?) on the Apex?

actually, it will be more practical if neptune publishes the aquabus protocol so third party can add new devices like water level sensor.

hugorsf
10/15/2014, 10:43 AM
Great job!
Sensor still working good?

Can you post the code part when you trigger (or not) the pin to fill the container?

Thanks!

d0ughb0y
10/15/2014, 10:49 AM
yes still works fine.

The code is part of my controller project. You can find the source in
https://github.com/d0ughb0y/Chauvet16

you can write your own code by using any ultrasonic sensor library to get a distance reading and turn the pin that controls your pump on/off.

hugorsf
10/15/2014, 11:03 AM
Good to know:)

My main concern is how to control de pump to work or not, because sometimes i want the container to fill in a certain value and other time i don't.

Here is my base code, that is working:

#define trigPin 12
#define echoPin 13

const int valvulaPin = A1; // Pino que controla a eletrovalvula
int ledState = HIGH; // Estado do relé. O relé liga em LOW e desliga em HIGH
int nivelMin = 5; // distancia em cm que faz desligar o rele
int nivelMax = 40; // distancia em cm a partir da qual o rele é ligado para encher o deposito

void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(valvulaPin, OUTPUT);
}

void loop()
{
float temp, dist, perc;

/*limpeza do pino*/
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

temp = pulseIn(echoPin, HIGH);
dist = (temp/2) /29.1; /*divide-se por 2 porque o sinal vai e volta e divide-se pela velocidade do som (29.1)*/
perc = (dist*100)/nivelMax;

Serial.print(dist);
Serial.print ("cm --> ");
Serial.print (perc);
Serial.println("%");

if (dist < nivelMin) //If container full
digitalWrite(valvulaPin, HIGH); //pump off
else if(dist >= nivelMax) //if container empty
digitalWrite(valvulaPin, LOW); //pump ON

//espera 30 segundos porque não vale a pena estar constantemente a correr o ciclo
delay(5000);
}



My issue is how to andle the bold part when i have read the sensor and have to do something with it.

What i want is the pump (valvulaPin) starts only when the container is empty; Fill all the container until is full and power of the pump. then the container starts lowering until is empty again when the pump should start again. So lets say the sensor read's half container. how can i know if its filling or empting?:headwallblue:

d0ughb0y
10/15/2014, 01:29 PM
you need to turn your pump off in setup() so you start at a known state, pump is off.

yajur
10/21/2014, 02:14 AM
can we just use this to check the sump level ? without makeing ATS ?

d0ughb0y
10/21/2014, 11:16 AM
sure. it will be as good/accurate as how well you calibrate it. It will be better to use it on return section as water will not be as turbulent as in skimmer section.

shiftline
03/04/2016, 10:32 AM
I have used the optical ones a bunch of times and love them.. very reliable little sensors

DIY Optical auto top off (http://fishtankprojects.com/diy-aquarium-projects/diy-auto-top-off-arduino-optical-ato.html)

Ill probably build a new one soon for my new tank and will make a how to when i build it

shifty51008
03/04/2016, 11:41 AM
any corrosion issues yet with the sensor doughboy?

shifty51008
03/04/2016, 11:42 AM
I have used the optical ones a bunch of times and love them.. very reliable little sensors

DIY Optical auto top off (http://fishtankprojects.com/diy-aquarium-projects/diy-auto-top-off-arduino-optical-ato.html)

Ill probably build a new one soon for my new tank and will make a how to when i build it

nice build, why did you paint the float black though?

d0ughb0y
03/04/2016, 12:01 PM
any corrosion issues yet with the sensor doughboy?

as long as salt water does not touch the sensor, you are fine.
early on when my skimmer goes nuts, the sensor get sprinkled with a little salt water and gets corroded. The current ultrasonic sensor I am using has not had that problem for a while.

d0ughb0y
03/04/2016, 12:29 PM
I have used the optical ones a bunch of times and love them.. very reliable little sensors

DIY Optical auto top off (http://fishtankprojects.com/diy-aquarium-projects/diy-auto-top-off-arduino-optical-ato.html)

Ill probably build a new one soon for my new tank and will make a how to when i build it

nice project.
FWIW, this DIY is for "water level" sensor. i.e. to detect if water level in ATO reservoir is 100% or empty, etc. and is not for detecting whether to turn on ATO pump or not. I don't know if there is currently any ATO system that includes top off reservoir water level sensor. Maybe for your ATO to be complete, you can include a water level sensor in top off water reservoir.

the problem with normal float switch, I solve using this type of float switch. (with splash guard)
http://autotopoff.com/Floatswitches/ms8000.jpg

I bought mine from Grainger for like $10 maybe 8 years ago and I thought that was expensive. autotofoff.com price is $14. never had a problem since I used it.

d0ughb0y
03/04/2016, 02:56 PM
shiftline, does that optical sensor output analog signal based on how far the water surface is? in that case, I think that can be used in reservoir to detect water level. In this case, I can see an advantage as it only requires 1 analog input compared to using ultrasonic sensor.

do you get any condensation, salt creep, etc on the optical sensor surface that might affect the reading?

shiftline
03/04/2016, 07:00 PM
It's a digital high low output. Needs 5v power. I have 2 on my current ATO for prob 6 months and haven't had any issues yet!

shiftline
03/04/2016, 07:01 PM
nice build, why did you paint the float black though?


It was originally designed for a fluval edge than and i didn't want to visible at the top rim of the tank

d0ughb0y
03/05/2016, 03:08 PM
It's a digital high low output. Needs 5v power. I have 2 on my current ATO for prob 6 months and haven't had any issues yet!

where did you get the sensor?

I found this that comes with a board that outputs both digital and analog.
http://www.ebay.com/itm/231727990883

and this just for the sensor
http://www.ebay.com/itm/301848699533

I always try to go for a simpler solution when possible, and determining if this will be simpler for water level detection compared to ultrasonic.

the optical sensor is rated 5-24 volts. If it will work on 3.3.v, then I'll probably go with this on my chauvet 2.0 controller.

KingFin
03/05/2016, 11:27 PM
Any concern for condensation build up on sensor? May give false readings in the future.

shiftline
03/06/2016, 12:48 PM
No issues so far but I have mine touching the tip of the sensor into the water. Once it drops it triggers my pump to add more water