Check the 34 other Arduino tutorials

At Bas on Tech, we believe that everyone should have access to valuable technology resources.

Your donation helps us cover the costs of hosting, videos and maintenance, ensuring that our platform remains free and open for all to use.

Thank you for your support!

iDEAL and Bancontact are listen on the "Direct Debit" tab in the 4th step.

#31 · Capacitive soil moisture sensor


Tutorial goals

  • ✓ Reading capacitive soil moisture sensor
  • ✓ Difference capacitive and resistance sensor
  • ✓ Working with map() function

Components needed

  • 1× Arduino
  • 3× Jumper wire (male-male)
  • 1× Sensor cable
  • 1× Capacitive soil moisture sensor

Summary

In this tutorial, Bas will show you how to use a capacitive soil moisture sensor. You'll need an Arduino board, the sensor, a sensor cable, jumper wires and a glass of water. He explains that the sensor works by measuring the resistance between two pins which changes based on the water level in the soil. However, he warns that fertilizers and corrosion can affect the sensor's accuracy. Instead of measuring resistance, this sensor works like a capacitor, measuring charge and discharge timing. The sensor also has a chip called a 555 timer chip which helps with timing. Bas will guide you on how to connect the sensor to the Arduino board and run the code. Finally, he'll show you how to calculate the percentage of moisture in the soil by adjusting the code and measuring the total range of the sensor.

Introduction

In this tutorial we are going to use an Arduino to read a capacitive moisture sensor . With this sensor you can, for example, determine whether a plant needs water 🤓

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

Traditional moist sensor

Traditional moist sensor
Traditional moist sensor

Above you see the most common used moisture sensor. It consists of two pins that you insert into the earth. These two pins act as a resistor. This allows you to measure how much current it conducts in the earth. Fertilizer also changes the resistance of the earth, which makes your measurement less accurate.

Corroded moist sensor
Corroded moist sensor

The biggest disadvantage of this sensor is that it can corrode over time. In the photo you can see that the metal on the right pin has almost completely rusted away.

This is because the direct current from the sensor will react with the water. So NOT use this sensor!

Capacitive moist sensor

Capacitive moist sensor
Capacitive moist sensor

For our Arduino we are going to use another moisture sensor: the capacitive moisture sensor . On this sensor you see 1.2, there is also a version with 2.0. You can use both, it doesn't matter for this lesson.

This sensor works in a different way than the traditional sensor mentioned earlier. The capacitive moisture sensor works like a capacitor. The water in the earth changes the capacitor's capacity. By measuring the charge and discharge time we can determine how moisty the earth is.

Schematic overview

schematic overview moist sensor
schematic overview moist sensor

At the top left you see that we send a pulse through a resistor . This then comes to our sensor, which behaves like a capacitor (the two horizontal bars).

schematic overview moist sensor
schematic overview moist sensor

When the capacitor is charged, it begins to discharge. The duration of this process is measured by the sensor. This is shown by the clock at the top right. Measuring the time is done by the 555 chip. This can be seen in the middle of the sensor.

The circuit

Connect the sensor according to the diagram below.

The circuit of the connected moisture sensor
The circuit of the connected moisture sensor

Arduino Code - part 1

The code below does nothing but read pin A0, and then send the measured value to the serial monitor.

1 void setup()
2 { 
3   Serial.begin(9600);
4 }
5 
6 void loop()
7 {
8   Serial.println(analogRead(A0));
9   delay(100);
10 }

Upload this code to your Arduino. Write down the value displayed on the serial monitor. We will use this value in part 2 to determine the humidity. Do the same if the sensor is in the water.

Arduino Code - part 2

We start by defining two constants:

1 const int dry = 595; // value for dry sensor
2 const int wet = 239; // value for wet sensor

Enter your own measured values here.

setup()

1 void setup()
2 { 
3   Serial.begin(9600);
4 }

In the setup() we do nothing but set the serial monitor to 9600 Baud.

loop()

1 void loop()
2 {
3   int sensorVal = analogRead(A0);
4 
5   int percentageHumididy = map(sensorVal, wet, dry, 100, 0); 
6 
7   Serial.print(percentageHumididy);
8   Serial.println("%");
9   
10   delay(100);
11 }

The loop() function consists of 3 parts:

  • Reading the moisture sensor
  • Convert the measured value to a percentage
  • Print the values on the serial monitor

map()

1 int percentageHumididy = map(sensorVal, wet, dry, 100, 0);

We convert the measured values to a percentage with map().

For the sake of convenience, we assume that the sensor has a linear course. This means that each step is the same size. In practice this will not be the case. For a really accurate measurement you need to measure the sensor values of as many intermediate steps as possible, for example every 10%. After this you can determine whether the measured values lie between two limits, to determine them the percentage.

We measured in part 1 that the sensor gives values between 239 and 595. We want to convert this to a scale from 0 to 100 to get percentages. The map() function has 5 arguments:

  • value: sensorVal
  • of value low: 239
  • of value high: 595
  • to value low: 0
  • to value high: 100

This is exactly what we pass to the map() function with map(sensorVal, wet, dry, 100, 0)

Serial.print() of Serial.println()

Further down in the code you will see two lines:

1 Serial.print(percentageHumididy);
2 Serial.println("%");

The first time use print() and the second time println(). The difference is that println() starts a new line after it prints, print() does not.

1 Serial.println(70);
2 Serial.println("%");

prints:

1 70
2 %

en

1 Serial.print(70);
2 Serial.println("%");

prints:

1 70%

In this way we can print the percentage with the % sign attached. There are other functions to do this easily, I'll get to that in another lesson.

Finally, we wait another 100ms and read the sensor again.

Upload code to Arduino

You can now upload the code to the Arduino. Then open the serial monitor to view the sensor readings.

Tools â–¸ Serial Monitor

Arduino IDE serial monitor
Arduino IDE serial monitor

You can now hold the sensor in the water to see the difference in percentage.

💡 At the bottom right you see the Baud rate. Note that this is the same as specified in Serial.begin(), in our case 9600.

#31 · Capacitive soil moisture sensor schakelschema