Arduino/Raspberry Pi via I2C

The past 3 days I’ve been slowly building up an example to interconnect an Arduino to a Raspberry Pi via I2C.

At this point, I don’t have a specific project in mind, but it seems self-evident that while the RPI is a great little device, it is kind of limited in its ability to talk with hardware devices. Raspbian Linux is not a real-time O/S, so it could drop hardware events, and Arduinos have lots of available pins (when you get into the Mega especially) and there are tons of shields and widgets to connect to an Arduino. So it seems like I’m going to want to be able to talk between the two.

I2C seems to be the easiest way to do this. There are libraries on both devices to do so and I2C is compatible with a lot of shields/sensors, so I figure this will be good knowledge for the future.

I did a reasonable amount of research trying to figure out how to interconnect the two devices. The website I decided to use for an example is:

Oscar Liang does a great job explaining and illustrating the connection. Since all of this is totally new to me, I wanted an example I could do verbatim. Oscar has a great example that is simple and easy but clearly explains to me how this all works.

Oscar connects the Arduino directly to the RPI and his site reasons how he can safely interconnect the 5V arduino to the 3.3V RPI. I don’t doubt him, but other sites both caution against doing this and saying it is OK. I only have 1 extra RPI to play with and I don’t want to burn it out, so I used a bidirectional logic level converter to interconnect the two devices. I purchased this from Sparkfun:

Oscar discusses this breakout board after the fact toward the end of his example. He had trouble with it. Someone else noted that the problem was probably due to it being unidirectional. The breakout board I purchased was bidirectional and worked fine.

Here is my rough schematic:



i2c schmatic

Once assembled, the board looks like this:


I proceeded to follow his instructions for getting I2C installed on the raspberry pi and copied his programs exactly. I’ve never used python before so there was some stumbling trying to get that program running. IDLE seems a bit weird compared to other IDEs I’ve used (then again Arduino’s IDE is quite weird).

The program’s on both sides came up running fine. I could type a number into the RPI and it would show up on the arduino, then back on the RPI. Entering 1 would toggle the onboard LED off and on. Pretty darn cool.

The next step is to figure out how to do the same thing with Free Pascal on the RPI side. I’m hopeful there is an interface to I2C in Free Pascal though I won’t be surprised if there isn’t.




This entry was posted in Arduino/RPI I2C, c-rpi. Bookmark the permalink.

1 Response to Arduino/Raspberry Pi via I2C

  1. Pingback: Reading an Arduino Pin from a Raspberry Pi Pin | Big Dan the Blogging Man

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.