Newb needs help

Hi All, I am building a small project for my kids, i am using the analogue moisture soil sensor and i have this working ok. (i know there are many of these projects but they all seem to use the digital interface board).
I have a 4 channel relay which i can get to work on its own but i am struggling with reading the analog value and have it turning on a relay. If i can get this to work i can incorporate other things thanks in advance paul.

/* Soil Mositure Basic Example
This sketch was written by SparkFun Electronics
Joel Bartlett
August 31, 2015

Basic skecth to print out soil moisture values to the Serial Monitor

Released under the MIT License(The MIT License – Open Source Initiative)
*/

int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power

//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.
// define names for the 4 Digital pins On the Arduino

// These data pins link to 4 Relay board pins IN1, IN2, IN3,IN4

#define RELAY1 3

#define RELAY2 4

#define RELAY3 5

#define RELAY4 6

void setup()
{
Serial.begin(9600); // open serial over USB

pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
}

void loop()
{
// Initialise the Arduino data pins for OUTPUT

pinMode(RELAY1, OUTPUT);

pinMode(RELAY2, OUTPUT);

pinMode(RELAY3, OUTPUT);

pinMode(RELAY4, OUTPUT);

Serial.print("Soil Moisture = ");
//get soil moisture value from the function below and print it
Serial.println(readSoil());

//This 1 second timefrme is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(1000);//take a reading every second
}
//This is a function used to get the soil moisture content
int readSoil()
{

digitalWrite(soilPower, HIGH);//turn D7 "On"
delay(10);//wait 10 milliseconds
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
return val;//send current moisture value
}

Move all pinMode from loop() to setup ().

You said you want to read analog signal but you never set any pin to INPUT.

I have moved the pin mode, cant i use the soilPin = A0 variable and if so how.

I'm new myself, but try to gain knowledge by looking at other projects. You need to set your A0 pin to be an input.

The code looks fine. By default all pins are input at startup so setting A0 to input is redundant but complete. It would be useful to know what the problem is other than 'can't get it to work' and what errors, if any are being reported.

And put your code in code tags!!!!

i am struggling with reading the analog value

I don't understand what this means. Your readSoil() function looks just fine.

What's your problem?

  • Is it not compiling?- Not uploading? - Nothing gets printed out to the serial monitor? - You are seeing garbage on the monitor instead of the text you expect? - Is it always returning a zero? - Random-looking values? - A fixed value that doesn't change even when you wet the soil? - A number in a range you don't expect? - A sensible-looking number, but you don't know how to interpret it? - The number comes back just fine, but you want to do something intricate with your four relays in response to the number and don't know how to proceed?

What? What's the problem?

You don't need to pinMode() analog inputs, the ADC library does it, what kind of prints are you getting, if any?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: