Watering pump with LCD, arduino based. need advice plz

dendad

Joined Feb 20, 2016
4,479
You still don't have pinMode commands for your analog inputs. This might explain the input where you get no sensor readings.
The Arduino does not require pinMode define for analogRead commands.


  • // set all moisture sensors PIN ID
  • int moisture1 = A0;
  • int moisture2 = A1;
  • int moisture3 = A2;
int moisture4 = A3;
And these can be changed too.
#define moisture1 A0
#define moisture2 A1
#define moisture3 A2
#define moisture4 A3
 

ebeowulf17

Joined Aug 12, 2014
3,307
The Arduino does not require pinMode define for analogRead commands.
Oops! I guess I've had a few redundant lines in all of my sketch setups!

Still, if nothing else, it seems like a good idea to explicitly set the internal pull ups on or off as needed, doesn't it?
 

ebeowulf17

Joined Aug 12, 2014
3,307
Oops! I guess I've had a few redundant lines in all of my sketch setups!

Still, if nothing else, it seems like a good idea to explicitly set the internal pull ups on or off as needed, doesn't it?
Actually, the Arduino website says this:
Details and Caveats
The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.
https://www.arduino.cc/en/Tutorial/AnalogInputPins

So, maybe setting the pin mode is a good idea after all, or else the official website is out of date? I don't know, but I'm going to keep doing it just to be sure.
 

dendad

Joined Feb 20, 2016
4,479
Actually, the Arduino website says this:
https://www.arduino.cc/en/Tutorial/AnalogInputPins

So, maybe setting the pin mode is a good idea after all, or else the official website is out of date? I don't know, but I'm going to keep doing it just to be sure.
If you have previously used the pin and need to change the mode back it does make sense. And as you say, it could be a good practice to set it as an input and not assume anything. Even for code readability it may be worth while.
I must keep that in mind for future projects.
 

Thread Starter

Pashgen

Joined Nov 16, 2017
23
Good Morning guys,

I just updated the code.
I cant see moisture readings on LCD anymore. I attached the picture

somehow sensor on A2 turns on relay 1 and 2 on relay shield at the same time. Pump relay is working fine.
A1 is working properly - Relay1 and pump Relay turns on when sensor is out of water.
A3 doesn't do anything
A0 turns on only Pump relay

C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define relay1 3
#define relay2 4
#define relay3 5
#define relay4 6
#define pump 2
// set all moisture sensors PIN ID
#define moisture1 A0
#define moisture2 A1
#define moisture3 A2
#define moisture4 A3

// declare moisture values
int moisture1_value = 0;
int moisture2_value = 0;
int moisture3_value = 0;
int moisture4_value = 0;

int sensorValue = moisture1;
int sensorValue1 = moisture2;
int sensorValue2 = moisture3;
int sensorValue3 = moisture4;

// set water relays
//int relay1 = 3;
//int relay2 = 4;
//int relay3 = 5;
//int relay4 = 6;

