Reading Soil Moisture Sensor in Serial
Last updated
Last updated
The soil moisture sensor is a device used to measure the moisture content in the soil. It helps monitor the water level in the soil, which is crucial for efficient plant growth.
The sensor consists of two conductive probes that are inserted into the soil. As the soil's moisture level changes, the conductivity between the probes also changes. This change in conductivity is used to determine the moisture content in the soil.
Look for the A0 connections on the Sensor Shield as marked on the following picture.
Connect the VCC pin of the soil moisture sensor to the V pin on the Shield.
Connect the GND pin of the soil moisture sensor to the G pin on the Shield.
Connect the A0 pin of the soil moisture sensor to the S input pin on the Shield.
Open a new file on the Arduino IDE, copy this code and paste it in the new file.
The code starts by declaring a variable soilMoisturePin
to store the analog pin A0, which is connected to the soil moisture sensor.
In the setup()
function, the serial communication is initialized with a baud rate of 9600 using Serial.begin()
.
Within the loop()
function, the soil moisture value is read using analogRead()
from the soilMoisturePin
and stored in the soilMoisture
variable.
The soil moisture value is then printed to the serial monitor using Serial.print()
to display a label "Soil Moisture: " and Serial.println()
to print the actual value.
A delay of 1 second is added using delay()
to wait before the next reading, preventing rapid data updates and making it easier to read the values.
By calibrating the sensor, we can establish the minimum and maximum values of moisture levels in the soil, allowing us to set appropriate thresholds for watering our plants.
It reads the sensor value from the soil moisture sensor and maps it to the desired range using the map()
function.
The sensor value, along with the corresponding mapped value in percentage, is then printed on the serial monitor.
The wet
and dry
variables represent the calibration values obtained during wet and dry calibration, respectively.
These values define the range of the sensor readings. The dryMap
and wetMap
variables represent the desired mapped range, which is from 0% to 100%.
The code runs in the loop()
function, continuously reading the sensor value, mapping it, and printing the results on the serial monitor.
The delay of 1 second ensures that the values are displayed at a reasonable interval.
Sensor: | Sensor Shield: |
---|---|
VCC
V
GND
G
A0
S