SPI is less popular than I2C but still you'll see lots of sensors and chips use it. Unlike I2C, you don't have everything share two wires. Instead, there's three shared wires (clock, data in, data out) and then a unique 'chip select' line for each chip.
The nice thing about SPI is you can have as many chips as you like, even the same kind, all share the three SPI wires, as long as each one has a unique chip select pin.
The formal/technical names for the 4 pins used are:
- SPI clock - called SCLK, SCK or CLK
- SPI data out - called MOSI for Microcomputer Out Serial In. This is the wire that takes data from the Linux computer to the sensor/chip. Sometimes marked SDI or DI on chips
- SPI data in - called MISO for Microcomputer In Serial Out. This is the wire that takes data to the Linux computer from the sensor/chip. Sometimes marked SDO or DO on chips
- SPI chip select - called CS or CE or SS
Remember, connect all SCK, MOSI and MISO pins together (unless there's some specific reason/instruction not to) and a unique CS pin for each device.
SPI on microcontrollers is fairly simple, you have an SPI peripheral and you can transfer data on it with some low level command. Its 'your job' as a programmer to control the CS lines with a GPIO. That's how CircuitPython is structured as well. busio
does just the SPI transmit/receive part and busdevice
handles the chip select pin as well.
Linux, on the other hand, doesn't let you send data to SPI without a CS line, and the CS lines are fixed in hardware as well. For example on the Coral, there's two CS pins available for the hardware SPI pins - ESPI1_SS0 and ESPI1_SS1 - and you have to use them. (In theory there's an ioctl option called no_cs
but this does not actually work)
The upshot here is - to let you use more than 1 peripheral on SPI, we decided to let you use any CS pins you like, CircuitPython will toggle it the way you expect. But when we transfer SPI data we always tell the kernel to use ESPI1_SS0. ESPI1_SS0 will toggle like a CS pin, but if we leave it disconnected, its no big deal
The upshot here is basically never connect anything to ESPI1_SS0. Use whatever chip select pin you define in CircuitPython and just leave the ESPI1_SS0 pin alone, it will toggle as if it is the chip select line, completely on its own, so you shouldn't try to use it as a digital input/output/whatever.
Parts Used
OK now that we've gone thru the warning, lets wire up an SPI MAX31855 thermocouple sensor, this particular device doesn't have a MOSI pin so we'll not connect it.
We recommend using a breadboard and some female-male wires.
You can use a Cobbler to make this a little easier, the pins are then labeled!
Wiring
- Connect the Coral 3.3V power pin to Vin
- Connect the Coral GND pin to GND
- Connect the Coral SCLK pin to the MAX31855 CLK
- Connect the Coral MISO pin to to the MAX31855 DO
- Connect the Coral GPIO_P29 pin to to the MAX31855 CS
Double-check you have the right wires connected to the right location, it can be tough to keep track of Pi pins as there are forty of them!
Install the CircuitPython MAX31855 Library
OK onto the good stuff, you can now install the Adafruit MAX31855 CircuitPython library.
As of this writing, not all libraries are up on PyPI so you may want to search before trying to install. Look for circuitpython and then the driver you want.
(If you don't see it you can open up a github issue on circuitpython to remind us!)
Once you know the name, install it with
pip3 install adafruit-circuitpython-max31855
You'll notice we also installed a few other dependancies called spidev, adafruit-pureio, adafruit-circuitpython-busdevice and more. This is a great thing about pip, if you have other required libraries they'll get installed too!
We also recommend an adafruit-blinka update in case we've fixed bugs:
pip3 install --upgrade adafruit_blinka
Run that code!
The finish line is right up ahead. You can now run one of the (many in some cases) example scripts we've written for you.
Check out the examples for your library by visiting the repository for the library and looking in the example folder. In this case, it would be https://github.com/adafruit/Adafruit_CircuitPython_MAX31855/tree/master/examples
As of this writing there's only one example. But that's cool, here it is:
# 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)
Save this code to your Coral by copying and pasting it into a text file, downloading it directly from the Coral, etc.
Change the line that says
cs = digitalio.DigitalInOut(board.D5)
to
cs = digitalio.DigitalInOut(board.GPIO_P29)
Then in your command line run
python3 max31855_simpletest.py
The code will loop with the sensor data until you quit with a Control-C
That's it! Now if you want to read the documentation on the library, what each function does in depth, visit our readthedocs documentation at
https://circuitpython.readthedocs.io/projects/max31855/en/latest/
Text editor powered by tinymce.