Irrigation system code. Help please!

I have written a code for a project,parts of it function as expected, the temperature sensor worked to start with but has since stopped working... is anyone able to point me in the right direction please? What I have written is below. The pump is powered by a seperate power supply and is just switched bythe Arduino.

int soil = A0;              //defines pin A0 as DFROBOT Soil moisture sensor
float soilValue;        //sets the value bTopicefore reading the sensor to 0
int temp = A1;              //defines pin A1 as LM35 temperature sensor.
float tempValue;        //sets the value before reading the sensor to 0
int pump = A2;               //defines pin A2 as the water pump

void setup() {
  // declare pin values (output or input)  
  pinMode (soil, INPUT);     //make the soil moisture sensor an input
  pinMode (temp, INPUT);     //makes the temperature sensor an input
  pinMode (pump, OUTPUT);   //makes the pump an output.

  Serial.begin (9600);       //initializes the serial communication at 9600 bits per second
  }

void loop() {
// read input pins:
  int soilValue = analogRead (soil);
  
  int temp_Value;
  float tempValue;
  temp_Value = analogRead (temp);
  temp_Value = (temp_Value/1024)*5000;
  tempValue = (temp_Value/10);

  

// print input data to serial monitor
// moisture sensor
  Serial.print("Soil Moisture Level: ");
  Serial.println(soilValue);                                              // prints out the state of the soil moisture sensor
// Temperature sensor
  Serial.print ("Temperature: ");
  Serial.println (tempValue);                                               // prints out the value of the temperature.
  Serial.print (" Degree Celsius\n"); 

// fuzzy logic
  if ((soilValue <= 300) && (tempValue >= 20 && tempValue <= 10))         // takes a reading of both the Soil and Temp values and applies IF rules.
     {
    digitalWrite (pump, HIGH);                                             // if the rule is satisfied, the pump is activated.
    delay (15000);                                                        // keeps the pump active for 30 s
   } 
  else if ((soilValue <= 300) && (tempValue <= 20))                           // takes a reading of both the Soil and Temp values and applies IF rules.
     {
      digitalWrite (pump, HIGH);
      delay (30000); 
     }
  else if ((soilValue >= 300) && (soilValue <= 700 && tempValue <= 20))    // takes a reading of both the Soil and Temp values and applies IF rules.
       {
        digitalWrite (pump, HIGH);
        delay (30000);
       }
    digitalWrite (pump, LOW);                                              // for all other possibilities, pump is turned off.
    delay (1800000);                                                      // pauses the system for 30 minutes before taking more readings
   

}

Did you forget to use code tags when posting code?

What does stopped working mean?

Are you using an Uno? With an Uno what's the largest int value that one can use? Does the largest int value one can use with an Uno can use exceeded 1800000? Do you know about unsigned longs? What is the largest value with an Uno of an unsigned long?

Did you try delay(1800000UL)?

Did you try using millis() for timing? <<<The better option.

1 Like

Was there a code change that caused the problem?

I'd print the raw value you're getting from the analogRead as a first check.

1 Like

Hi Idahowalker,

I may well have forgotten to use tags, apologies I'm new here so still working things out!

The sensor was giving accurate (according to a separate thermometer) readings in the serial monitor, but has just started giving 0 usually or a reading that is obviously a long way out, for example the I get 0's most of the time but will occassionally get a reading of 7 or even 50 which obviously isn't close to being accurate.

I will try using millois instead of delays for the timin, thank you for the tip.

The code for that section hasn't changed at all, but I did change the code for the water pump from a digital pin to an analogue pin. It could of course be either a loose connection on the breadboard I am using, although I have tried other areas of the board with the component and get similar results. I will try printing the raw value before making any of the numerical adjustments.

I may try taking that section of the code and trying it isolated as well.

There is only one ADC on the board, so all analog pins use the same one. I have never heard of problems using an analog pin for digital but on the basis that it was the last change, it's a little suspicious.

How much current does the pump switching circuit pull?

wildbill, the pump draws more current than the arduino can supply but it is on a separate power supply so that shouldn't be causing any issues. The pump is wired similarly to the link below

Needs a 5mA current for switching signal so should be well within the limits of the Arduino!

Yes, but then again, no. You are also powering the relay coil from the Arduino and they can pull considerably more current. Worse, the manufacturers have a tendency to understate how much that is. For a single relay, it might be as much as 90mA if you're really unlucky, more likely ~30.

Would it be a good idea to use the same separate power supply for the relay as well? Just use the arduino to supply a switching signal?

I don't think so, powering both sides from the same supply rather defeats the point of the relay I would say.

Just remove the relay entirely and check whether your temperature readings are corrected.

You tried. Edit your post, it's not three apostrophes (single quote) it's three of some other symbol that looks like that.

''' single quote, no

``` tick, yes.

It's on your keyboard somewhere. Look at the preview window you see you get it right, before and after your code.

a7

1 Like

Click the edit :pencil2: down here :point_down: then highlight code and use the code tags <|> up here :point_up_2:then click save

Thank you! Just edited it so hopefully it's looking better now?

Perfect, much easier for others to read and help

1 Like

Thank you for your advise I really appreciate it :slight_smile:

Update, I tested the temp sensor on its own.. below are the readings I am getting, these are approximately 1 second apart so clearly not accurate. Sample of the test code is below!

int val;
int tempPin = 1;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000;
  float cel = mv/10;
  Serial.print("TEMPRATURE = ");
  Serial.print(cel);
  Serial.print("*C");
  Serial.println();
  delay(1000);
*/
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.