Learn to code

ARDUINO & CODES

Previous Post
Measuring Temperature with TMP36 Sensor
Next Post
Create multiple proximity buttons with MPR121 for Arduino

Detecting Moisture with Soil Moisture Sensor and Arduino

in Arduino Sensors

In this tutorial, you will learn how to read the values from a soil moisture sensor. So, soil moisture sensors basically measure the content of water present in the soil. If you are building a smart garden that waters plant this sensor is ideal for reading the wetness of the soil.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 5
Soil Moisture Sensor Soil Moisture Sensor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Soil Moisture Sensor

The soil moisture sensor consists of two probes that measure the volumetric content of water. The current is passed through the soil while the resistance value is returned that indicates the moisture value of the soil.

When there is more water in the soil, the soil will conduct more current which means less resistance. Therefore, the moisture level will be higher.
Dry soil will conduct current POORLY when there is less water present in the soil, which returns a lower moisture level.

TM1637 4-Digit 7-Segment Display Specifications

FeatureSpecification
Operating voltage 3.3V – 5.5V
Operating Current 80 mA
Operating Temperature-10ºC to +80ºC
Alpha-numeric digits4
Luminance levels8

BreadBoard Layout

Soil Moisture Sensor Connections

Soil Moisture Sensor FC-28Arduino Board
VCC5v output
GNDGND
A0A0 pin

The Code

// https://www.arduinoplatform.com
// Code for controlling a moisture sensor with Arduino

int moisture_pin = A0;

int output_value ;

void setup() {

  Serial.begin(9600); // beginning Serial communication

  Serial.println("Reading From the Sensor ...");

  delay(2000);

}

void loop() {

  output_value = analogRead(moisture_pin);

  output_value = map(output_value, 1023, 165, 0, 100);

  Serial.print("Mositure : ");

  Serial.print(output_value);

  Serial.println("%");

  delay(2000);

}

Code Explanation

To be able to measure the right soil moisture in percentage we need to make some calculations. The output of the sensor value ranges van be measured from 0 – 1023. However, this is in the case that you are using a 5v sensor. The output voltage of the sensor is between 0 – 4.2. So we need to do some calculations.

If the sensor is returning 0 output voltage it will correspond to 1023 while 0 corresponds to 5v.

However, since the sensor only gives an output between 0-4.2 the maximum reading is between 165 – 1023.

(1023/100)*4.2 = 859. there is a difference of 164 between 1023 and 859. But we know that if there is nothing measured in the soil sensor the value should be lower than if there is there water detected. Therefore, we range the values between 165 – 1023 and not 0 – 859.

In other words (theoretically), to measure the correct moisture percentage we need to use the number 1023 for 100% and 165 for 0%.

The analog value that is received by the sensor can be used t represent a moisture value in percentage by using the following formula.

Moisture in percentage = 100 – (ANALOG VALUE * 100) or we can use the map function

Moisture in percentage = (output sensor, 1023, 165, 0, 100). 

Another way to determine 100% moisture is by trial and error. In other words, measure the output when the soil is wet or dry and use these values in the map function.

Constant variables

In the first part of the sketch, we are going to declare the pin of the moisture sensor.

Furthermore, an integer variable is declared for storing the analog value that is received from the moisture sensor.

The next part is to begin Serial Communication and send some text to the Serial Monitor.

// https://www.arduinoplatform.com
// Code for controlling a moisture sensor with Arduino

int moisture_pin = A0;

int output_value ;

void setup() {

  Serial.begin(9600); // beginning Serial communication

  Serial.println("Reading From the Sensor ...");

  delay(2000);

}

Reading the sensor 

To be able to read the sensor we need to read the analog input.

After reading the analog input we are going to use the map function to convert the analog values, that range from 165 -1023, to 0 – 100 that we can use as percentages. Remember, that the value 165 means that very wet while 1023 means that the measured area is dry.

After that, we are simply just printing the converted value to the Serial Monitor.

void loop() {

  output_value = analogRead(moisture_pin);

  output_value = map(output_value, 1023, 165, 0, 100);

  Serial.print("Mositure : ");

  Serial.print(output_value);

  Serial.println("%");

  delay(2000);

}

In this tutorial, I have demonstrated how you can use a soil moisture sensor for measuring the moisture level in percentages. We used the map function() to present the results correctly.

I hope that you enjoyed reading the tutorial and fount is useful and informative. If you did please subscribe to my Instagram or Pinterest account found at the top of this page.

In any case that you have questions, feel free to ask them in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu