The Raspberry Pi can be used for just about anything. It’s safe to say that the Pi and similar single board computers are becoming ubiquitous.

When you start experimenting with GPIO pins on your Raspberry Pi, things get even more interesting. We’ve already covered what all of these pins do, so now let’s take a look at how to enable the SPI and I2C protocols on your Pi and why you might want to.

What is SPI on Raspberry Pi?

Before diving in, let’s take a quick look at what these two protocols do.

SPI (or Serial Peripheral Interface) allows a microcontroller like the Pi to communicate with more than 100 peripheral components at the same time. The microcontroller acts as a «master» for all «slave» components and can communicate with them at high speed. This diagram describes a simple SPI connection:

SPI and I2C on Raspberry Pi
Image credit: Cburnett/

If you are reading about SPI for the first time, this may seem intimidating, but keep up the good work! SCLK is the clock frequency set by the master, which determines the rate at which information is distributed between devices.

In each cycle (or «tick») of the clock, both master and slave send and receive one bit of information. This requires contacts. MOSI (Master Out Slave In) and MISO (Master In Slave Out).

Conclusion SS or Slave Select (labeled CE0 or CE1 on the Pi) is used to tell the slave whether to communicate with the master or not — at any given time. In most cases, each slave requires its own SS pin, but may share SCLK, MOSI, and MISO pins.

Some devices can be «daisy-chained» to share the SS pin, with a total of four pins used, plus two for power and ground. SPI is known for being incredibly fast and is commonly used in shift registers or ADCs (analogue-to-digital converters) to transfer data between devices.

There is a simple tutorial for SPI on Advanced Startup YouTube channel explaining the protocol further:

How to enable SPI on Raspberry Pi

To enable SPI on Raspberry Pi, open raspi-config from a terminal by typing:

sudo raspi-config 

Go to Interface Options and in the next menu choose P4 SPI Enable/disable automatic loading of the SPI kernel module . Select Yes in the request window. SPI is now enabled!

SPI and I2C on Raspberry Pi

Also, you can enable SPI from file /boot/config.txt . Open a terminal window and type:

 sudo nano /boot/config.txt 

Find the line that says #dtparam=spi=on and remove the character # . Whichever way you enable SPI, reset the Pi and SPI.

What can you do with SPI on Raspberry Pi?

While there are hundreds of different projects that use many different SPI peripherals, programming your own RFID reader is a good starting project.

We used the MFRC-522 reader in our tutorial » For an introduction to the Raspberry Pi RFID Flavorer, check out the piddlerintheroot manual.

This kind of setup is not just for hobby electronics either. In our collection of the best Raspberry Pi projects we have unveiled a full scale MES system that works using Raspberry Pi and RFID as part of a large scale factory work in Europe.

What is I2C on Raspberry Pi?

I2C is a communication protocol created by Phillips Electronics that has become widespread in microcontroller-based hobby projects. The reason for this is obvious — it only uses two wires instead of the four minimum SPIs. It is also well supported by custom libraries. There are many components designed to be used with I2C on the Raspberry Pi. Although slower than SPI, it is still fast enough for most everyday tasks.

Like SPI, the protocol has leading device like Pi, and subordinate a device such as a screen, shift register, or motor driver.

The first connection between devices is SCL (Serial Clock), which is set by the master to synchronize data transfer. The second line is SDA (serial data) that sends data back and forth between all devices on the I2C bus.

The master starts communicating with primary bit and seven bit hexadecimal address . This must match the slave so they can communicate. That’s how many devices can be used with only two wires.

The master device then indicates whether it wants to read or write ( R/W ) slave device before receiving confirmation or ACK back.

YouTuber: Advanced startups YouTuber: A simple and detailed overview of I2C:

How to enable I2C on Raspberry Pi

I2C is also enabled on the Pi via the menu raspi-config . Open terminal and type:

 sudo raspi-config 

Use the arrow keys to select » Interface Options» and then P5 I2C Enable/disable automatic loading of the I2C kernel module . Confirm that you want to enable the protocol and you should see a confirmation screen.

SPI and I2C on Raspberry Pi

As with SPI, you can also enable I2C by editing the config.txt file. Open terminal and type:

 sudo nano /boot/config.txt 

Change # dtparam=i2c_arm=on removing the symbol # .

SPI and I2C on Raspberry Pi

Reboot your Pi and I2C is ready to go!

What can you do with I2C on Raspberry Pi?

I2C is great for any project that requires a lot of outputs. Typical use is for LCD screens that require up to 16 pins. I2C reduces this to just two for communication and two for power and ground.

Many LCD screens come with an attached backpack I2C which greatly simplifies their setup and frees up the rest of the Pi’s pins for other sensors and peripherals.

TheRaspberryPiGuy has a detailed YouTube guide on setting up and using an I2C LCD screen on a Raspberry Pi

Using SPI and I2C saves contacts!

SPI and I2C on the Raspberry Pi are certainly not for beginner electronics enthusiasts, but they also have nothing to fear.

Before diving in, it’s a good idea to check out the Raspberry Pi Beginner’s Guide to learn the basics.

Working on sample projects is the best way to learn and we look forward to seeing what you come up with! Who Knows What Weird And Wonderful Raspberry Pi Creations Are you will do?

Похожие записи