Accelerometer and Magnetometer Part 2: Raspberry Pi – Arduino Communication


The next step in my accelerometer+magnetometer sensor project is sending data collected by the Arduino to a Raspberry Pi. The Pi will ultimately control the entire system, which will also involve servos and possibly other actuators. My parts from Adafruit arrived last evening, and I had a basic system running this morning. I am using the Serial Peripheral Interface (SPI), which is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices. I read about how to connect the Raspberry Pi and Arduino at this web site. There are many varieties of connections and protocols that can be used. For instance, a simple voltage divider circuit can be used instead of the logic level converter that I used. While my data rates are low, in general it’s not as good to use resistive voltage dividers for serial communication since the RC time is increased. This leads to delays in the edge transitions as data are passed between devices. I was also nervous about any possibility of overvoltage on the Pi.

I chose to purchase two parts from Adafruit for this project. I purchased a logic level converter and the Pi Cobbler, which just provides a convenient way to attach the Raspberry Pi to a breadboard.

I am away on a trip right now, which is how I have enough time to make progress on this project. I could bring the Arduino and Raspberry Pi with me, but certainly not a monitor or keyboard to use with the Pi. I chose instead to use the Real VNC Viewer to access the Raspberry Pi desktop from my laptop computer. I plug the same laptop in to the Arduino through the serial port when I upload Arduino programs.

Mind the pins on the Raspberry Pi carefully. Here are two useful images that can help you.

I also use Putty and PSCP to have SSH access to the Raspberry Pi. There is a great tutorial on the Adafruit web site about how to enable SSH on the Pi as well as how to use VNC. I could download programs on my PC and upload them to the Pi. The Raspberry Pi has Python pre-configured. I uploaded using PSCP, and opened and ran it using the Python interface on the Raspberry Pi desktop, which I accessed using VNC from my laptop. The Raspberry Pi had to be located close to the router in this house since it was attached to the internet through an Ethernet cable. Rather than relocating all of my circuitry and my laptop up to the router this morning, it would have been nicer to stay at my workbench. If I had a WiFi dongle installed on the Pi, this would have been possible. I have one of those on order as well, but it will arrive at my home rather than at my present, remote location.

Here is a screen shot showing the VNC window accessing the Raspberry Pi desktop. The Python editor is open. I have the Arduino editor open at the same time.


Note that the web site I linked used the LED on Arduino pin 13. Since I added a protoboard, I chose instead to use pin 2, which I had separately wired to a larger LED. Also, the Arduino code on the web site DOES NOT turn OFF the LED. It also does not include line breaks for the serial monitor outputs. You can download the code on the two links below.

For the Raspberry Pi:

For the Arduino: RasPiComm.ino

Note that the serial lines must be DISCONNECTED from the Arduino in order for new code to be uploaded successfully.

Here is a video of the system working. Next I will start to pass real data.

By making small changes to code I have already written, I can demonstrate passing data to the Raspberry Pi. Here is a screen shot. I used all the same steps described above to upload programs to the Arduino and Raspberry Pi. The small window in the center of the screen shows the Z acceleration and magnetic heading passed from the Arduino to the Pi.


Here are links to the modified programs.

For the Raspberry Pi:

For the Arduino: MAG_ACCEL_Write_Raspberry_Pi.ino


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 )

Google+ photo

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

Connecting to %s