SunFounder 3in1 Kit
• Or copy this code into Arduino IDE.
After the code is uploaded successfully, if your threshold is set correctly, you will see the red LED light up when the
soil is dry to remind you that you need to water; after watering, the green LED will light up.
How it works
...
void loop() {
int sensorValue = analogRead(soilMoisture);
Serial.println(sensorValue);
if (sensorValue > threshold) {
digitalWrite(redPin, HIGH); // Turn the red LED
digitalWrite(greenPin, LOW); // green
} else {
digitalWrite(greenPin, HIGH); // Turn on the green LED
digitalWrite(redPin, LOW); // red
}
}
...
First set a threshold value and then read the value of the soil moisture module, its value decreases as the moisture
level increases. If the value currently read is greater than the set threshold, then let the red LED light up, otherwise
it will turn on the green LED.
This threshold value needs to be adjusted according to the actual situation, you can upload the code first, then open
the serial monitor to check the value, record the value in both wet and dry conditions, and then choose a middle value
as the threshold value.
4.5.3 5.3 State Change Detection
When the button controls other devices, it can not only work when it is pressed, but stop when it is released. It is also
possible to switch the working state each time the button is pressed.
In order to achieve this effect, you need to know how to toggle the working state between off and on when the button
is pressed, That is “state change detection”.
In this project, we will use the button to control the motor.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name ITEMS IN THIS KIT LINK
3 in 1 Starter Kit 380+
You can also buy them separately from the links below.
138 Chapter 4. Basic Projects