Greetings. I am trying to do what I think it is a very simple sketch with arduino, but it does not work correctly. I have a water level sensor like this one:
And I just what to initiate a relé with a water pump when the level of water is low, and stop it when the level is high again.
The sketch I am using is this one:
int floatPin = 4; // Entrada analogica por ejmeplo la 4
int valor = 0;
int bombaaguaPin = 49;
void setup(){
Serial.begin(9600);
pinMode (floatPin, INPUT);
pinMode (bombaaguaPin, OUTPUT);
Serial.println("Sensor de liquidos");
}
void loop(){
// leemos el valor que devuelve la sonda
if ( analogRead (floatPin) == 0 ){
Serial.println("Nivel de agua bajo");
}
// Un pequeño retardo.
delay (1000);
And the problem I encounter is that it is not reliable at all. Suddenly the relé starts on and off continuously, suddenly when it is off it gets on without any change in the water level....
Could someone tell me if this is a failure from the program, if there is a way to make it more precise, or if it is just that the sensor is not good, or what might be the reason...
Are you using A4 or D4 ? Does the sensor work like a switch ?
If you're using D4, it's digital pin and you should use digitalRead() instead of analogRead() with digital pins..
If you wish to use analogRead(), you should use analog pins for that (A0, A1 etc..)..
If you're using A4, you should use some hysteresis to avoid oscillating around sensing point.
Also, try to avoid if(analogRead(pin) == 0) statement, use rather if(analogRead(pin) < 10) instead.
Also if you trying to control relay directly with pin D49, remember maximum current value for IO-pins..
Try to connect another one of sensor's wires to the A4 pin and another in to the 5V and after that maybe something like this could work:
Many many thanks TommIP, I am trying the sketch you tell me.
Just a couple of questions:
*** If I connect one of the cables of the float switch to 5 V, and the other to A4...should I also put a resistance between A4 and ground? Something like this (assuming Cable A and Cable B are the 2 cables from the float sensor):
Cable A: to 5 V in arduinio
Ground (arduino) ----10 K Ohms -----Cable B -----Pin A4.
In this way, I will avoid current going through Pin A4...Is this correct? (as far as I read by now it should be...)
***The second question is if it is better to use A4 (analog pin) or use a digital one, as far as I read the float sensor does work like a switch. You wrote sketch for analog input, and I am trying the one you wrote.
As said, many thanks for your help!! Will tell you if it works.
Please find out how your switch operates. The data sheet indicates that it is a simple contact, which can only be on or off. When you want to start the pump on low water level, and stop it on high water level, how do you intend to achieve that with a single switch?
DrDiettrich:
Please find out how your switch operates. The data sheet indicates that it is a simple contact, which can only be on or off. When you want to start the pump on low water level, and stop it on high water level, how do you intend to achieve that with a single switch?
It operates as a float sensor, a part is fixed at the chosen position, and a mobile part may flow when the water level increases. If the water level is low, below the sensor, ohmic resistance between the 2 wires is 0. When the water reaches the sensor the mobile part starts floating, and the resistance gets different from 0. In principle i see no problem to use it as a single switch, but I will try to treat it as a analog variable. (As soon as I have time, hopefully soon!!!!). Thanks for your interest!!
TCU_SCI:
It operates as a float sensor, a part is fixed at the chosen position, and a mobile part may flow when the water level increases. If the water level is low, below the sensor, ohmic resistance between the 2 wires is 0. When the water reaches the sensor the mobile part starts floating, and the resistance gets different from 0.
The resistance will jump from zero (ON) to infinite (OFF), with no other values in between. You can use the sensor to switch the pump on when the level is below the chosen position, and it will stop when the level is above that position. No Arduino needed for that.
For what it's worth, the non-Arduino/any MCU method of controlling a pump or anything else between high and low levels is called a relay with "hold-on" contacts. You want something to operate when either a high or low level is reached. You want it to run until another level is reached. The difference between the two levels is "hysteresis" - you don't want the pump to operate if the level changes by 1-mm - this is called "hunting". The hold-on concept is delightfully simple, once you get your head round it. No software, just a relay with two changeover contacts. To translate this into a sketch would just involve some logic like AND's, STATECHANGE. Or easier still, some CMOS chips to latch and release. Example - water level rises in a sump in the basement - high level float switch operates, energises relay. Contacts close to operate pump. Another set of contacts with the low level float switch in series supply power to the relay coil. The level drops past the high switch - nothing happens because the coil is fed by the other contacts, effectively a latch - level drops to low float switch, contacts open, releases power to pump and so on ad infinitum. I would like a pound or a dollar for every time I've used "hold-ons", the best thing anyone taught me
An even simpler circuit can be done using a 555 timer. When the input goes low ( low water contact pulls pin 2 to ) V.) Set the timer to make the pump run until the tank roughly 3/4 full.A 555 can drive a 12V relay directly & the circuit can be built for about $4.
Any advance on $4?
The really basic way of achieving this is the float switch (with tilt switch inside) that either pops up like a mooring bouy at the high level adjusted by the length of the attached cable, then flops over as the level drops - that's the hysteresis - low and high level in one switch. But seeing as this is an Arduino question, then translate the "logic" of the float switches into software flip-flops or whatever. Nothing more to add.
TommiP sketch is working really well, by far all perfect!!!!!!!!
Really thanks to you all for your kind interest and help.
Perhaps it is also possible to use sensor without Arduino, but I am not that sure that it would be reliable because even with Arduino, if I did not include the hysteresis that TommiP was remarking, the relay was not really working so well....
Anyway, all good that ends good, and I may only be really grateful to you all, as my project is working by now, thanks to your help (particularly to TommiP).