The Raspberry Pi and the Arduino are both amazing, and each has its particular strengths. There are lots of projects that use one or the other, but there’s growing interest in solutions that combine the strengths of the two platforms.

We use both on C3Pi (our experimental robot). At present we use I2C to link the two, and today we found a way to simplify the way they are connected. Since there seems to be a lot of interest in this topic, I thought we’d share our approach and explain why the solution is a little simpler that we thought!

c3pi-direct-connection-427x285

The I2C software is easy

We’ve been using our Python library on C3Pi to drive the I2C bus that we currently use to connect the Pi to the Arduino. We’ve also adapted the Arduino I2C slave code which is one of the Arduino  examples.

Both programs are very simple. You can find the current versions on github. (Warning: expect the code to change rapidly as we are experimenting with new ideas!)

Linking the Pi and Arduino hardware is easier than we thought

Until today, we were using a sparkfun level shifter to connect the 3v3 I2C bus used by the Pi to the 5v I2C bus on the Arduino.

It turns out that we don’t need to, because the pull-up resistors on the Pi keep the SDA and SCL lines close enough to 3v3 to be safe, even if they are directly connected.

Until today I thought that wasn’t the case. I dimly remembered that the Arduino has pull-up resistors that are enabled by default when you use the Wire library. However, it turns out that these are weak (high-resistance) pull-ups; the Pi has strong pull-ups which dominate those on the Arduino, making direct connection safe.

This only applies to I2C. If you want to connect the serial port on the Pi to the serial port on the Arduino, you will need some protective circuitry between them. That’s why we have a level shifter on our interface board.

But for I2C, just connect the Pi and Arduino as shown below and everything will be fine!

.————-.           .————.
|  Pi         |           | Arduino    |
|          SDA|———–|SDA         |
|          SCL|———–|SCL         |
|          Gnd|———–|Gnd         |
|             |           |            |
.————-.           .————.

Do you pine for the plug-in peripherals on other platforms like the Grove System from seeed studio or the Gadgeteer?

Are you finding it hard to choose between the great interface boards from companies like Ciseco and Quick2Wire?

Grove real time clock

Grove RTC

Sytech 3 Axis Accelerometer

Sytech 3 Axis Accelerometer

Help is at hand!

We’re about to start prototyping the babelboard – an inexpensive board that lets you connect I2C-based boards from all these families to the Raspberry Pi.

This will allow you to use dozens of I2C-based devices with the Pi without having to design or solder them. Just plug them in and write some code!

Over the next few days we will open up the project on GitHub and start posting details here. The board is fairly simple so we hope to have it available in kit form within weeks.

Find out more

If you want to know more, or have ideas, join our support forum, head over to the babelboard topic and get in touch.

A few people are currently using the beta 0.5+ plus version of our analogue board.

The board is almost unchanged since version 0.2, so you can assemble it using the old guide.

The position of the jumper pins has been tidied up a bit but that shouldn’t be a problem.

We’ll do an updated assembly guide when the production boards arrive later this month.

We’ve been asked to document the pin allocations that we use for the I2C connectors on our boards.

Here they are:

i2c-pinoutThe Int line is used to signal an interrupt from those boards that can generate one, including the MCP23017 Port Expander Board.

The 3v3 line is powered by the Interface Boards’ voltage regulator, and can provide a much higher current than the Pi’s 3.3 volt supply.

One of the themes that came out of yesterday’s excellent Raspberry Jam at Milton Keynes was the very positive overlap between the Arduino and Raspberry Pi communities.

A lot of us Pi enthusiasts also have Arduinos and use them for projects. We’re beginning to investigate what the two can do together, and we’re seeing a number of projects (like Gordon Henderson’s wiringPi) which let people use their Arduino skills and code to drive hardware attached to the Pi.

Some of us are also currently evaluating a Pi shield from Cooking Hacks which allows you to plug in Arduino shields and control them from the Pi.

Of course, not everyone has both computers. If you’re trying to decide which one to use Andrew back has written a great comparison on DesignSpark.

If you do have both, there are several ways in which the Pi and Arduino can be connected.

You can use the serial ports. You need to remember to insert a level shifter to convert between the Pi’s 3.3 volts and the Arduino’s 5 volts, and you need to write programs to send and parse text at each end of the wire.

For most applications I prefer using I2C or SPI. You still need a level shifter, but the software is pretty straightforward. The standard Arduino examples include a sketch that makes the Arduino act as an I2C slave device, which you can then control from the Pi.

Since you decide on the address the Arduino uses on the I2C bus, you can (in theory) control over a hundred Arduinos from a single Pi! I2C is fairly slow, and if you need to send a lot of information to or from the Arduino then SPI might be a better solution.

One combination that I want to explore when I have time (ha!) links the Pi and the Teensy 3. If you haven’t come across one before, the Teensy is an Arduino-compatible computer with a very small form factor, and the Teensy 3 has a powerful ARM processor while maintaining a high degree of Arduino library compatibility.

