Raspberry Pi (RPi) is a small, low-cost, single-board computer that offers more than just a Linux operating system. It also has communication buses and general-purpose input/output (GPIOs) that can be used to interface external sensors and devices.
One example is the serial peripheral interface or SPI. SPI communication is based on a shift register, where a master and multiple slaves acquire data by shifting one another’s registers.
An SPI protocol has four wires:
1. Master out slave in (MOSI): A master sends data to a slave
2. Master in slave out (MISO): A slave sends data to a master
3. Clock (CLK): A master and a slave must be operating at the same clock speed
4. Chip select (CS): Multiple devices can be connected to a single master. However, to access each device, the CS must first be enabled, which ensures all other devices on the bus remain disabled.
The RPi header SPI (including the MOSI, MISO, and CLK) is present on GPIO 9, 10, 11. The CS is present on the GPIO’s 8 and 9. Two chip selects (CE0 and CE1) mean that the default RPI hardware can control two slaves.
Additionally, it’s possible to increase the number of slaves simply by declaring any other GPIO pin as the output — and, then, controlling the CS slaves with it.
Raspberry Pi’s typical SPI master and slave arrangement are shown below. Three slaves are commonly attached with one master. Two of the slaves are controlled with RPi’s default chip select pins and a dedicated RPi GPIO pin is used to control the third slave.
By default, RPi’s SPI bus is disabled, so it’s necessary to enable it to communicate. The hard-coded manner to do so is to modify the root file. Another option is to enable it during the boot-up.
However, the easiest way is to enable it through RPi’s configuration window. Go to the menubar -> configuration -> RPI configuration and then check the “Enable” box next to the SPI.
Once RPi’s SPI bus is enabled, type this command into the console:
Lsmod | grep spi_
You should get the spi_bcm chip name, which means that the SPI is enabled. (If you fail to get this message, then the SPI is not enabled and you’ll need to try again.)
Once the SPI is enabled, it’s possible to interface any external SPI device with RPi. Raspberry Pi offers pre-defined packages that are a great resource when working with the SPI hardware. These packages provide libraries and interfaces that are easy and straightforward to use.
The RPi Python bundle
One of the most popular SPI packages for RPi is Spidev, a Python bundle that can easily access, program, and support the development of applications. Installing Spidev is also easy. In the console, simply type the following commands (in this order):
Sudo apt update
Sudo apt install python3-pip
Sudo pip3 install spidev
In the above installation procedure, python3 was used for all of the commands (because Python 3 was the program installed on the Raspberry Pi).
First, the RPi packages are updated and then the python3 pip manager is installed. Pip is Python’s package manager, so you can install any Python packages using it. The last step is to install Spidev by using the pip3 package manager.
To locate Spidev, switch to the /usr/lib/python3/dist-packages and list the packages. The compilation is another test, ensuring that the installation and compilation are successful. To do so, it’s necessary to import the package in the code as per below…
An example of the SPI code for Raspberry PI is shown below. First, the Spidev package is imported and its instance is created. With this instance, it’s possible to access the public variables and methods in the package.
The SPI parameters — such as the clock, transfer bit pattern, chip select, or the bits per transfer — can each be set individually. Data can also be transferred byte by byte or as a list.
To purchase RPi and practice using the SPI, go to:
- Raspberry Pi: Mouser
You may also like:
Filed Under: Microcontroller Projects