// set water pump
//int pump = 2;
//
void setup() {
digitalWrite(relay1, HIGH);
delay(1000);
digitalWrite(relay1, LOW);
delay(1000);
digitalWrite(relay2, HIGH);
delay(1000);
digitalWrite(relay2, LOW);
delay(1000);
digitalWrite(relay3, HIGH);
delay(1000);
digitalWrite(relay3, LOW);
delay(1000);
digitalWrite(relay4, HIGH);
delay(1000);
digitalWrite(relay4, LOW);
delay(1000);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(pump, LOW);
 
  // declare relay as output
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  // declare pump as output
  pinMode(pump, OUTPUT);
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}
void loop() {
 
// read the value from the moisture sensors:
// moisture1_value = analogRead(moisture1);
// moisture2_value = analogRead(moisture2);
// moisture3_value = analogRead(moisture3);
// moisture4_value = analogRead(moisture4);
//sensorValue = analogRead(moisture1);
//sensorValue1 = analogRead(moisture2);
//sensorValue2 = analogRead(moisture3);
//sensorValue3 = analogRead(moisture4);
moisture1_value = analogRead(moisture1);
moisture2_value = analogRead(moisture2);
moisture3_value = analogRead(moisture3);
moisture4_value = analogRead(moisture4);

lcd.setCursor(0, 0);
lcd.print(sensorValue);
lcd.setCursor(7,0);
lcd.print(sensorValue1);
lcd.setCursor(0, 1);
lcd.print(sensorValue2);
lcd.setCursor(7,1);
lcd.print(sensorValue3);

// check which plant need water
// and open the switch for that specific plant

if(moisture1_value>=600){
  digitalWrite(relay1, HIGH);
}
if(moisture2_value>=600){
  digitalWrite(relay2, HIGH);
}
if(moisture3_value>=600){
  digitalWrite(relay3, HIGH);
}
if(moisture4_value>=600){
  digitalWrite(relay4, HIGH);
}

// make sure there is at least one plant that needs water
// if there is, open the motor
if(moisture1_value>=600 || moisture2_value>=600 || moisture3_value>=600 || moisture4_value>=600){
  digitalWrite(pump, HIGH);
}

// let it water the plant for 5 seconds
delay(5000);

// turn the pump off
digitalWrite(pump, LOW);

// go each switch and turn them off
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);

// wait and repeat the process

delay(1000);

}
Moderators note: Please use code tags for pieces of code
 

Attachments

Last edited by a moderator:

ebeowulf17

Joined Aug 12, 2014
3,307
Ok, for the third time:

Do all four relays open and close in the correct order during the start up sequence?

Answer that question before changing any more code or asking any more questions. It will help identify possible wiring issues.

Seriously, just answer that question.

Please.
 

ebeowulf17

Joined Aug 12, 2014
3,307
Sorry if my last two posts came off as harsh or curt. My frustration is that you keep changing everything at once and then listing everything that's wrong, all in a sort of random order. If you can be a little more methodical and work in a logical order, it will be much easier for us to help you.

I'd suggest fixing one issue at a time:
  1. Get the relay outputs working. The test sequence in your startup code should be perfect for that. All four relays should turn on and back off in order. If that works, we know that wiring and pin assignments are correct. If not, we start troubleshooting there first. Ignore anything that happens based on sensor inputs. Just restart the Arduino repeatedly until you're sure which relays work in the right order and which ones don't.
  2. AFTER step 2 is complete, get sensor readings displaying properly. Per my previous post, you need to update some variable names in your lcd.print commands, and then you should have working sensor readings on everything that was working before. For any remaining sensors that don't display properly, we can double check wiring, pin assignments, input/output/pullup status, etc. as needed.
  3. AFTER steps 1&2 are complete, we can see if there are any remaining behavioral problems. Trying to diagnose logic states when your inputs and outputs aren't working right would be impossible. After those ins and outs are working properly, it should be easy to fix any remaining logic bugs, if there are any.
Cheers.
 

be80be

Joined Jul 5, 2008
2,072
The code is not in any right order and code tags help to make it more readable.
All of the setup is wrong
C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define relay1 3
#define relay2 4
#define relay3 5
#define relay4 6
#define pump 2
// set all moisture sensors PIN ID
#define moisture1 A0
#define moisture2 A1
#define moisture3 A2
#define moisture4 A3

// declare moisture values
int moisture1_value = 0;
int moisture2_value = 0;
int moisture3_value = 0;
int moisture4_value = 0;

int sensorValue = moisture1;
int sensorValue1 = moisture2;
int sensorValue2 = moisture3;
int sensorValue3 = moisture4;

// set water relays
//int relay1 = 3;
//int relay2 = 4;
//int relay3 = 5;
//int relay4 = 6;

