SPI or Serial Peripheral Interface, is a blazingly fast communication protocol that we can use in high-speed applications such as cameras, displays, and mass storage device adapters. In this video, we will be taking a look at the pros and cons of SPI, when you would use it, and also covering an example of wiring up and using an SPI micro SD card adapter with the Pico to perform some data logging.

Transcript

SPI, or Serial Peripheral Interface, is a little bit of a step up from UART in terms of complexity. This is because we now have four wires to deal with, and we can have multiple devices all talking to each other over those same four wires. One of the advantages of SPI is being able to have multiple things connected at once, but its biggest advantage is that it is the fastest of all the three protocols in this chapter by far. This means that it's great for things that need high data transfer rates, like this microSD card reader, which we'll be setting up in this video.

Now, this multiple devices all talking at once thing isn't a complete free-for-all; it's ordered and managed with something called Controller Target Architecture. You may have also heard the Master-Slave terminology used, but those terms are being phased out, and you might hear a few other replacements for these terms: Primary, Secondary, Controller, Worker, Initiator, Responder; it all describes the same thing. In our situation, the Pico will be the controller, and any modules that we plug into it, or any other devices, will be the targets. This means that the controller, our Pico, is just going to be the one that initiates and manages all the communication between the devices, just like how an airport control tower manages all the aircraft.

Alright, let's dive into an example of wiring up and using this SPI SD card reader and logging some data from our Pico onto an SD card. Now UART was great because we can write all the needed code with just standard MicroPython libraries, but chances are with SPI devices like this SD card reader, you're going to need to use a library. Each device will probably need a different library, so if you have the SD card reader, you'll be able to follow along with what we're doing here, but if you have a different device, you'll be needing to use a different library, and we'll have a completely different set of code. But you'll still be able to follow along with the important part of this video, which is how to wire up an SPI device, and how SPI works.

So like UART, there are two SPI peripherals that we can use, SPI1 and SPI0, and there are also multiple GPIO pins that we can use to access that hardware on the Pico. For this, you'll probably need to check a Pico pinout, because nearly every single GPIO pin can act as one of those four required pins for an SPI peripheral. For this example though, we will be using pins 12 to 15, which are connected to SPI1.

Let's make our first connection, the serial clock wire. SPI is a synchronous protocol, meaning that all devices that are connected together need to interact with each other in sync. When using SPI, the Pico is going to create something called a clock signal, which is just a digital rhythm that everything uses to synchronize to. So we need to connect the serial clock of our reader to the serial clock pin of our Pico, which is pin 14.

Now we need to connect the two data lines, and each of these are like a one-way street for data to flow. One is going to carry information from the Pico to the target devices, or just the target device in our case, we've only got one. And another will carry information from the target devices back to the Pico, or the controller. But we've run into a bit of an issue here of a lawless naming scheme for these pins. On my SD card reader, these pins are labelled as MOZI and MISO, and these are the most common names for these pins, but they're being phased out as they use the older terminology. And there hasn't really been a standardized replacement for them, so you might find devices with these pins labelled as DIN and DON, SDI, SDO, PICO, POKI, there is a lot of different names that you'll encounter. The key thing though is to check what naming scheme your device uses, and know that one will be used to send data to the controller, and one will be used to receive data from the controller. One consistent thing though is that our Pico calls these the TX and RX pins, just like you are. So pin 15 is the transmitter pin for us, and I'll connect that to the MOZI pin of our card reader. On target devices with this MISO and MOZI labeling, you can just look at the last letter to see if it's an input, I, or an output, O. So this wire goes from TX, which transmits data from the Pico, and it goes to the MOZI pin, which inputs that data on the reader. Then from the MISO pin, we want to output data and receive that on the Pico. So we'll plug that into pin 12, the RX pin.

The last pin is the chip select pin, and it is used by the controller to select which target device it is talking to. Because if we had another target device connected along here, and our Pico were to send out some information, because both of these devices are connected by the same wires, how would they know which one was meant to receive that message? This is where the chip select pin comes in. So we're going to connect the CS pin of our card reader to pin 13 of our Pico. Now when the Pico sends or receives data, it will use a digital output on that pin 13 to tell the SD card that it is meant to be receiving that information. You don't need to worry about coding that though. It's all going to be handled by the library. If we had another target device, we would need to connect its CS pin to a different GPIO pin, and any GPIO pin will work with the chip select. It's just using a digital output. And as always with any module, we need to give it power. This is a 5-Volt device, so I'm just going to plug it and power it off a VBUS like so. And that is how we make all of our SPI connections.

Now this is where it becomes different for every device. I'm just working off the documentation page here that's compatible with this SD card reader. And if you have another device, you'll need to go out and source a compatible library and find out how to use it. So I'm just going to go along here and look for the library to download. That looks like it's it. I'm going to save it, sweet, and plug in the Pico, and then I'm just going to go ahead and upload that to the Pico, sweet. And down here, it looks like we have some sample code that we can use. I'm just going to literally copy and paste that over directly and use that as a start. So it looks like this code analog reads and then just stores it with some time to the SD card. I'm just going to modify it real quick.I think we'll just import random and just kind of create some random numbers and just store that on the SD card instead. And you could store whatever you want on this. So I'm just going to generate a random integer between 0 and 100 with the random library. And I don't think we need to use that T. I don't think we need to write the T or that. We do need to write the string that creates a new line that looks like it pushes it to the SD card. And we don't need to print X there. That looks pretty neat.

So some important things you might need to do in your library is up here, we initialize the SPI object. We're using peripheral one and we set the clock pin, the mozzie pin and the meso pins. And we also designate the chip select pin here, which can be any other GPIO. And then it looks like we pass it off to our library here. And that sets it all up for us.

I've got my SD card here. It's formatted as FAT32. I'm just going to plug it in and then I'm going to run the code. And you can see we are starting to generate some random numbers. I'm just going to stop it. Control C is a nice little shortcut for that. And I'm just going to quickly unplug it. And I'm going to open up that SD card. And if I open that file, you can see that we have our randomly generated numbers here. And it's in the file name that was created here. All right, cool.

We know how to plug an SPI device into the Pico. What can we do with it? Well, SPI is in a bit of a niche. If we only wanted to connect two devices together, we might opt for UART for its simplicity and just compatibility with so many things. And if we wanted to connect lots of things together, like lots of sensors together, as we'll see in the next video, I2C is often a better and easier choice to do this. That's why SPI is great when it plays to its strengths, its high speeds.

SD card readers, we want to transfer data as fast as possible. So they will often use SPI. Higher resolution displays also commonly use SPI. OLED screens and E-ink displays need those faster speeds to update all of those pixels. You'll also find SPI modules used in things like this camera module. This one is plugged into the other SPI peripherals. And the Pico takes a photo and then transfers it onto the SD card. We can just do a real quick test here. And it does take a few seconds though, because capturing photos and processing images like this really starts to push microcontrollers like the Pico to its limits. But this is not to say you can't use SPI for things like sensors, because sometimes you may not even have a choice and may have to use an SPI sensor. But more often you'll find them in these high-speed applications.

Three key takeaways. One, SPI is a com protocol that can connect multiple devices together and uses the controller target architecture. Two, to use SPI, you'll need four wires, a clock wire, two data transfer wires, and a chip select wire. And three, SPI is much faster than UART and I2C and is often used in high-speed applications.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.