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); } |
- 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.
- (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.
Here are some examples,
1000 means 1 second and
1000x60 seconds = 60000 that's one minute, and that's how you can calculate.
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