// set water pump
//int pump = 2;
//
void setup() {
  digitalWrite(relay1, HIGH);
  delay(1000);
  digitalWrite(relay1, LOW);
  delay(1000);
  digitalWrite(relay2, HIGH);
  delay(1000);
  digitalWrite(relay2, LOW);
  delay(1000);
  digitalWrite(relay3, HIGH);
  delay(1000);
  digitalWrite(relay3, LOW);
  delay(1000);
  digitalWrite(relay4, HIGH);
  delay(1000);
  digitalWrite(relay4, LOW);
  delay(1000);
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(pump, LOW);

  // declare relay as output
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  // declare pump as output
  pinMode(pump, OUTPUT);
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}
void loop() {

  // read the value from the moisture sensors:
  // moisture1_value = analogRead(moisture1);
  // moisture2_value = analogRead(moisture2);
  // moisture3_value = analogRead(moisture3);
  // moisture4_value = analogRead(moisture4);
  //sensorValue = analogRead(moisture1);
  //sensorValue1 = analogRead(moisture2);
  //sensorValue2 = analogRead(moisture3);
  //sensorValue3 = analogRead(moisture4);
  moisture1_value = analogRead(moisture1);
  moisture2_value = analogRead(moisture2);
  moisture3_value = analogRead(moisture3);
  moisture4_value = analogRead(moisture4);

  lcd.setCursor(0, 0);
  lcd.print(sensorValue);
  lcd.setCursor(7, 0);
  lcd.print(sensorValue1);
  lcd.setCursor(0, 1);
  lcd.print(sensorValue2);
  lcd.setCursor(7, 1);
  lcd.print(sensorValue3);

  // check which plant need water
  // and open the switch for that specific plant

  if (moisture1_value >= 600) {
    digitalWrite(relay1, HIGH);
  }
  if (moisture2_value >= 600) {
    digitalWrite(relay2, HIGH);
  }
  if (moisture3_value >= 600) {
    digitalWrite(relay3, HIGH);
  }
  if (moisture4_value >= 600) {
    digitalWrite(relay4, HIGH);
  }

  // make sure there is at least one plant that needs water
  // if there is, open the motor
  if (moisture1_value >= 600 || moisture2_value >= 600 || moisture3_value >= 600 || moisture4_value >= 600) {
    digitalWrite(pump, HIGH);
  }

  // let it water the plant for 5 seconds
  delay(5000);

  // turn the pump off
  digitalWrite(pump, LOW);

  // go each switch and turn them off
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);

  // wait and repeat the process

  delay(1000);

}
None of this is needed
C:
digitalWrite(relay1, HIGH);
  delay(1000);
  digitalWrite(relay1, LOW);
  delay(1000);
  digitalWrite(relay2, HIGH);
  delay(1000);
  digitalWrite(relay2, LOW);
  delay(1000);
  digitalWrite(relay3, HIGH);
  delay(1000);
  digitalWrite(relay3, LOW);
  delay(1000);
  digitalWrite(relay4, HIGH);
  delay(1000);
  digitalWrite(relay4, LOW);
  delay(1000);
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(pump, LOW);
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
4,479
I was posting some other stuff and it went away???
have a look at this and see if it is ok. There are some added noted in the code for you to check.

C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// this says you have a 20x4 LCD but your pictures show 16x2  ??????
LiquidCrystal_I2C lcd(0x27,20,4);


// define IO pins
#define pump 2
#define relay1 3
#define relay2 4
#define relay3 5
#define relay4 6

#define moisture1 A0
#define moisture2 A1
#define moisture3 A2
#define moisture4 A3

// declare moisture values
int moisture1_value = 0;
int moisture2_value = 0;
int moisture3_value = 0;
int moisture4_value = 0;

void setup() {
// define pin mode before use.
// declare pump as output
pinMode(pump, OUTPUT);

// declare relay as output
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);

// set all outputs  to off
digitalWrite(pump, LOW);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);

// do you need this??????
// lcd.init();
  lcd.backlight(); // ??????

// mine is as follows....
  lcd.begin(16, 2);
  lcd.clear();


// run a startup clack test

digitalWrite(relay1, HIGH);
delay(500);
digitalWrite(relay1, LOW);
delay(500);
digitalWrite(relay2, HIGH);
delay(500);
digitalWrite(relay2, LOW);
delay(500);
digitalWrite(relay3, HIGH);
delay(500);
digitalWrite(relay3, LOW);
delay(500);
digitalWrite(relay4, HIGH);
delay(500);
digitalWrite(relay4, LOW);
delay(500);

