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.

If you live within reach of Milton Keynes, you won’t want to miss nest weeks’ Raspberry Jam.

Peter Onion and friends run these each month at the The National Museum of Computing, Bletchley Park. Up to 40 Raspberry Pi enthusiasts turn up for an informal but lively session. There are lots of experienced Pi users as well as a healthy sprinkling of beginners, and you can always wander round the museum once the meeting is over.

The meetings usually fill up quickly so book your tickets now!

Here’s the pinout for our SPI connectors.

The CS line is connected to CS 0 for the connector on the left and CS 1 for the connector on the right of the Interface Board, as seen from above. Int can be connected to an interrupt for those chips that support it, such as the MCP23S17 SPI Port Extender.

spi-pinout

 

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.

When you put together one of our interface boards you’ll see that it breaks out the GPIO pins into their own header and labels them 0 to 7. Our GPIO library numbered Pins 0..23, after the header pins on the Raspberry Pi. That’s pretty awkward in practice so we’re experimenting with ways to make it easier to write programs that work with broken out GPIO headers.

Our current approach is to replace the Pin type with two pin classes: GPIOPIn and HeaderPin. A GPIOPin is created with a pin number from 0 to 7 that identifies the pin on the interface board. A HeaderPin is created with a pin number from 0 to 23 that identifies the pin on the Pi’s header.

For example:

#!/usr/bin/python3

import sys
from time import sleep
from quick2wire.gpio import GPIOPin, In, Out

led = GPIOPin(1, direction=Out)
button = GPIOPin(0, direction=In)

while True:
      led.value = button.value
      sleep(0.1)

To ensure that existing code keeps working, we define Pin as a synonym of HeaderPin. But when we’ve found an approach that works well we’ll deprecate Pin and eventually remove it from the API.

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.