simple sensor + relay code help

I have been asked by a very good friend who is now wheelchair bound to build a fully automated watering system, simple enough I thought, I have been building electronic projects for years now so this can’t be that hard, trouble is, I have not had to get into code writing before, apart from simple batch files for dosbox, the only other stuff like this I have had to deal with was in the early days of linux but that was just tracking libraries down and compiling stuff nothing like writing code yourself.

So, I have managed to work out what hardware i need:

Arduino mega

1 x 8 relay board

1 x 2 relay board

1 x display

1 x pump

9 x moisture sensors

1 x temp sensor

1 x flow meter

10 x solenoid controlled valves

11 way manifold

lots of pipe

and many many wires

simple system description

pump in water tank goes to manifold which has 10 solenoid controlled outlet valves, 9 valves outlet to plants 1 valve has return pipe to tank, all valves controlled by relay board, each plant has water sensor.

The idea is this, the first action for the system is to open valve 10 (return to tank pipe attached) and then activate the pump for 20 seconds to make sure system charged, then it turn off the pump and closes the valve, then it goes through a cycle where it checks each plant moisture sensor and if the sensor on a plant is saying dry then the appropriate valve opens and the pump is activated for a predetermined time, then the valve is closed and the next plant moisture sensor is checked and so on.

The temp sensor is to monitor feed tank, a water heater may be added later.

The flow meter is only to be activated when in the watering cycle and not a system charge cycle, this is to keep an eye on usage and on a charge cycle the water is not being used.

I have an Arduino mega 2560 and I have an 8 relay module and a moisture sensor that has it’s own little board with it that has 2 pins to the sensor and 4 pins to the Arduino, Ihave managed to wire this up and managed to get it to print the readings from the pins in the serial monitor in the official sketch software so I know it’s working ok

I have also managed to get the relays to connect to the arduino and all of them be off upon boot, then i have managed to get them to turn on and off again.

What I need to know is how do I get the sensor to trigger the relays I don’t seem to be able to get my head round getting the two to work together. I know i need in code a way of saying when the value of analog input 1 is say 400 then digitalWrite(RELAY1, LOW); but if analog input 1 is greater than 400 then digitalWrite(RELAY1, HIGH); but I can’t get it right, so it is time to ask someone who knows.

If there is someone out there who can point me in the right direction i would be most pleased, I will learn with your help.

sensor in plant pot registers plants state, when moist then leave and check next plant sensor, if

I’m not sure if I understand what’s needed but might this snippet be part of what you’re trying to do ?

loop(){


  //read the 1st moisture sensor and compare to turn on water threshold
  if(analogRead(sensor1Pin) < thrshOn){
    //turn on water by an active low to relay board
    digitalWrite(RELAY1, LOW);
  }
  //also check if moist enough to turn off water
  if(analogRead(sensor1Pin) > thrshOff){
    digitalWrite(RELAY1, HIGH);
  }
  //read the 2nd moisture sensor and compare to turn on water threshold
  if(analogRead(sensor2Pin) < thrshOn){
    //turn on water by an active low to relay board
    digitalWrite(RELAY2, LOW);
  }
  //also check if moist enough to turn off water
  if(analogRead(sensor2Pin) > thrshOff){
    digitalWrite(RELAY2, HIGH);
  }
... insert code for rest of sensors here...
  //wait some seconds for watering to have an effect
  delay(wait1time);

}

You’ll need to define some constants and pin names and I’ve yet to show your priming operation but the above is the start of an idea I think. Also the above could be more compactly written using a for loop but the above is easier to grasp. The for loop coding may better integrate w/a priming sequence and I can show you that if/when desired.

ps - I included some hysteresis btw on and off so the relays aren’t chattering on noise. I’ve assumed a higher A/D reading is more moist and so thrshOff should be > thrshOn.

Thankyou for your input, so far I have managed to cobble this together which seems to work but i am sure there are better ways but trying to understand what it is doing is important so I have more commands to use.

int sensor1 = A0; // select the input pin for sensor 1

int sensorValue1 = 0; // variable to store the value coming from sensor 1

#define RELAY1 2

#define RELAY10 11

#define PUMP 12

void setup()

{

// Initialise the Arduino data pins for OUTPUT

pinMode(RELAY1, OUTPUT);

pinMode(RELAY10, OUTPUT);

pinMode(PUMP, OUTPUT);

digitalWrite(RELAY1, HIGH); // This sets relays to off state on start up

digitalWrite(RELAY10, HIGH);

digitalWrite(PUMP, HIGH);

Serial.begin(9600); // initialize serial communications at 9600 bps

Serial.print("Watering System version 1.0 " );

Serial.println();

Serial.print("System Charging " );

Serial.println();

digitalWrite(RELAY10, LOW);

delay(2000);

digitalWrite(PUMP, LOW);

delay(20000);

digitalWrite(PUMP, HIGH);

delay(3000);

digitalWrite(RELAY10, HIGH);

delay(2000);

Serial.print("System Ready " );

Serial.println();

}

void loop()

{

Serial.print("Cycle Starting " ); // print to screen

Serial.println();

delay(2000); // pause for 2 seconds

// start of plant1 cycle

sensorValue1 = analogRead(sensor1); // read the value from sensor1:

delay(1000); // pause for 1 second

Serial.print("sensor 1 = " ); // print to screen

Serial.println();

Serial.println(sensorValue1); // print sensor value to screen

Serial.println();

if (sensorValue1 < 500) // if sensor value greater than figure

// then do the following

{

digitalWrite(RELAY1, HIGH); // keep valve closed

}

if (sensorValue1 > 500) // if sensor value is less than figure

// then do the following

{

Serial.print("Plant 1 Needs Water " ); // print to screen

Serial.println();

delay(2000); // pause for 2 seconds

digitalWrite(RELAY1, LOW); // open water valve 1

delay(2000); // pause for 2 seconds

Serial.print("Activating Pump " ); // print to screen

Serial.println();

delay(2000); // pause for 2 seconds

digitalWrite(PUMP,LOW); // turn on the pump

delay(5000); // pause for 5 seconds

digitalWrite(PUMP,HIGH); // turn off the pump

delay(5000); // pause for 5 seconds

digitalWrite(RELAY1, HIGH); // close water valve 1

delay(2000); // pause for 2 seconds

Serial.print("Watering Complete " ); // print to screen

Serial.println();

delay(2000); // pause for 2 seconds

}

else //otherwise

digitalWrite(RELAY1, HIGH); // keep valve 1 closed

Serial.print("Plant 1 is Happy " ); // print to screen

Serial.println();

delay(2000); // pause for 2 seconds

// end of plant1 cycle, onto the next

What I need to add is when a reading from the water tank is saying water too cold then the cycle will not start until certain conditions have been met, so if the safe temp is between 12 and 16 degrees then it needs to know that if it is below this then not to run through the watering cycle but wait until temp is reached, not learned this yet so any help would be good.

Label:

Wait a few minutes

Test water temp

If water temp to cold goto Label

Continue with program

With a Label you can get the code to just loop until a criteria is met. At the start of each cycle it will test the water temp and keep on testing it until its warm enough. If the water NEVER gets to temp then this part of the program just becomes an infinate loop with no way out. Yo could add a delay of a few minutes to slow it down.

You could do with some sort of count for the plants you are looking after otherwise you will have to repeat the code over and over again.

Program in Pascal usually so this is all a bit new. Looks like good fun though. :smiley: