Soil Moisture Sensor to Create a microbit Plant Watering Alarm
A soil moisture sensor module is designed to measure the amount of water in the soil by reading its resistance. It passes an electric current through the two prongs and measures the resistance of the soil.
A search on eBay or Amazon for soil moisture sensor arduino will yield many different types of sensors. The one used in this example should have three components on the prongs itself. There are three variations of the same type above.
The Soil Moisture Sensor has three header pins. These are labelled VCC, Output and GND. To connect wires to the sensor you will need female header pin connectors. Pictured right are examples of hookup wires with female header pin connectors.
Notes of Caution
Supplying constant power to the sensor drastically reduces its lifespan. The metal on the prongs slowly degrade dues to electrolysis. It’s much better to only power the sensor when you want to take a reading. In the example on this page, the sensor is turned on just before a reading is taken and then turned off.
We need to establish what the sensor’s reading is when the plant needs to be watered (the soil is dry) and when the plant needs to be watered (the soil is wet).
Flash a blank program (this can just read `from microbit import *)
Click ‘REPL’ button
Reading for Dry Soil
We need to know what sensor reading should trigger the need to water it. Attach VCC to 3V pin and SIGNAL to pin0. Put the sensor in dry soil and type in pin0.read_analog() in REPL. The return will look something similar to this:
80 is the reading for dry soil on our sensor.
Reading for Moist Soil
We also need to establish a ‘good’ reading for adequately watered soil. Repeat the above but with soil that has been watered. With my sensor, a well watered plant gave a reading of 211.
Using the Threshold
With the data we obtained we can now say:
A well watered plant will give a reading of around 211.
A dry plant will give a reading of around 80.
With these reading we can conclude a soil sensor reading of less than 180 requires the soil to be watered. Using these reading we can now write our code.
Show a sad face if the sensor reading is less than 180 (dry).
Analog sensor reading, especially with a moisture sensor, can be jumpy and erratic. You can create a function to smooth the reading from the sensor.
Output a sound to a speaker or a buzzer.
Pump water the plants.
Notes of Caution
These sensors are pretty terrible at reading the moisture content of the soil in a plant pot. The resistance of the soil is modified not just by the moisture content, but by temperature and the humidity outside. They’re also pretty quick to degrade. You have been warned!