Hello, I am new to Arduino and programming in C and, quite predictably, am having problems with my first project.
I am trying to design a simple project to fill a water tank using a pump when the contents gets low and stop filling once it has filled. I am using two float sensors, one at the top and one at the bottom. The pump is operated via a solenoid.
I designed and debugged the project using the simulator on the circuits.io web site and, I'm pleased to say, that after some tweaks it seemed to work on the simulator. Unfortunately, when I put the theory into practice it was not the case. The solenoid only seems to switch on the action of the lower sensor.
The sketch uses an If statement to sense when the bottom sensor is off, this action will turn on the pump. The using a do while statement the pump stay on until the top sensor is on, then turn the pump off.
I have attached a copy of the circuit and the code. Can any please advise me where I am going wrong?
I'm sure to the more experienced of you the answer is probably quite obvious but I have tried and tried and I can't see it.
Posting code / images in a PDF is a bit annoying. Just place them inline.
Press Ctrl + T to fix the code formatting.
Simulators are rubbish, just wire it up and test it.
An Arduino can't control a relay directly. You have to use a transistor (and resistor and diode) to do so or make it easy by grabbing a relay module.
We know nothing about the float sensors you use...
You can use the build in pull ups instead of external resistors. Just call pinMode(pin, INPUT_PULLUP), connect the switch between GND and pin and keep in mind a activated switch now reads LOW and a deactivated switch reads HIGH.
Just drop the do-while. Let the loop run free and start the pump when water is low and stop it when it's high again.
I would use an 'if' here.
If bottom sensor Closes, set a variable.
If the variable is set turn on the pump, else turn the pump off.
When the top sensor Closes, reset the variable.
If this is a real project you should have a hardware fallback.
When a sensor below the bottom Closes, ring a bell. . .
When a sensor above the top Closes, disable the power supply and ring a bell.
Sorry for putting the code in a pdf and for not using 'code tags' (what ever they are!).
Like I said, I'm new to this and trying to learn.
Here is the code in a text format.
int TopSens=5; // Sets Float Sensor located at the top of the tank to pin 5
int BotSens=6;// Sets Float Sensor located at the bottom of the tank to pin 6
int TopOut;// Output from Float Sensor located at the top of the tank
int BotOut;// Output from Float Sensor located at the bottom of the tank
int Pump=3;// Sets solenoid to pin 3
void setup() {
Serial.begin(9600);
pinMode(TopSens, INPUT);
pinMode(BotSens, INPUT);
pinMode(Pump, OUTPUT);
}
void loop() {
TopOut = digitalRead(TopSens);
BotOut = digitalRead(BotSens);
Serial.print("Top : ");
Serial.print(TopOut);
Serial.print(" Bot : ");
Serial.println(BotOut);
if (BotOut == 0)// Checks if bottom sensor is off
{
do
{
digitalWrite(Pump, HIGH);// If bottom sensor is off the set solenoid to on
TopOut = digitalRead(TopSens);// Read Top sensor
} while (TopOut == 0);// While top sensor is off solenoid stays on
}
else
{
digitalWrite(Pump, LOW);// When top sensor is on turn solenoid off
}
}
That's why there is that super handy How to use the forum sticky on top of each board So please read that and then go back to edit your post to add code tags
Then, you can start working on fixing the problem.
if (LevelIsMin)
TurnPumpOn();
else if (LevelIsMax)
TurnPumpOff();
The pump turns on when the lower switch indicates level is at or below minimum. The pump remains on until the upper switch indicates the level is at or above the maximum.
You can go back and edit your post with
the code tags Larry has shown. They can be manually typed it,
In any case, I see nothing wrong with the code
but I suspect there is something wrong with your wiring.
I wouldn't have written the code the way you did but
that is another issue.
If you are using the Arduino to other parallel operations,
the do ... while is blocking and the Arduino can do nothing
else until the top switch goes to true.
Your print statements won't print while filling and the solenoid
is on.
Dwight
How much current does your relay coil require? Arduino pins can only supply 20 mA continuously and you should have a suppression diode across the coil.
Can you post a part number and/or link to your relay? Also, here's a suggested modification to your program.
unsigned long start; // Print timer start
int fin = 1000; // Print timer finish
byte TopSens = 5, // Sets Float Sensor located at the top of the tank to pin 5
BotSens = 6, // Sets Float Sensor located at the bottom of the tank to pin 6
TopOut, // Output from Float Sensor located at the top of the tank
BotOut, // Output from Float Sensor located at the bottom of the tank
Pump = 3; // Sets solenoid to pin 3
void setup() {
Serial.begin(9600);
pinMode(TopSens, INPUT);
pinMode(BotSens, INPUT);
pinMode(Pump, OUTPUT);
}
void loop() {
TopOut = digitalRead(TopSens);
BotOut = digitalRead(BotSens);
if(millis() - start > fin){ // Wait fin milliseconds to print
start += fin; // Reset timer
Serial.print("Top : ");
Serial.print(TopOut);
Serial.print(" Bot : ");
Serial.println(BotOut);
}
if(TopOut == 1)
digitalWrite(Pump, LOW);
else if(BotOut == 0)
digitalWrite(Pump, HIGH);
/*
if (BotOut == 0)// Checks if bottom sensor is off
{
do
{
digitalWrite(Pump, HIGH);// If bottom sensor is off the set solenoid to on
TopOut = digitalRead(TopSens);// Read Top sensor
} while (TopOut == 0);// While top sensor is off solenoid stays on
}
else
{
digitalWrite(Pump, LOW);// When top esnsor is on turn solenoid off
}
*/
}