If you haven't already done so, be sure your board is added to the large if statement in Adafruit_Blinka/src/busio.py. See the I2C page for more details.

Next you'll want to work on getting SPI working. To get SPI working, you will first need to make sure your SPI is enabled. The exact method to enable it can vary from manufacturer to manufacturer. For the Pine64, we used armbian-config and enabled spi-spidev. You can check if it's enabled by typing the following command:

ls /dev/spi*

If it is enabled, there should be at least one SPI port listed.

Adding to the Chip File

Near the bottom of the Chip File, you'll want an spiPorts tuple variable that contains all of the SPI ports. It should end up looking something like the following:

# ordered as spiId, sckId, mosiId, misoId
spiPorts = (
    (0, SPI0_SCLK, SPI0_MOSI, SPI0_MISO),
    (1, SPI1_SCLK, SPI1_MOSI, SPI1_MISO),
)

The values in each of the tuple items are as follows:

  • The SPI bus number. For instance, a value of 1 corresponds to /dev/spidev0.1. At this time, only Spidev 0 is supported.
  • The SCLK or Serial Clock pin. This should be an alias for readability.
  • The MOSI or Microcontroller Out Serial In pin. This should also be an alias for readability.
  • The MISO or Microcontroller In Serial Out pin. This should also be an alias for readability.

Adding to the Board File

The only thing you will need to add to the board file are any aliases for the Pins.

Parts Used

To test, you'll need an SPI controlled board such as the MAX31855, which is a Thermocouple Amplifier sensor (for temperature measurement) along with a couple other parts to connect it to the board.

Some boards operate at a lower voltage than 3.3v and may need to be wired through a Level Shifter as well.
Angled shot of a Thermocouple Amplifier breakout board.
Thermocouples are very sensitive, requiring a good amplifier with a cold-compensation reference. The MAX31855K does everything for you, and can be easily interfaced with any...
$14.95
In Stock
Angled shot of a Thermocouple Type-K Glass Braid Insulated wire.
Thermocouples are best used for measuring temperatures that can go above 100 °C. This is a bare wires bead-probe which can measure air or surface temperatures. Most inexpensive...
$9.95
In Stock
Angled shot of Half-Size Breadboard with Mounting Holes.
This cute 3.2″ × 2.1″ (82 × 53mm) solderless half-size breadboard has four bus lines and 30 rows of pins, our favorite size of solderless breadboard for...
$5.00
In Stock
Angled shot of Premium Female/Male 'Extension' Jumper Wires - 40 x 6 (150mm)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of...
$3.95
In Stock

Wiring

  • Connect the SBC 3.3V power pin to Vin
  • Connect the SBC GND pin to GND
  • Connect the SBC SCLK pin to the MAX31855 CLK
  • Connect the SBC MISO pin to to the MAX31855 DO
  • Connect the SBC D5 pin to to the MAX31855 CS

Run the Test Script

After wiring it up, double-check your connections. Next, make sure the MAX31855 library is installed. If you chose a different sensor, make sure that library is installed:

sudo pip3 install adafruit-circuitpython-max31855

Next save the simpletest code to your board as max31855_simpletest.py:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import digitalio
import adafruit_max31855

spi = board.SPI()
cs = digitalio.DigitalInOut(board.D5)

max31855 = adafruit_max31855.MAX31855(spi, cs)
while True:
    tempC = max31855.temperature
    tempF = tempC * 9 / 5 + 32
    print("Temperature: {} C {} F ".format(tempC, tempF))
    time.sleep(2.0)

Now go ahead and run that code using the following command:

sudo python3 max31855_simpletest.py

You should see it outputting the temperature every 2 seconds or so:

You will want to perform the same test procedure on each SPI port that you want to test. Be sure to change both your wiring and the corresponding pins in the test script if necessary.

Troubleshooting Tips

  • Make Sure SPI is enabled on your board
  • Try a different GPIO for the Chip Select Pin
  • Check your Wiring
  • The board may just not support SPI. It often doesn't work for us.

This guide was first published on Apr 01, 2020. It was last updated on Mar 13, 2020.

This page (Getting SPI Working) was last updated on Mar 02, 2023.

Text editor powered by tinymce.