Watering pump with LCD, arduino based. need advice plz

Thread Starter

Pashgen

Joined Nov 16, 2017
23
Hi guys and girls,

could you please help me with my project?
I want to show current readings from sensor on LCD.
watering is working and everything else, I just couldn't get sensor value on LCD.
I also wonder how can I show it as percentage on LCD. For instance - completely wet soil = 100% , medium = 50%, super dry = 0%. yeah, something like that.
here is code:
C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

int PUMP = 13;
int sensor = 8;
int val;
const int DigitalInPin = 8;
int sensorValue = digitalRead(8);
void setup() {
  pinMode(13,OUTPUT);
  pinMode(8,INPUT);
lcd.init();  // initialize the lcd
lcd.init();
delay(100);
}
void loop() {
  val = digitalRead(8);
 
  if(val ==LOW)
  {
  digitalWrite(13,LOW);
  }
  else
  {
  digitalWrite(13,HIGH);
  }
  delay(400);

lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Soil Moisture:");
sensorValue = digitalRead(8);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
}
 

Attachments

Last edited by a moderator:

dendad

Joined Feb 20, 2016
4,481
For a start, the digitalRead will only give you a 1 or 0.
Is the sensor you are using capable of analog outputs?
If not, you need to change to a different sensor and use analogRead to get a variable reading.
So what is the data on the sensor you have?
 

Thread Starter

Pashgen

Joined Nov 16, 2017
23
not much data to be honest. just that - Soil Hygrometer Humidity Detection Module Moisture Water Sensor Soil Moisture for arduino Diy Kit

and picture
looks like similar types of sensors,
also same connections.

It is connected to digital pin 8. somehow its the only way I got actual pump working. So sensor is doing its job and detecting moisture etc.
So everything is working, but LCD showing 1 or 0 (now I know coz its the only value it can actually show, coz its digital pin). I want to change to something more sensible. maybe even show readings as a %, for instance - very wet 100%, super dry - 0%, lol
As I understand I have to connect it to analogue input, lets say A5. I tried that, I got value readings.
But I couldn't get pump working. I tried to change the code but not success. Sorry, I don't have much experience with that code or arduino :( I just started like a day ago
 

Attachments

dendad

Joined Feb 20, 2016
4,481
If the sensor is an analog out, try something like taking a reading on a analog input with it in water. That will be 100%.

Use this mod to your code...
int sensor = A0; // connect the sensor"AC" pin to A0
.
.
.
.
sensorValue = analogRead(sensor);
lcd.setCursor(0, 1);
lcd.print(sensorValue);

and try this in various levels of moisture to see if you get a reading.
I found some info http://www.instructables.com/id/Soil-Moisture-Sensor/ that may help you. It looks to have an analog out (AC pin) and a digital out (DC pin).
 
Last edited:

Thread Starter

Pashgen

Joined Nov 16, 2017
23
yes!
it is working!!!!!
so when dry its showing 1023
after I put in water (will get soil later), its showing 300-400 but last digit from 1023 stays. so its showing 4something3.
how to wipe that last digit? I mean when value changed.

but YEAH! so happy its showing it on LCD
here is modified code:

C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

int PUMP = 13;
//int sensor = 8;
int sensor = A0;
int val;
const int DigitalInPin = 8;
int sensorValue = digitalRead(8);
void setup() {
  pinMode(13,OUTPUT);
  pinMode(8,INPUT);
lcd.init();  // initialize the lcd
lcd.init();
delay(100);
}
void loop() {
  val = digitalRead(8);
 
  if(val ==LOW)
  {
  digitalWrite(13,LOW);
  }
  else
  {
  digitalWrite(13,HIGH);
  }
  delay(400);

lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Soil Moisture:");
//sensorValue = digitalRead(8);
sensorValue = analogRead(sensor);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
}
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
4,481
Here is one way...
.
.
.
lcd.setCursor(0, 1); // start of line
lcd.print(" "); // blank out old reading with 5 spaces
lcd.setCursor(0, 1); // back to start of line again
lcd.print(sensorValue);
}
 

Thread Starter

Pashgen

Joined Nov 16, 2017
23
Really need more help please!

I am taking my project to the next level and adding valves.
The idea is to use one pump and 4 valves, 4 sensors.
So watering system can take care of 4 plants.

I am using 4 relay shield for valves. One separate relay for the pump.

Its not working the way I want. Basically, it worked properly only for on sensor.

