Example Code for Raspberry Pi – Analog Capacitive Soil Moisture Sensor

Last revision 2026/01/20

The article offers a comprehensive guide on using an Analog Capacitive Soil Moisture Sensor with Raspberry Pi, detailing hardware components, connection setup, and programming instructions to help users measure and monitor soil moisture levels effectively for agricultural and forestry use.

Introduction

This is a simple moisture sensor that can be used to detect soil moisture. When the soil is short of water, its output value will decrease, otherwise it will increase.
This kind of sensor is mainly used to measure the relative water content of soil, do soil moisture monitoring, agricultural irrigation and forestry protection.

Hardware Preparation

Wiring Diagram

  • Connect the Raspberry Pi correctly to devices such as the screen, power, keyboard and mouse.

  • Connect the sensor to analog port A0 on Raspberry Pi expansion board. The wiring diagram is as follows.

  • Find components and connect

Schematic and operating principle

The soil moisture sensor judges the soil moisture content by its water level.

As shown in the figure, when the soil moisture sensor probe is suspended in the air, the base of the triode is open, and the output of the triode is 0. When it is inserted into the soil, the resistance value of the soil is different due to the different moisture content. Then the base of the triode will provide a variable conduction current. The conduction current from the collector to the emitter of the triode is controlled by the base, converted into a voltage after the pull-down resistor of the emitter.

Sample Code

  • Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’

sudo apt-get install build-essential python-dev python-smbus git

  • Download the drive library and program.

git clone https://github.com/DFRobotdl/111.git

  • Find SEN0114.py in the file you downloaded before, open and run it with, you can see the printed data.

Was this article helpful?

TOP