Soil Moisture Watering System

Hello and Many Thanks in advance if you can help!

I am extremely new (started learning this week) to Arduino and coding in general and I am on a mission to code a Solenoid Valve to open and shut according to a soil moisture sensor reading. I have made the circuit board but I am really stuck with the coding and was wondering if anyone can help!

Here is what I have compiled looking at others projects: (probably all over the shop!)

int solenoidPin = 4; //This is the output pin on the Arduino we are using
int sensor_pin = A0; //setting the sensor pin
int output_value ;
int moisture; //the variable to control

void setup(){
Serial.begin(9600); //must begin to use serial monitor
Serial.println(“Reading From the Sensor …”);
pinMode (sensor_pin,INPUT); //setting sensor pin to be input
pinMode(solenoidPin,OUTPUT); //Sets the pin as an output
}

void loop(){
// put your main code here, to run repeatedly:
moisture = analogRead(sensor_pin); //read the value from sensor pin
output_value = map(output_value,550,0,0,100);
Serial.println(moisture); //show the value receive in serial monitor
Serial.print(output_value);
Serial.println("%");
delay(1000);
if(moisture<=60){ //the critical value to trigger the solenoid
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
}
else{
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Seconds
}
}

Many Thanks
K

What happens when you try it?

Does your code compile and load? If it loads what exactly does it do? And what do you want it to do that’s different? What are your Serial prints showing you is happening? What is 'output_value" for? You calculate it then never use it.

What type of solenoid valve and how is it connected? Many solenoids need more current than an Arduino pin can safely supply.

Steve

Thanks for having a look at this:

Here are pictures of my circuit so far and i am using a 12V power supply into the DC and using a 12v LED in place of the 12V Solenoid valve for testing purposes:

The issue I am having is that the LED (what will be the valve) does not turn off regardless of the moisture sensor being in or out of water.

With regard to the output value. I was trying to turn the analog reading into a percentage so that when to moisture gets below 60% it turns of the LED (what will be the valve).

Many Thanks
Katie

3.jpg

4.jpg

KMayhew:
With regard to the output value. I was trying to turn the analog reading into a percentage so that when to moisture gets below 60% it turns of the LED (what will be the valve).

Look again at what slipstick wrote. You calculate output_value, but you still use moisture in your if.

What do your serial prints tell you that they are?

As wildbill says, your if statement should use (output_value <= 60) NOT moisture. And you haven’t said what values you’re seeing from your Serial.prints.

A circuit diagram (hand drawn is fine) would be useful but I can’t really tell anything from those photos.

But if you’re trying to drive a 12V LED or solenoid direct from an Arduino digital pin it’s not going to work. And you’re likely to kill that pin or possibly the whole Arduino.

Steve

The serial prints are looking like this when the sensor is dry 102384% 361884% when wet and and iv attached a picture of the circuit diagram

When the arduino is plugged in the LED is permanently on and does not turn off when moisture sensor enters water

Thanks

Have you changed your code to use output_value instead of moisture yet?

Please post your updated code in code tags.

Code is now looking like this:

Would you mind highlighting which bit I should change?

int solenoidPin = 4; //This is the output pin on the Arduino we are using
int sensor_pin = A0; //setting the sensor pin
int output_value ;
int moisture; //the variable to control

void setup(){
Serial.begin(9600); //must begin to use serial monitor
Serial.println(“Reading From the Sensor …”);
pinMode (sensor_pin,INPUT); //setting sensor pin to be input
pinMode(solenoidPin,OUTPUT); //Sets the pin as an output
}

void loop(){
// put your main code here, to run repeatedly:
moisture = analogRead(sensor_pin); //read the value from sensor pin
output_value = map(output_value,550,0,0,100);
Serial.print(moisture); //show the value receive in serial monitor
Serial.print(output_value);
Serial.println("%");
delay(1000);
if(moisture<=600){ //the critical value to trigger the solenoid
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
}
else{
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Seconds
}
}

So you're still not using output_value? What does your new code do?

And for the 3rd time of asking, what do the Serial.prints show? E.g. when the sensor is dry and when it is wet? Basically how do you know the sensor is actually doing anything?

Steve

Water valve is now permanently off

When sensor is dry Serial Print is:
Moisture Reading: 1023
Moisture Percentage: 0%

When sensor is wet Serial Print is:
Moisture Reading: 1023
Moisture Percentage: 0%

And Code Now looks like this:

int solenoidPin = 4; //This is the output pin on the Arduino we are using
int sensor_pin = A0; //setting the sensor pin
int output_value;
int moisture; //the variable to control

void setup(){
Serial.begin(9600); //must begin to use serial monitor
Serial.println("Reading From the Sensor ...");
pinMode(sensor_pin,INPUT); //setting sensor pin to be input
pinMode(solenoidPin,OUTPUT); //Sets the pin as an output
}

void loop(){
// put your main code here, to run repeatedly:
moisture = analogRead(sensor_pin); //read the value from sensor pin
output_value = map(moisture,1023,0,0,100);
Serial.print("Moisture Reading: ");
Serial.println(moisture); //show the value receive in serial monitor
Serial.print("Moisture Percentage: ");
Serial.print(output_value);
Serial.println("%");
delay(200);
if(output_value>=60){ //the critical value to trigger the solenoid
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
}
else{
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(200); //Wait
}
}

Not really surprising: your sensor can't tell the difference between wet and dry!

No point messing with the code until you get that working.

Sorry copy and paste error on my behalf!

When sensor is wet it is:
Moisture Reading: 384
Moisture Percentage: 62%

Post your code in code tags

Apologies, I only signed up today so need to read up on how to do this!

int solenoidPin = 4;    //This is the output pin on the Arduino we are using
int sensor_pin = A0; //setting the sensor pin
int output_value;
int moisture; //the variable to control

void setup(){
  Serial.begin(9600); //must begin to use serial monitor
  Serial.println("Reading From the Sensor ...");
  pinMode(sensor_pin,INPUT); //setting sensor pin to be input
  pinMode(solenoidPin,OUTPUT); //Sets the pin as an output
}


void loop(){
  // put your main code here, to run repeatedly:
  moisture = analogRead(sensor_pin); //read the value from sensor pin
  output_value = map(moisture,1023,0,0,100);
  Serial.print("Moisture Reading: ");
  Serial.println(moisture); //show the value receive in serial monitor
  Serial.print("Moisture Percentage: ");
  Serial.print(output_value);
  Serial.println("%");
  delay(200);
  if(output_value>=60){ //the critical value to trigger the solenoid
    digitalWrite(solenoidPin, HIGH);    //Switch Solenoid ON
  }
  else{
    digitalWrite(solenoidPin, LOW);     //Switch Solenoid OFF
    delay(200);                      //Wait 
  }
}

O.k. that latest code looks like it should work. If it doesn’t there’s probably a wiring or power problem.

Your diagram doesn’t show any LED and it doesn’t show any 12V supply so it doesn’t seem to bear much relationship to what you’re describing. What are the components (power supply, transistor, diode, resistor) you’re using? How exactly are they connected?

Steve