This article will cover how to interface a FC-28 soil moisture sensor with a Raspberry Pi.
The sensor measures the volumetric content of water in soil and presents the moisture value in voltage.
It also provides both analog and digital outputs, but for this project we'll be solely utilizing the analog output.
To enable the sensor analog mode, we'll need an analog pin to produce output.
Which poses an issue, since the Raspberry Pi doesn't include an analog pin.
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).
Enabling the SPI Interface with the Raspberry Pi
Follow these steps to enable the SPI interface with the Raspberry Pi:
- Start by launching the terminal and typing in the below command:
- Navigate to the interfacing options.
- Enable the SPI interface.
- Reboot your Raspberry Pi.
Installing the spidev Library
The spidev library will help with reading the sensor values over the SPI interface.
Use the following commands to install the spidev library:
sudo apt-get install git python-dev
git clone git://github.com/doceme/py-spidev
sudo python setup.py install
Installing the numpy Library
The numpy library is also required in order to convert the output values to a percentage.
The output values we receive from the MCP3008 IC will be in the 0-0123 range mentioned earlier. We'll then map these values to 0-100 for a percentage.
Install the numpy module with the following command:
sudo apt-get install python-numpy
The components required for this project:
- Raspberry Pi
- FC-28 Soil Moisture Sensor
- Jumper Cable
Circuit Diagram and Explanation
The MCP3008 has a total of 16 pins, out of which 8 are used for recording analog input values. The remaining 8, located from CH0-CH7 (pins 1-8), are used to communicate with the Raspberry Pi through the SPI protocol method.
When reading the analog output value from the soil moisture sensor—keep in mind that is measures the moisture in percentages and will map the values from 0-100.
The values will be mapped using a specific interp module from the numpy library.
Use the Python code below to read the sensor moisture values.