Watering pump with LCD, arduino based. need advice plz

Discussion in 'General Electronics Chat' started by Pashgen, Nov 16, 2017.

  1. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    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:
    Code (C):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. LiquidCrystal_I2C lcd(0x27,20,4);
    4.  
    5. int PUMP = 13;
    6. int sensor = 8;
    7. int val;
    8. const int DigitalInPin = 8;
    9. int sensorValue = digitalRead(8);
    10. void setup() {
    11.   pinMode(13,OUTPUT);
    12.   pinMode(8,INPUT);
    13. lcd.init();  // initialize the lcd
    14. lcd.init();
    15. delay(100);
    16. }
    17. void loop() {
    18.   val = digitalRead(8);
    19.  
    20.   if(val ==LOW)
    21.   {
    22.   digitalWrite(13,LOW);
    23.   }
    24.   else
    25.   {
    26.   digitalWrite(13,HIGH);
    27.   }
    28.   delay(400);
    29.  
    30. lcd.backlight();
    31. lcd.setCursor(0,0);
    32. lcd.print("Soil Moisture:");
    33. sensorValue = digitalRead(8);
    34. lcd.setCursor(0, 1);
    35. lcd.print(sensorValue);
    36. }
     
    Last edited by a moderator: Dec 7, 2017 at 2:15 AM
  2. dendad

    Active Member

    Feb 20, 2016
    823
    207
    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?
     
    Pashgen likes this.
  3. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    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
     
  4. dendad

    Active Member

    Feb 20, 2016
    823
    207
    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: Nov 16, 2017
    Pashgen likes this.
  5. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    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:

    Code (C):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. LiquidCrystal_I2C lcd(0x27,20,4);
    4.  
    5. int PUMP = 13;
    6. //int sensor = 8;
    7. int sensor = A0;
    8. int val;
    9. const int DigitalInPin = 8;
    10. int sensorValue = digitalRead(8);
    11. void setup() {
    12.   pinMode(13,OUTPUT);
    13.   pinMode(8,INPUT);
    14. lcd.init();  // initialize the lcd
    15. lcd.init();
    16. delay(100);
    17. }
    18. void loop() {
    19.   val = digitalRead(8);
    20.  
    21.   if(val ==LOW)
    22.   {
    23.   digitalWrite(13,LOW);
    24.   }
    25.   else
    26.   {
    27.   digitalWrite(13,HIGH);
    28.   }
    29.   delay(400);
    30.  
    31. lcd.backlight();
    32. lcd.setCursor(0,0);
    33. lcd.print("Soil Moisture:");
    34. //sensorValue = digitalRead(8);
    35. sensorValue = analogRead(sensor);
    36. lcd.setCursor(0, 1);
    37. lcd.print(sensorValue);
    38. }
     
    Last edited by a moderator: Dec 7, 2017 at 2:15 AM
  6. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    here is how it looks on LCD
     
  7. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    Thank you so much dendad!!!
    Legend!
     
  8. dendad

    Active Member

    Feb 20, 2016
    823
    207
    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);
    }
     
  9. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    awesome!
    all done!

    thank you very much!!!!!!!
     
  10. dendad

    Active Member

    Feb 20, 2016
    823
    207
    Glad to be of help :)
     
    JohnInTX likes this.
  11. dendad

    Active Member

    Feb 20, 2016
    823
    207
  12. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
  13. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    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:
    Code (C):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. LiquidCrystal_I2C lcd(0x27,20,4);
    4.  
    5. // set all moisture sensors PIN ID
    6. int moisture1 = A0;
    7. int moisture2 = A1;
    8. int moisture3 = A2;
    9. int moisture4 = A3;
    10.  
    11. // declare moisture values
    12. int moisture1_value = 0;
    13. int moisture2_value = 0;
    14. int moisture3_value = 0;
    15. int moisture4_value = 0;
    16.  
    17. int sensorValue = moisture1;
    18. int sensorValue1 = moisture2;
    19. int sensorValue2 = moisture3;
    20. int sensorValue3 = moisture4;
    21.  
    22. // set water relays
    23. int relay1 = 3;
    24. int relay2 = 4;
    25. int relay3 = 5;
    26. int relay4 = 6;
    27. // set water pump
    28. int pump = 2;
    29. void setup() {
    30.   // declare relay as output
    31.   pinMode(relay1, OUTPUT);
    32.   pinMode(relay2, OUTPUT);
    33.   pinMode(relay3, OUTPUT);
    34.   pinMode(relay4, OUTPUT);
    35.   // declare pump as output
    36.   pinMode(pump, OUTPUT);
    37.   // declare the ledPin as an OUTPUT:
    38.   Serial.begin(9600);
    39.   lcd.init();
    40. }
    41. void loop() {
    42.  
    43. // read the value from the moisture sensors:
    44. moisture1_value = analogRead(moisture1);
    45. moisture2_value = analogRead(moisture2);
    46. moisture3_value = analogRead(moisture3);
    47. moisture4_value = analogRead(moisture4);
    48. lcd.backlight();
    49. sensorValue = analogRead(moisture1);
    50. sensorValue1 = analogRead(moisture2);
    51. sensorValue2 = analogRead(moisture3);
    52. sensorValue3 = analogRead(moisture4);
    53. lcd.setCursor(0, 0);
    54. lcd.print(sensorValue);
    55. lcd.setCursor(7,0);
    56. lcd.print(sensorValue1);
    57. lcd.setCursor(0, 1);
    58. lcd.print(sensorValue2);
    59. lcd.setCursor(7,1);
    60. lcd.print(sensorValue3);
    61.  
    62. // check which plant need water
    63. // and open the switch for that specific plant
    64.  
    65. if(moisture1_value>=900){
    66.   digitalWrite(relay1, HIGH);
    67. }
    68. if(moisture2_value>=900){
    69.   digitalWrite(relay2, HIGH);
    70. }
    71. if(moisture3_value>=900){
    72.   digitalWrite(relay3, HIGH);
    73. }
    74. if(moisture4_value>=900){
    75.   digitalWrite(relay4, HIGH);
    76. }
    77.  
    78. // make sure there is at least one plant that needs water
    79. // if there is, open the motor
    80. if(moisture1_value>=900 || moisture2_value>=900 || moisture3_value>=900 || moisture4_value>=900){
    81.   digitalWrite(pump, HIGH);
    82. }
    83.  
    84. // let it water the plant for 5 seconds
    85. delay(5000);
    86.  
    87. // turn the pump off
    88. digitalWrite(pump, LOW);
    89.  
    90. // go each switch and turn them off
    91. digitalWrite(relay1, LOW);
    92. digitalWrite(relay2, LOW);
    93. digitalWrite(relay3, LOW);
    94. digitalWrite(relay4, LOW);
    95.  
    96. // wait and repeat the process
    97.  
    98. delay(1000);
    99.  
    100. }
     
    Last edited by a moderator: Dec 7, 2017 at 2:14 AM
  14. ebeowulf17

    Well-Known Member

    Aug 12, 2014
    1,329
    214
    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.
     
  15. dendad

    Active Member

    Feb 20, 2016
    823
    207
    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: Dec 6, 2017 at 7:45 PM
  16. dendad

    Active Member

    Feb 20, 2016
    823
    207
    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);
     
  17. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    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

    Code (C):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. LiquidCrystal_I2C lcd(0x27,20,4);
    4. #define relay1 3
    5. #define relay2 4
    6. #define relay3 5
    7. #define relay4 6
    8. #define pump 2
    9. // set all moisture sensors PIN ID
    10. int moisture1 = A0;
    11. int moisture2 = A1;
    12. int moisture3 = A2;
    13. int moisture4 = A3;
    14.  
    15. // declare moisture values
    16. int moisture1_value = 0;
    17. int moisture2_value = 0;
    18. int moisture3_value = 0;
    19. int moisture4_value = 0;
    20.  
    21. int sensorValue = moisture1;
    22. int sensorValue1 = moisture2;
    23. int sensorValue2 = moisture3;
    24. int sensorValue3 = moisture4;
    25.  
    26. // set water relays
    27. //int relay1 = 3;
    28. //int relay2 = 4;
    29. //int relay3 = 5;
    30. //int relay4 = 6;
    31.  
    32. // set water pump
    33. //int pump = 2;
    34. //
    35. void setup() {
    36. digitalWrite(relay1, HIGH);
    37. delay(1000);
    38. digitalWrite(relay1, LOW);
    39. delay(1000);
    40. digitalWrite(relay2, HIGH);
    41. delay(1000);
    42. digitalWrite(relay2, LOW);
    43. delay(1000);
    44. digitalWrite(relay3, HIGH);
    45. delay(1000);
    46. digitalWrite(relay3, LOW);
    47. delay(1000);
    48. digitalWrite(relay4, HIGH);
    49. delay(1000);
    50. digitalWrite(relay4, LOW);
    51. delay(1000);
    52. digitalWrite(relay1, LOW);
    53. digitalWrite(relay2, LOW);
    54. digitalWrite(relay3, LOW);
    55. digitalWrite(relay4, LOW);
    56. digitalWrite(pump, LOW);
    57.  
    58.   // declare relay as output
    59.   pinMode(relay1, OUTPUT);
    60.   pinMode(relay2, OUTPUT);
    61.   pinMode(relay3, OUTPUT);
    62.   pinMode(relay4, OUTPUT);
    63.   // declare pump as output
    64.   pinMode(pump, OUTPUT);
    65.   // declare the ledPin as an OUTPUT:
    66.   Serial.begin(9600);
    67.   lcd.init();
    68. }
    69. void loop() {
    70.  
    71. // read the value from the moisture sensors:
    72. moisture1_value = analogRead(moisture1);
    73. moisture2_value = analogRead(moisture2);
    74. moisture3_value = analogRead(moisture3);
    75. moisture4_value = analogRead(moisture4);
    76. lcd.backlight();
    77. sensorValue = analogRead(moisture1);
    78. sensorValue1 = analogRead(moisture2);
    79. sensorValue2 = analogRead(moisture3);
    80. sensorValue3 = analogRead(moisture4);
    81. lcd.setCursor(0, 0);
    82. lcd.print(sensorValue);
    83. lcd.setCursor(7,0);
    84. lcd.print(sensorValue1);
    85. lcd.setCursor(0, 1);
    86. lcd.print(sensorValue2);
    87. lcd.setCursor(7,1);
    88. lcd.print(sensorValue3);
    89.  
    90. // check which plant need water
    91. // and open the switch for that specific plant
    92.  
    93. if(moisture1_value>=600){
    94.   digitalWrite(relay1, HIGH);
    95. }
    96. if(moisture2_value>=600){
    97.   digitalWrite(relay2, HIGH);
    98. }
    99. if(moisture3_value>=600){
    100.   digitalWrite(relay3, HIGH);
    101. }
    102. if(moisture4_value>=600){
    103.   digitalWrite(relay4, HIGH);
    104. }
    105.  
    106. // make sure there is at least one plant that needs water
    107. // if there is, open the motor
    108. if(moisture1_value>=600 || moisture2_value>=600 || moisture3_value>=600 || moisture4_value>=600){
    109.   digitalWrite(pump, HIGH);
    110. }
    111.  
    112. // let it water the plant for 5 seconds
    113. delay(5000);
    114.  
    115. // turn the pump off
    116. digitalWrite(pump, LOW);
    117.  
    118. // go each switch and turn them off
    119. digitalWrite(relay1, LOW);
    120. digitalWrite(relay2, LOW);
    121. digitalWrite(relay3, LOW);
    122. digitalWrite(relay4, LOW);
    123.  
    124. // wait and repeat the process
    125.  
    126. delay(1000);
    127.  
    128. }
     
    Last edited by a moderator: Dec 7, 2017 at 2:13 AM
  18. Pashgen

    Thread Starter New Member

    Nov 16, 2017
    14
    1
    I did check relay with relay example sketches. relay shield is working fine, all 4 relays responded
     
  19. dendad

    Active Member

    Feb 20, 2016
    823
    207
    Why are you still reading twice?
    And you are not consistent in your numbering.


    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.
     
  20. ebeowulf17

    Well-Known Member

    Aug 12, 2014
    1,329
    214
    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?
     
Loading...