Saturday, June 22, 2013

Connecting Raspberry Pi and Arduino. Hardware

Though you can control peripheral hardware using Raspberry Pi's GPIO (general ports for input/output), I prefer to do that using an array of Arduino. The advantages are:
  • No limitation in ports. You can always use Arduino Mega, or even use an array of them.
  • Physical separation of controller and Arduino.
  • It's not that scary to burn another Arduino for $10 rather than Raspbery Pi for $35.
And the most effective way to connect several Arduino and Raspberry Pi is I2C (or I2C to be precise). You need only 3 wires (4 in practice) to connect even several Arduino.

So the general connection schema using I2C looks like this:


Unfortunately, you cannot insert wires from Arduino to Raspberry Pi. The problem is the I2C bus does not strictly specify the signal levels. As a result we have 5V for Arduino's I2C implementation, and 3.3V for Raspberry Pi's one. And if you connect the I2C pins directly, you're in great danger of burning your Raspberry Pi (or at least part of it). So we have to use an I2C bridge, or level shifter. You can buy a ready one or create you own, as I did. And the schema for the bridge is pretty simple:

It requires 4 N-channel MOSFETs (I used BS170) and 4 pull-up resistors (10K each). Another important moment is Arduino and Raspberry Pi need to have the common ground. So simply join the ground pins (and that would the the fourth required wire I was talking about earlier).

After soldering I got this:


And the common connection scheme using the I2C bridge now looks like:


Scheme of pins for Raspberry Pi can be found here. And for Arduino SDA is A4, SCL is A5.

That's it with the hardware part. In this post I describe the software part of connecting Arduino and Raspberry Pi.

No comments:

Post a Comment