It’s important to keep your plants watered and healthy. However, if you struggle to remember if you’ve watered them or want to get a reminder, you can build a device to help you keep track of the moisture levels in your plant’s soil using a soil sensor. This guide will show you how.
A soil sensor features two long probes that you insert into the soil of your plant. The probes act as a resistor. Dry soil doesn’t conduct electricity very well, so resistance will be higher. As water is added, it increases the conductivity, leading to a higher voltage returning to the Arduino. By measuring this voltage, your projects can act based on how dry or wet the soil is.
This value can then be passed to your projects and used as a variable to respond to your dry plants in whatever way works best for you. For example, you could set the project to spray your plants with water. However, to keep things simple, we’ll be building a project that will switch on an LED when it detects that water levels are too low. If you see the light on, it’s time to water the plant.
One important thing to keep in mind with sensors like this is that the metal probes can get oxidized over time and lose their effectiveness. If you use them regularly, then you may need to replace the sensor every year or so. However, they’re very cheap, so this shouldn’t be too big of a deal.
For the version of the project we’re going to make, you won’t need much material beyond what comes in standard electronics kits. However, keep in mind that if you want to deploy this in your own garden, you may need to do some extra work to make sure that your project is protected from the elements.
Arduino Uno | ![]() |
Moisture Sensor | ![]() |
LED | ![]() |
Breadboard & wires | ![]() |
Arduino IDE | ![]() |
USB Cable | ![]() |
If you plan on putting this project outdoors or where the Arduino is likely to get splashed with water, you may also want to buy a case for it.
The code for this project is fairly simple. Copy the below script into your IDE and upload it to your board.
int sensorPin = A0;
int moistureLevel ;
int LED = 6;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
moistureLevel = map(sensorValue,0,550,0,100);
Serial.println(sensorValue);
delay(30);
if (moistureLevel < 50) {
analogWrite(LED, HIGH);
} else {
analogWrite(LED, LOW);
}
}
Now, let’s go over each piece of the sketch before moving onto the wiring.
int sensorPin = A0;
int moistureLevel ;
int LED = 6;
int sensorValue = 0;
First, we’ll define a few variables. The sensorPin variable will read input from the moisture sensor. The moistureLevel variable will store the final moisture level after mapping it to a more usable level, and LED will store the value for pin 6. Finally, we’ll add a variable to store the value coming off the moisture sensor.
void setup() {
Serial.begin(9600);
}
The setup section is fairly simple. Essentially, we’re just starting the serial monitor so we can see the values that the sensor outputs.
sensorValue = analogRead(sensorPin);
moistureLevel = map(sensorValue,0,550,0,100);
Serial.println(sensorValue);
delay(30);
The first line here will read the output of the moisture sensor. This value here will range from 0 to 1023, so the next line remaps it from 0 to 100, just to make it a bit more manageable. That value will then be output to the serial monitor so you can monitor it live. Finally, a brief delay is added. You can adjust this based on your needs. This won’t affect much, but in any case, moisture sensors don’t to be updated super frequently anyway.
if (moistureLevel < 50) {
analogWrite(LED, HIGH);
} else {
analogWrite(LED, LOW);
}
In the last section, we’ll take the final remapped output value from the moisture sensor and use that to turn the LED on if the plant is too dry. Anything under 50 and the LED will turn on, indicating the soil is dry. Above 50 and the LED will turn off. You can adjust this number based on what you see in your serial monitor to make sure it’s accurately reflecting the moisture values in your plant.
The wiring on this project isn’t too complicated, but you can expand on it with your own actions based on the moisture sensor’s output. For our purposes, though, we’re keeping it simple with just an LED. To connect everything:
● Connect the GND pin of the moisture sensor to GND on the Arduino.
● Connect the VCC pin of the moisture sensor to 5V.
● Connect the signal pin of the moisture sensor to pin A0
● Connect the short leg of the LED to pin 6.
● Add a 220 ohm resistor after the long leg of the LED.
● Connect the other end of the resistor to GND.
When you’re done, place the moisture sensor into your potted soil and monitor the values it returns using the serial monitor. You can make adjustments to the code based on your own needs. You can also try expanding this project by adding new parts. For example, you could set up the project to automatically spray your plants if they get to dry.