raspberry pi

Soil Moisture Sensor with Raspberry Pi, Circuit Diagram, and Python Code

Soil moisture sensor with Raspberry Pi, Overview:

Soil moisture sensor with Raspberry Pi– In this tutorial, you will learn how to use a Soil Moisture Sensor with the Raspberry Pi. I have been using the Soil Moisture Sensor with Arduino, ESP8266, and ESP32 for monitoring the soil moisture. The Soil moisture sensor is used for the Measurement of soil moisture. Of course, it is a practical area of ​​application monitoring of potted plants or beds. Since handling this Sensor is pretty much the same as that of the water level sensor, we extend it Lesson on a Small Project. At the end of this section we will connect one small pump to the system and automatically water the plant as soon as the Soil becomes too dry. You can find this model (see Figure 1) very easily on Amazon, the purchase links are given below. The prices are between 2 dollar and 10 dollar, depending on the Home country of the provider.

Amazon Purchase Links:

Soil Moisture Sensor Arduino:


12v DC water pump

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other components and tools:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Soil moisture Sensor with Raspberry Pi
Figure 1: soil moisture sensor with analog value output

You now have the choice between digital or analog signal outputs. This can be recognized by the output pins of the signal board. You should connect the four pins with the following Find labeling and corresponding function:

  • VCC: supply voltage 2.5 V to 5 V.
  • GND: ground
  • DO: digital output
  • AO: analog output

instead of using the above sensor, you can also use this new type of the Soil moisture sensor.

Connect VCC as usual with the 3.3 V of the Raspberry Pi (e.g. pin 1). GND becomes e.g. on pin 6 to connect it to the ground of the Raspberry Pi.

Now we come to the special feature of this module: The Pin DO gives a high signal off as soon as a set threshold value has been exceeded. You can do this with check the potentiometer built into the board. However, here is Trial & Error announced, because you cannot immediately know which setting to the water requirement Corresponds to your plant. You should have the right mindset though have found out through some field tests, use the output Signal very simply at any GPIO pin that is declared as an input.

The last pin, AO, outputs an analog signal. To process the signal we have to again make use of the MCP3008.

MCP3008 IC:

Soil moisture Sensor with Raspberry Pi

We’ll be using an MCP3008 IC, a 10-bit, 8-channel ADC (analog to digital converter) to resolve the analog pin problem.

The MCP3008 uses the SPI bus protocol to receive analog input values from the Raspberry Pi.

It features 8 analog inputs and uses four of the Raspberry Pi’s pins, excluding the power and ground pins.

It produces output values from a range of 0-1023 (Note: 0 represent OV and 1023 represents 3.3V).

Soil Moisture Sensor Interfacing with Raspberry Pi:

Soil moisture Sensor with Raspberry Pi

So that you adopt the following program, first set up the SPI interface and the MCP3008 is correctly connected with the Raspberry Pi. So now connect VCC to pin 1 and GND to pin 6 of the Raspberry Pi. AO connect to CH0 of the MCP3008. Now you can do the following Python Use code:

So that you adopt the following program, first set up the SPI interface and the MCP3008 is correctly connected with the Raspberry Pi. So now connect VCC to pin 1 and GND to pin 6 of the Raspberry Pi. AO connect to CH0 of the MCP3008. Now you can do the following Python Use code:

The determination of the final percentage value may seem a bit vague to you at first occur, but has a background: If you operate the sensor at 3.3 V, so it delivers a maximum digital value of approx. 460 when completely wet, so e.g. when immersed in a water glass. The dry sensor delivers 1023. Now two steps are necessary to obtain a meaningful moisture level:

  • First, we reverse the raw sensor value by subtracting it from 1023. This gives us the highest value with full moisture.
  • The following line specifies that the maximum value is 460 and calculates the value in percent.

The low price of the sensor module is certainly also reflected in the quality of the Product, which is why you set the maximum value through the water glass test yourself and adjust the variable in line 6 accordingly. Genteel In other words, this number juggling can also be called calibration, making the whole matter appear more professional.

The sensor is now set and can be inserted into the flower pot. Respect, think highly of Make sure that the connection contacts are not isolated and only the copper-plated ones Fork tines should be sunk into the ground. Accordingly cautious you should also be watering the plants.

The field test to determine the correct time to soak is now yours left. What is clear, however, is that a vegetable is more likely to be watered should as e.g. a cactus.

Automatic watering:

Now the optional, but definitely more exciting part of this section follows. What lies closer to monitoring soil moisture than automatic watering of the plant (see Figure 2)?

Soil moisture Sensor with Raspberry Pi
Figure 2: The automatically watered plant

For this we have expanded our circuit a little and a relay and a small one Pump added (see Figure3).

Soil moisture Sensor with Raspberry Pi
Figure 3: Relay switching with water pump

The pump we are using in this example can be found on Amazon, the purchase links are given above. Depending on availability, you will find small pumps there, which work with 3 V to 12 V operating voltage. Notice that in the pump as well a motor is installed that requires more power than the Raspberry Pi can do without damage can deliver. It is therefore necessary to connect an external pump voltage. In the above circuit you can see I have connected a 6V pump, which you can replace with a 12V pump, as this pump is controlled using a relay so it really doesn’t matter if you connect a low voltage pump or high voltage pump, follow the relay ratings. The relay itself is controlled using a driver based on the NPN transistor and a resistor. You can use any general purpose NPN and PNP transistor like for example 2n2222 or BC547, these are PNP type. You can read in detail about the types of relay and how to use them.

In order for the plant to be watered automatically, you need the Python program by the following lines for GPIO control of pin 3:

import RPi. GPIO as GPIO

GPIO. setmode (GPIO. BOARD)

relay = 3

GPIO. setup (rel, GPIO. OUT)

Also add the following code after print (“Soil moisture:” …).

if percentage value <= 30:

GPIO. output (relay, True)

time. sleep (3)

GPIO. output (relay, False)

This small extension switches on the relay on pin 3 for three seconds and sets thus the pump is in motion. As a limit, we have soil moisture of 30% set.

Now lay a hose from the suction port of the pump into a sufficiently large one large water tank. Place a hose from the pump outlet into the Plant pot.

Last but not least, you can use the finished Python script via cronjob e.g. Call every hour leave to check the soil moisture every hour. To do this, save the script at /pi/home/ soilmoisture.py, and then edit your code

This line is required to call the program every hour:

@hourly python / pi / home / soilmoisture.py

Now it’s up to you: Determine the right time and the right watering time Duration of the pump run time, build an elegant housing for the entire system or add more sensors, plants, and pumps.


The sensor presented here is cheap, but not recommended for continuous use. The copper layer has settled in the moist soil of the plant pot after week fees already resolved (see Figure 4). This initially ensures getting stronger fluctuating measured values ​​up to total failure. The plant is also loosened Metal in the medium did not fall in the long term.

Conclusion: for playing and experimenting the sensor presented here is definitely recommended for plants; for continuous operation it is better to use more expensive sensors.

Soil moisture Sensor with Raspberry Pi

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button