Turning led on and off with soil moisture sensor

Once you know how to turn a led with soil moisture sensor then you can do anything you like, for example turning on a motor and turning it off or turn on a tap to water the garden or turning light on and off and so on.

In this post I'll share with you a simple sketch but as I've said before by changing a bit you can control lot of other things, so let's see the sketch first.

The sketch to turn a led on and off
int led1 = 4;

void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(led1, OUTPUT);
Serial.begin(9600);
}


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);

if (sensorValue >=600 && sensorValue <=800){
Serial.println(sensorValue);
    digitalWrite(led1, HIGH);
  }
  else {
Serial.println(sensorValue);
    digitalWrite(led1, LOW);
  }
delay(1000);
}
Pins:
  • GND of the sensor goes to GND of the Arduino.
  • VCC of the sensor goes to 3V. Even though in the manual it says it can handle 5volts and I've tried few times but it didn't work so I think 3 volts is fine.
  • A0 of the sensor goes to analogue pin 0 of the Arduino
  • I have made pin number 4 for LED but it's up to you which ever pin you want to use. 

What can you change?
  •  int sensorValue = analogRead(A0); // In this you can change the analogue pin 0 to other pins, but make sure you change in the "sensorValue" pin as well.
  •  if (sensorValue >=600 && sensorValue <=800){ // First let me tell you what this line actually does. Basically by putting 600 and 800 in the above line you telling the sensor that if the value number (which obviously tells you how moist the soil is) is above 600 and under 800 than turn on the led1 and that's done by this line digitalWrite(led1, HIGH);.
  •   if (sensorValue >=600){ // If you don't like the between value like shown at the example avobe than this code will do the trick. Basically this code means if value digits are more than 600 then it will turn the led on.
And if the moisture level is below 600 and above 800 it will turn the led1 off which is done by this line (digitalWrite(led1, LOW);). You can change the numbers (600 and 800) according to your need.
  •  (delay(1000);) This means the sketch takes reading from the sensor and prints it on serial monitor after every second, you can make it slower for example make it get the readings after each 15 minutes or faster like five times in one second as you wish.
To make it slow or fast you just need to change the number.
Here are some examples,
1000 means 1 second and
1000x60 seconds = 60000 that's one minute, and  that's how you can calculate.
That's all for this post, I hope you can make interesting things with this sketch and if you didn't understand something then please feel free to ask me in the comment below. Thanks :)

1 comment:

  1. Thanks for this post! I am trying to connect the grove moisture sensor with the oled display. Do you have a code for how to do that?

    ReplyDelete



Newly posted:
Reason why rich getting richer and poor getting poorer