Add Capacitive Soil Moisture Sensors to Raspberry Pi

Raspberry Pi is a famous series of small single-board computers (SBCs) developed in the United Kingdom by the Raspberry Pi Foundation in cooperation with Broadcom. This is a step by step tutorial for using Raspberry Pi and capacitive soil moisture sensor with Microchip MCP3002 analog-to-digital converter (ADC) and a Python script for detecting the soil moisture in percentage.

Capacitive Soil Moisture Sensor

Capacitive Soil Moisture Sensor v1.2 and v2.0 measures the volumetric content of water inside the soil and retrieves the moisture level by capacitive sensing rather than resistive sensing like other sensors. The benefit of using a capacitive soil moisture sensor is the lack of corrosion and longer lifespan.

Wiring

Unlike Raspberry Pi Pico, the recently released microcontroller, all versions and models of the Raspberry Pi single-board computers do not include an analog-to-digital converter (ADC). This tutorial explains how to use Microchip MCP3002 with Raspberry Pi.

Microchip MCP3002 is a 10-bit resolution, dual channel ADC with SPI hardware bus. It can be connected to any Raspberry Pi single board computer version and model, including Raspberry Pi 4 and Raspberry Pi 0. However, this tutorial is not for Raspberry Pi Pico microcontroller. For more details about the wiring of Microchip MCP3002 a Raspberry Pi single-board computer have a look at my previous tutorial.

Prototypes for Raspberry Pi add-on boards

Alternatively, the easier option without a breadboard an a bunch of cables, is to use a dedicated Raspberry Pi add-on board with built-in ADC. Using the free and open source tool KiCad we designed ANAVI Gardening uHAT exactly for this purpose. It has dedicated pins for connecting a couple of capacitive soil moisture sensors. The prototype has been created thanks to PCBWay. This is a lead-free prototype printed circuit board with 2 layers, green solder mask and white silkscreen. PCBway offers a huge variety of colors and even flexible PCB.

Software

Flash Raspberry Pi OS, the official Debian based GNU Linux distribution by the Raspberry Pi, on microSD card and boot it. On the Raspberry Pi, open a terminal and using the raspi-config tool enable SPI as shown in the video. Reboot the Raspberry Pi.

Python3 script for reading data from a couple of capacitive soil moisture sensors through Microchip MCP3002 ADC is available at the anavi-examples repository in GitHub. The script relies on popular Python libraries spidev and RPi.GPIO. Open a terminal and run the following commands to clone anavi-examples and run the script:

git clone https://github.com/AnaviTechnology/anavi-examples.git
cd anavi-examples/anavi-gardening-uhat/soil-moistore-sensors/python/
python3 soil-moistore-sensors.py

You may also like

Raspberry Pi & Microchip MCP3002 Analog-to-digital Converter (ADC)

If you are reading this blog post, I am sure you are familiar with Raspberry Pi, the a series of small single-board computers (SBCs) developed in the United Kingdom by the Raspberry Pi Foundation in cooperation with Broadcom. Unlike the recently released microcontroller Raspberry Pi Pico, all versions and models of the Raspberry Pi Linux computers do not include an analog-to-digital converter (ADC). If you need to read data from an analog device such as a potentiometer, sound or soil moisture sensor the solution is to use an external ADC, for example Microchip MCP3002.

Prototyping on a breadboard with Raspberry Pi 4, 10K potentiometer and Microchip MCP3002 ADC

Microchip MCP3002 ADC

Microchip MCP3002 is a 10-bit resolution dual channel ADC with SPI hardware interface for connecting to embedded devices such as Raspberry Pi. MCP3002 operates over a broad voltage range, from 2.7V to 5.5V. It is offered in 8-pin MSOP, PDIP, TSSOP and 150 mil SOIC packages. MCP3002 PDIP package is appropriate for prototyping on a breadboard.

Raspberry Pi and Microchip MCP3002 Wiring

Microchip MCP3002 has to be connected to the dedicated SPI pins (MISO, MOSI, SCL and SS) on the Raspberry Pi GPIO header. In the video a 10K potentiometer is connected to one of the two channels of the ADC for testing purposes. The potentiometer as well as Microchip MCP3002 are powered with 5V from the Raspberry Pi.

Microchip MCP3002 attached to Raspberry Pi 4 over SPI

Enable SPI

Boot Raspberry Pi OS, the official Debian based GNU Linux distribution by the Raspberry Pi, from a microSD card. Open a terminal and using the raspi-config tool enable SPI as shown in the video. After that reboot the Raspberry Pi and proceed to the next step.

Reading Data with Python

Python3 script for reading data from analog devices through MCP3002 is available at the rpi-examples repository in GitHub. The script relies on popular Python package RPi.GPIO. Open a terminal and run the following commands to clone rpi-example and run the script:

git clone https://github.com/leon-anavi/rpi-examples.git
cd rpi-examples/MCP3002/python
python3 adc.py

The potentiometer acts like a variable resistor. Rotate it and observe the output of the Python script. You will notice a change of the voltage between 0V and 5V depending on the position of the potentiometer.

ANAVI Garderning uHAT

Thanks to PCBway, the sponsor of this video, we can go to the next level and use a prototype of ANAVI Gardening uHAT as a Raspberry Pi add-on board with SOIC package of Microchip MCP3002. ANAVI Gardening uHAT follows the specifications of Raspberry Pi Foundation for HAT (hardware attached on top), including for an EEPROM with device-tree binary overlay configurations.

Prototypes of the new Raspberry Pi add-on board with Microchip MCP3002 ADC from PCBWay


You may also like