// are you going to use the serial???
Serial.begin(9600);


}
void loop() {

moisture1_value = analogRead(moisture1);
moisture2_value = analogRead(moisture2);
moisture3_value = analogRead(moisture3);
moisture4_value = analogRead(moisture4);

lcd.setCursor(0, 0);
lcd.print(moisture1_value);
lcd.setCursor(7,0);
lcd.print(moisture2_value);
lcd.setCursor(0, 1);
lcd.print(moisture3_value);
lcd.setCursor(7,1);
lcd.print(moisture4_value);

// check which plant need water
// and open the switch for that specific plant

if(moisture1_value>=600){
digitalWrite(relay1, HIGH);
}
if(moisture2_value>=600){
digitalWrite(relay2, HIGH);
}
if(moisture3_value>=600){
digitalWrite(relay3, HIGH);
}
if(moisture4_value>=600){
digitalWrite(relay4, HIGH);
}

// make sure there is at least one plant that needs water
// if there is, open the motor
if(moisture1_value>=600 || moisture2_value>=600 || moisture3_value>=600 || moisture4_value>=600){
digitalWrite(pump, HIGH);
}

// let it water the plant for 5 seconds
delay(5000);

// turn the pump off
digitalWrite(pump, LOW);

// go each switch and turn them off
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);

// wait and repeat the process

delay(1000);

}
Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

ebeowulf17

Joined Aug 12, 2014
3,307
I'd add these with the other pinModes:
pinMode(moisture1, INPUT);
pinMode(moisture2, INPUT);
pinMode(moisture3, INPUT);
pinMode(moisture4, INPUT);

And I'd add these with the other digitalWrites, just after the pinModes:
digitalWrite(moisture1, LOW);
digitalWrite(moisture2, LOW);
digitalWrite(moisture3, LOW);
digitalWrite(moisture4, LOW);

The digitalWrite commands guarantee that the internal pullup resistors are disabled for those inputs.

I have no idea if these lines will address your sensor input problems, but they can't hurt.
 

dendad

Joined Feb 20, 2016
4,479
None of this is needed
  • digitalWrite(relay1, HIGH);
  • delay(1000);
  • digitalWrite(relay1, LOW);
  • delay(1000);
  • digitalWrite(relay2, HIGH);
  • delay(1000);
  • digitalWrite(relay2, LOW);
  • delay(1000);
  • digitalWrite(relay3, HIGH);
  • delay(1000);
  • digitalWrite(relay3, LOW);
  • delay(1000);
  • digitalWrite(relay4, HIGH);
  • delay(1000);
  • digitalWrite(relay4, LOW);
  • delay(1000);
  • digitalWrite(relay1, LOW);
  • digitalWrite(relay2, LOW);
  • digitalWrite(relay3, LOW);
  • digitalWrite(relay4, LOW);
digitalWrite(pump, LOW);
We know that is not needed. It is just a debug test added to the start. Note it does not run the pump. just clacks each relay.
 

Thread Starter

Pashgen

Joined Nov 16, 2017
23
Got it working!!!!!!!!!!!
I decided to rebuild everything.
Using 8 relay module instead of shield and pump relay
picture 1 shows all 5 relays are ON when 4 sensors in the water
picture 2 shows readings from sensors
picture 3 shows one relay OFF and PUMP relay OFF when sensor out of water
picture 4 shows reading from sensors when one sensor out of water

for some reason I could get proper sensor readings on LCD only while using sensorValue, so kept it.
I also changed when they should react if(moisture1_value>=600). Was trying with 900 before. I guess better sensors will have much broader range.

Thank you very much guys!

I am so happy! Its so hard! I am learning a lot from you guys, especially from dendad!
Thank you so much dendad!!! You are the man! I want to be your student!
I don't have any pumps or valves yet but I am pretty sure relays and sensors doing what they suppose to.
Now waiting for my shipment of good capacitive soil moisture sensors and lots of valves and few different pumps.