Could you please have a look at my code:
C:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,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;
// set water pump
int pump = 2;
void setup() {
  // 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();
}
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();
sensorValue = analogRead(moisture1);
sensorValue1 = analogRead(moisture2);
sensorValue2 = analogRead(moisture3);
sensorValue3 = 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>=900){
  digitalWrite(relay1, HIGH);
}
if(moisture2_value>=900){
  digitalWrite(relay2, HIGH);
}
if(moisture3_value>=900){
  digitalWrite(relay3, HIGH);
}
if(moisture4_value>=900){
  digitalWrite(relay4, HIGH);
}

// make sure there is at least one plant that needs water
// if there is, open the motor
if(moisture1_value>=900 || moisture2_value>=900 || moisture3_value>=900 || moisture4_value>=900){
  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);

}
 

Attachments

Last edited by a moderator:

ebeowulf17

Joined Aug 12, 2014
3,307
Really need more help please!

I am taking my project to the next level and adding valves.
The idea is to use one pump and 4 valves, 4 sensors.
So watering system can take care of 4 plants.

I am using 4 relay shield for valves. One separate relay for the pump.

Its not working the way I want. Basically, it worked properly only for on sensor.

Could you please have a look at my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,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;
// set water pump
int pump = 2;
void setup() {
// 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();
}
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();
sensorValue = analogRead(moisture1);
sensorValue1 = analogRead(moisture2);
sensorValue2 = analogRead(moisture3);
sensorValue3 = 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>=900){
digitalWrite(relay1, HIGH);
}
if(moisture2_value>=900){
digitalWrite(relay2, HIGH);
}
if(moisture3_value>=900){
digitalWrite(relay3, HIGH);
}
if(moisture4_value>=900){
digitalWrite(relay4, HIGH);
}

// make sure there is at least one plant that needs water
// if there is, open the motor
if(moisture1_value>=900 || moisture2_value>=900 || moisture3_value>=900 || moisture4_value>=900){
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);

}
What is and isn't working on the remaining sensors?
  • Do sensor readings look right on display?
  • Do valves open properly, but pump only activates for one valve?
Two thoughts on your code:
  • Why use two different sets of variables for analog sensor readings when they're exactly the same?
  • You never declare analog input pins as inputs, nor do you explicitly declare internal pullups on or off for those pins.
 

dendad

Joined Feb 20, 2016
4,481
For a start, why do you need moistureX_values and sensorX_values? They are both the same.

Another thing, use #define not int, as it does not use memory space..

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

You need to put this in the setup too. Then all the outputs start at a known state. That is always a good idea....

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

I can't spot why it is not going as yet. Test all the hardware with a simple cycle the output test loop.
 
Last edited:

dendad

Joined Feb 20, 2016
4,481
Something like this to make sure it works.
In fact, a 100mS version could be put in your startup code as a built in test every time it starts.

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

Thread Starter

Pashgen

Joined Nov 16, 2017
23
thnks guys. I changed the code.
Sensor on A2 working fine, pump relay and valve relay turns on/off
Sensor on A3 is not working at all. I tried to swap sensors, didn't help. I checked with multimeter - no wiring faults.
Sensor on A1 is good - pump relay and valve relay turns on/off
Sensor on A0 switches on only pump relay, but nothing at shield relay, which I think is number 4 relay. However it shows value on screen properly.

could you please help

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
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;

// 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();
}
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();
sensorValue = analogRead(moisture1);
sensorValue1 = analogRead(moisture2);
sensorValue2 = analogRead(moisture3);
sensorValue3 = 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);

}
 
Last edited by a moderator:

dendad

Joined Feb 20, 2016
4,481
Why are you still reading twice?
And you are not consistent in your numbering.

  • // 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();
  • sensorValue = analogRead(moisture1);
  • sensorValue1 = analogRead(moisture2);
  • sensorValue2 = analogRead(moisture3);
sensorValue3 = analogRead(moisture4);

You have the numbering different between sensor values and moisture values.

Change it to...
// read the value from the moisture sensors:

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

...only and remove all references to sensor values. Change them all to moisture values.
And put the lcd.backlight(); in the setup.
 

ebeowulf17

Joined Aug 12, 2014
3,307
You still don't have pinMode commands for your analog inputs. This might explain the input where you get no sensor readings.

As for the relay that doesn't fire, does that relay fire during the test sequence you added in the beginning, but not in response to sensor changes, or does it never fire at all?
 
Top