Tutorial Update: Now with More Python!

We've updated our popular "Raspberry Pi SPI and I2C Tutorial" with some Python examples.

Favorited Favorite 3

One of our more popular tutorials shows how to use the SPI and I2C buses available on the Raspberry Pi GPIO header. It seems that people are interested in connecting hardware to the Raspberry Pi, which is definitely a good use for the tiny computer.

The original tutorial showed how to use the WiringPi library with C++ to talk to a SPI 7-segment display and an I2C digital-to-analog converter (DAC). While I do thoroughly enjoy C/C++, I figured giving Python examples alongside the C/C++ ones would give readers more options.

Raspberry Pi SPI example

As might be expected, the C/C++ examples are faster and more efficient. However, since Python seems to be a popular way for writing quick scripts or prototyping, knowing how to use SPI and I2C protocols might help with your next Raspberry Pi project. The updated tutorial can be found here:

Raspberry Pi SPI and I2C Tutorial

October 29, 2015

Learn how to use serial I2C and SPI buses on your Raspberry Pi using the wiringPi I/O library for C/C++ and spidev/smbus for Python.

I used the smbus Python package in the tutorial, as that seems to be the de facto package for communicating over an I2C bus. However, it is lacking some of the lower level I2C functions, such as clock stretching, which limits its usefulness with some sensors that require it (like the Si7021). If you can recommend any I2C Python packages that are easy to use and can handle clock stretching, please let us know in the comments!

Comments 2 comments

  • Member #134773 / about 6 years ago / 1

    Hi Shawn!

    I apologize that I don't have time at the moment to read the updated tutorial in detail, but it looks interesting. One thought, though, that came to mind, is a "spin-off" from the "Bark back" project for those of us who don't have a canine sharing our homes: have some motion detectors (maybe including the door bell switch?) to play recorded barks from actual dogs... might scare off someone who has "malitious intent"... ;-)

    Anyway, thanks for the efforts! I'm hoping it won't be too long before I have time to tinker a bit more with I2C and/or SPI from a Pi.

    • No worries, and I definitely appreciate the time you take to look at the tutorials! There really wasn't much added to the tutorial, as it's been around for a while. All I did was add a couple of Python examples to give people options when it comes to using SPI and I2C on the Pi.

Related Posts

Recent Posts


All Tags