Here is the code:
C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4);

// set all moisture sensors PIN ID
int moisture1 = A0;
int moisture2 = A1;
int moisture3 = A2;
int moisture4 = A3;

// declare moisture values
int moisture1_value = 0;
int moisture2_value = 0;
int moisture3_value = 0;
int moisture4_value = 0;

int sensorValue = moisture1;
int sensorValue1 = moisture2;
int sensorValue2 = moisture3;
int sensorValue3 = moisture4;

// set water relays
//int relay1 = 3;
//int relay2 = 4;
//int relay3 = 5;
//int relay4 = 6;
#define pump 2
#define relay1 3
#define relay2 4
#define relay3 5
#define relay4 6
// set water pump
//int pump = 2;
void setup() {
digitalWrite(relay1, HIGH);
delay(1000);
digitalWrite(relay1, LOW);
delay(1000);
digitalWrite(relay2, HIGH);
delay(1000);
digitalWrite(relay2, LOW);
delay(1000);
digitalWrite(relay3, HIGH);
delay(1000);
digitalWrite(relay3, LOW);
delay(1000);
digitalWrite(relay4, HIGH);
delay(1000);
digitalWrite(relay4, LOW);
delay(1000);
// go each switch and turn them off
// declare relay as output
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  // declare pump as output
  pinMode(pump, OUTPUT);
  // declare the ledPin as an OUTPUT:

  lcd.init();
}
void loop() {
 
// read the value from the moisture sensors:
moisture1_value = analogRead(moisture1);
moisture2_value = analogRead(moisture2);
moisture3_value = analogRead(moisture3);
moisture4_value = analogRead(moisture4);
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("Soil Moisture");
sensorValue = analogRead(moisture1);
sensorValue1 = analogRead(moisture2);
sensorValue2 = analogRead(moisture3);
sensorValue3 = analogRead(moisture4);
lcd.setCursor(0, 1);
lcd.print("Sen1 ");
lcd.setCursor(5, 1);
lcd.print(sensorValue);
lcd.setCursor(0,2);
lcd.print("Sen2 ");
lcd.setCursor(5, 2);
lcd.print(sensorValue1);
lcd.setCursor(12, 1);
lcd.print("Sen3 ");
lcd.setCursor(17,1);
lcd.print(sensorValue2);
lcd.setCursor(12,2);
lcd.print("Sen4 ");
lcd.setCursor(17,2);
lcd.print(sensorValue3);


//lcd.setCursor(0,2);
//lcd.print(sensorValue1);
//lcd.setCursor(0, 3);
//lcd.print(sensorValue2);
//lcd.setCursor(5, 2);
//lcd.print(sensorValue3);


// check which plant need water
// and open the switch for that specific plant

if(moisture1_value>=600){
  digitalWrite(relay1, HIGH);
}
if(moisture2_value>=600){
  digitalWrite(relay2, HIGH);
}
if(moisture3_value>=600){
  digitalWrite(relay3, HIGH);
}
if(moisture4_value>=600){
  digitalWrite(relay4, HIGH);
}

// make sure there is at least one plant that needs water
// if there is, open the motor
if(moisture1_value>=600 || moisture2_value>=600 || moisture3_value>=600 || moisture4_value>=600){
  digitalWrite(pump, HIGH);
}

// let it water the plant for 5 seconds
delay(5000);

// turn the pump off
digitalWrite(pump, LOW);

// go each switch and turn them off
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);

// wait 5 minutes and repeat the process
// delay(300000);
delay(1000);

}
Moderators note: Please use code tags for pieces of code
 

Attachments

Last edited by a moderator:

be80be

Joined Jul 5, 2008
2,072
You could use map and get human readable readings
int val = analogRead(0); val = map(val, 0, 1023, 0, 100);
then you could print it out 0 to 100 percent
 

Thread Starter

Pashgen

Joined Nov 16, 2017
23
That is awesome idea!
Going to do it asap!

I also was wandering how to show it as a bar or something like that?

Thank you be80be!
 
Top