At this point, we have connected the humidity sensor to the Arduino. Keep a pot of soil handy to test our humidity measuring system:
/* Gardening system
* Lets water our plants on time
*/
// humidity sensor is at analog port 0
const in SENSOR_PORT = A0;
int sensorValue = 0;
void setup() {
// We will initialize the serial port so we can print humidity data to the serial port.
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// Read the input on analog pin 0:
sensorValue = analogRead(SENSOR_PORT);
// A well watered soil will give a reading of atleast 380 to 400, a fully moist soil will have reading of 1023.
sensorValue = constrain(sensorValue, 400, 1023);
// We will reverse the range so that we get a rough humidity percentage reading i.e.
// fully moist soil will give a reading of 100% for 100% humidity and
// dry soil will read 0% for 0% humidity
sensorValue = map(sensorValue,400,1023,100,0);
// Print humidity value
Serial.println(sensorValue);
if (sensorValue >= 20) {
Serial.println("soil is wet");
} else
{
Serial.println("soil is dry");
}
delay(1000);
}
Upload the sketch to Arduino and start reading the serial port using the Arduino IDE by pressing .
Start placing the soil sensor in soil samples with different moisture levels. You should see the different humidity levels on the serial port monitor between 0 to 100.
Next we will set up the rest of our gardening system.