It has 16K of RAM – not much by Pi standards, but eight times the memory of a standard Arduino – and it has a much more powerful processor. Together the Pi and the Teensy 3.0 should form an awesome combination.

I’ve just come home after a great session in Norwich. One of the most enjoyable things about this sort of meeting is that you get a wonderful range of perspectives on the Pi. Here’s one.

Nev Young explained that he’d already recovered the price of his Pi in reduced electricity costs. His web server (please don’t all visit at once!) now runs on a Raspberry Pi, consuming about two watts instead of three hundred. That’s the way to save the planet one server at a time. Brilliant!

There were lots of other interesting people there. Too many to mention them all, but they included a solicitor who runs archlinux on his laptop, and is teaching himself Python on his Pi, a retro-game-meister, a developer who is working on what could be a uniquely extensible social game, and of course @stevedp – a fellow Python developer and Pi enthusiast who manages the http://norwichrpi.org/ website.

I did our blinken-bar led demo, but with a new twist. We’ve just published the first version of a library for the PCF8591, which is the chip at the heart of our I2C analogue board. We’ve hooked one up to a photocell and written a little Python program to display the light level on a Bar LED. I have to admit to a childish pleasure in seeing the LED respond as I wave my hand above the board.

Here’s our post about the demo with video and code, and a report from @norwichrpi (with photos) here.

I was made to feel very welcome, and will be heading back when we have the next lot of boards to show.

Most of our users started out with revision 1 of the Raspberry Pi, but a growing number have revision 2 boards, and some have both.

A few things changed in revision 2, including GPIO pin numbering and the I2C interface.

Until yesterday our GPIO library did not handle header pin 13 correctly on revision 2 RasPis, and the I2C library required you to specify that you wanted to use I2C bus  1.

We’ve updated the library and those problems should now be a thing of the past.

If you are using the quick2wire-python-api libraries, you will need to pull the new versions from github. If you got to the directory in which you cloned the library, just run git pull from the command line and you should get the new version of the code.

Last night we went along to the Raspberry Jam session hosted at Mozilla’s super offices in Central London to meet, mingle and demonstrate our beta boards.

There were a couple of excellent presentations, one on RiscOS for the PI, one on the awesome #TechnoTunnel. The latter defies description – you had to be there – but I’m hoping someone took a video. When it’s published we will share the link.

There was a real buzz throughout the evening, both metaphorically and literally, as our neighbours were demonstrating two lively and noisy Roadsters. They had on-board RasPis controlled over bluetooth. One was almost certainly breaking the speed limit – at least until it crashed!

As we hoped, Sukkin Pang of SK Pang Electronics was there with his custom workbench for our hardware. The version he brought has a mobile battery powering the Pi which runs for hours without a mains supply. Neat and simple to use, like the Pi. Sukkin gave us some great feedback about the beta boards.

We had a steady stream of visitors asking about the interface board, port extender and analogue board. Our demo showed three sets of bar LEDs being controlled by a Python program over the I2C bus. The cool bit is that each is controlled by a separate program running in its own process. The Linux kernel sorts out all the concurrency issues and the simple Python code just works.

We met several people working on fascinating projects, ranging from home control to an allotment management project combining remote greenhouse control with intruder detection. The Pi makes it easy to create projects which would have been impossible a couple of years ago.

The Jams are a great way to stimulate your imagination, find out what others are doing and try out ideas. if you’re in the London area and remotely interested in the Raspberry Pi the #raspijam is a must-go event.

While we’re waiting for the first interface boards to arrive we’ve been checking out our libraries against the new Raspbian distro.

The latest Raspbian kernel supports I2C and SPI, and today we got all our loopback tests passing. This means we have working Python libraries on Raspbian for GPIO, I2C and SPI.

Raspbian is now the official recommended kernel, so we’re fast approaching the point where a new Pi user can plug in our hardware, install our libraries, run an example and expect everything to work.

At the moment the Python GPIO library needs our gpio-admin library as well as the Python code. Soon we’ll package the two together as a .deb file, which will make things even easier. The gpio-admin and Quick2Wire Python libraries are on github so if you feel like experimenting, clone away and let us know how you get on! The libraries have examples and test code included.

If you want to try out the SPI code, you’ll need to upgrade to the latest Raspbian firmware. The kernel in the current Raspbian image (2012-07-15-wheezy-raspbian) supports I2C but not SPI; you can use Hexxeh’s firmware updater to get a later kernel, which supports both.

There are a couple of other things you’ll need to do: uncomment the two lines in /etc/modprobe.d/raspi-blacklist.conf which stop needed modules from loading, and add a line i2c-dev to /etc/modules to ensure that the relevant i2c module is loaded on boot-up.

Your raspi-blacklist.conf should then look like this:

# don't blacklist spi and i2c by default
# blacklist spi-bcm2708
# blacklist i2c-bcm2708

and  /etc/modules should look like this:

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev

PS Things are moving fast. If you want to keep up-to-date with what’s happening here at Quick2Wire, sign up for our free newsletter. Just fill in your email at the top of the page and click sign up. We won’t spam you, and you can unsubscribe at any time.