The Yocto Project on Raspberry Pi 5 Episode 10: Use SPI for ADC and Soil Moisure Sensor on Linux
This article is part of the video tutorials for Yocto and OpenEmbedded on the Raspberry Pi 5 based on the Scarthgap Long Term Support release. The tutorials provide step by step instructions for building custom Linux images, enabling hardware interfaces, and using peripheral devices. In this session, the focus is on enabling the Serial Peripheral Interface and using it with a capacitive soil moisture sensor through an Analog to Digital Converter on the Raspberry Pi 5.
Understanding SPI
SPI or Serial Peripheral Interface is a synchronous serial communication protocol used for short distance communication between a master device such as a microcontroller or microprocessor and one or more peripheral devices. It uses separate lines for data in, data out, clock, and chip select. SPI is widely used in embedded systems for connecting sensors, displays, memory devices, and converters due to its high speed and simple hardware implementation. In Linux based systems, SPI devices are typically accessed through the spidev interface in user space using programming languages such as Python or C.
Step by Step Tutorial
This tutorial explains how to enable SPI on a Raspberry Pi using a custom Linux image built with the Yocto Project and OpenEmbedded. The examples are based on the Scarthgap LTS release and the meta raspberrypi Board Support Package layer. The hardware used in this demo is the ANAVI Gardening uHAT, an open-source Raspberry Pi add-on board made for home automation, gardening and agriculture applications. It has been designed with KiCad and features connectors for multiple sensors and supports 1-Wire, ADC through SPI and I2C interfaces, making it ideal for monitoring temperature, soil moisture, and other environmental conditions. It is also available at Mouser Electronics.
Step 1: Enable SPI in Build Configuration
Add the following line to the conf/local.conf file to enable the SPI bus during image build.
ENABLE_SPI_BUS = "1"
Step 2: Include the spidev Python Package
Include the spidev Python 3 package used in the example applications along with other useful tools by appending the following line to conf/local.conf.
IMAGE_INSTALL:append = " python3-spidev"
Step 3: Build the Image
Build the core image base target with your configured Yocto environment. After the build completes, flash the generated image to a microSD card and insert it into the Raspberry Pi 5.
bitbake core-image-base
Step 4: Read Sensor Data Using Python
After booting the system, open the Python 3 interactive shell and execute the example code from the following GitHub Gist. The code demonstrates how to read values from a capacitive soil moisture sensor through an ADC (Analog to Digital Converter) Microchip MCP3002 connected via the SPI interface.
Following these steps enables SPI communication and demonstrates how to interface with sensors such as capacitive soil moisture sensors through an Analog to Digital Converter using a Yocto based Linux image on the Raspberry Pi 5. This approach helps embedded developers understand peripheral management and provides a foundation for building advanced sensor applications.