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         |
|             |           |            |
.————-.           .————.

Interactive Python

One of the great features of Python is the interactive interpreter. If you run the python3 command with no arguments it starts in an interactive mode in which you can enter Python code. The interpreter executes the code immediately and displays any calculated result.

Computer programmers call an interactive interpreter a “REPL”, which stands for Read/Eval/Print Loop. The interpreter reads some code from user input, evaluates the code, prints the result and loops round to repeat the process.

When programming in Python I find it useful to keep three windows open at all times: my text editor; a shell command prompt for running my program, tests or shell commands; and an interpreter prompt so that I can experiment with unfamiliar APIs, double-check my understanding of language features, run little manual or automated tests and use Python’s built-in help browser.

ipython3-in-action

The help browser is another great Python feature. The built-in help function can be used to read documentation about the modules, classes & functions of the Python APIs. We’ve written documentation for the Quick2Wire APIs that you can read with the help command. For example, the following command will show you the help for our GPIO API.

help('quick2wire.gpio')

IPython

The standard python3 interpreter has a pretty rudimentary REPL. There’s a much better one in the Raspbian software repository called ipython3, which provides a lot of help for entering and working with Python code. It auto-indents multi-line control flow statements, function and class definitions. It can auto-complete names for you: pressing Tab will complete a partially entered name from those that are in scope. It has a searchable command history that persists between sessions, so you can easily repeat or edit previous input. It can reload modules that you have imported, so if you change a code in your editor you can reload it into thee REPL. And much much more.

You can install ipython3 with the command:

sudo apt-get install ipython3

We’re still experimenting with our API design, trying to find an abstraction for GPIO that works for both on-board GPIO pins and pins of GPIO expansion devices like our MCP23017 board. We realise that this creates a bit of churn in the API and that changes have been landing in the GitHub repository without warning.

To improve the situation we’re changing our development process so that big API changes are done in public and accepted into the API project after a review period. We’re using GitHub’s pull requests to manage the process.

In future, when writing a new API feature or changing an existing API we will make a personal fork of the quick2wire-python-api repository. When the change is ready to be integrated into the API we will issue a pull request, and the change will appear in the issue tracker. Other team members will then review the code, suggest improvements or raise objections. And it’s not limited to the team members either. If you use the API and are interested in upcoming changes you can see what we’re changing and make comments too. Or, fork the library yourself and send us pull requests.

Here’s an example: https://github.com/quick2wire/quick2wire-python-api/pull/22

We hope this will help avoid unpleasant surprises and make the evolution of the software more transparent to folks using it.

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.

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.

It can be a bit of a pain doing Python development over ssh with the Raspberry Pi. You’re limited to text-mode editors unless you start an X environment. The alternative is to edit files on your PC and sync them to the Pi for testing, but that’s fiddly end error-prone.

If you’re running Linux on your PC you can mount files from your PC’s filesystem on the Pi or vice versa. You can then edit programs on your PC with your favourite editor and run programs on the Pi without having to sync at all.

If you mount a directory on the Pi into your PC’s filesystem, the files remain on the Pi when you unmount the directory from your PC, so you can edit programs on the Pi with your PC and leave them running on the Pi when you finish working.

If you mount a directory on your PC into the Pi’s filesystem, you can continue working on the program on your PC even when you’ve unmounted it from the Pi – handy if you want to work on the move – and then remount it to run your program on the Pi.

My favourite tool for doing this is sshfs. It uses ssh and sftp as the transport and so there’s no need to edit system-global configuration files, as you do to export NFS directories, and you don’t need to be root to export or mount a filesystem.

You’ll need to install sshfs on your PC and your Raspebrry Pi. On each machine, install it with apt-get:

% sudo apt-get install sshfs

You need to add yourself to the fuse group to mount FUSE filesystems like sshfs. On each machine, run

% sudo adduser $USER fuse

Now log out and back in again and you’ll be able to mount remote directories into the filesystem on the Pi:

% mkdir quick2wire
% sshfs -o idmap=user -o gid=`id --group` nat@192.168.1.4:quick2wire quick2wire

(Replace “192.168.1.4″ with the IP address or hostname of your PC and “nat” with your user id on your PC.)

Now you can edit the files on your PC, and run them on the Pi immediately.

Automating logon to the Pi

You’ll get asked to enter your password each time you want to connect. If you use an SSH key-pair, life is even simpler; ssh will use the keys to log you in without you having to enter your password.

If you need to generate a key pair on your PC. run

% ssh-keygen -t rsa

Accept the default location.

You will be prompted for a passphrase, and asked to confirm it. If you enter an empty string, you can use your key to log on to the Pi without further interaction.

So can anyone else who gains access to your PC account, so use this option with care!

OpenSSH will create a pair of files in your .ssh directory – a private key which you must keep secret, and a public key which you can transfer to other computers.

To transfer your public key to the the RPi, on your PC run:

# On Linux
% ssh-copy-id nat@<raspi-ip>

# On OSX
% cat ~/.ssh/id_rsa.pub | ssh nat@<raspi-ip> 
  "umask 077; mkdir -p .ssh ; cat >> .ssh/authorized_keys"
and after one final prompt you won’t be prompted for your Pi password when using ssh or sshfs.

When you’re finished

When you’ve finished, unmount the directory using fusermount -u:

% fusermount -u quick2wire

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.

 

We’ve got an experimental version of our SPI library working. Like our I2C library, it’s written in Python and uses Python ctypes to drive the low-level interface provided by the kernel.

You can see our usual exciting binary counter demo http://youtu.be/6RReTCq29wY

We’re driving an MCP23S17 16-bit SPI port extender, and sending the PORTA outputs to a Bar LED display.

There’s still lots of work to do but we’ll be releasing the current code on GitHub shortly.