Arduino working with Raspberry Pi

The microcontroller and the microcomputer.

  1. The ALL Sensor
  2. Threading or faking threading
  3. Arduino to Pi
  4. Notes on Housing
  5. Notes on ‘on the fly’ Arduino reprogramming


A flickr album of the photos of this project:

I’ve become pretty adept at using raspberry pi. However, there are some flaws that I just can’t get around. The power source and the networking. Firstly, the pi even the A+ is a power hog. Secondly, there are some communication problems. The Arduino has some light at the end of the tunnel for these issues. It’s not an straight forward solution to the problems, but it’s cheap and good enough.

By now all the bigwigs appear to be calling it the ‘internet of things’. To me the word ‘things’ means more than stuff plugged into an outlet. So far, every single raspberry pi I have requires an outlet. This is fundamentally limiting. The batteries that you can buy online will only last at best 48 hours at best and are not very reliable. I’ve seen others that have used a car batteries to power the pi for longer extended periods, but that is just not portable at all. The raspberry itself sucks more power than a typical smart phone. This is very frustrating when you want to use the device for something away from home, or away from a car. There are solar panels and this is helpful, but reducing power consumption would go much further.

The second issue has to do with networking or rather getting the devices to communicate with each other or a computer over distance. There are the usual suspects: wifi, bluetooth, GSM (cell phones), and even infrared. Again, all of these use power themselves compounding the original problem. Also, an M2M line will run you at least 6$ a month minimum in order to use a cell phone network for whatever you are doing. The rest have range limitations, or in the case of infrared proximity issues. As far as distance and power the most promising of all the options is XBee. However, it only solves the problem of distance when loaded onto a raspberry pi.

Enter the Arduino. It is not a full fledged computer. It is a microcontroller. By comparison the Broadcom chip in the does 700MHz, but the Arduino does 8MHz or 16MHz. This is like the difference between a small moped and a jet, where your typical desktop/laptop computer is either a train or a spaceship Analogy[Arduino=mope, Pi=jet, Laptop=spaceship, Desktop=train]. The microcontroller (read Arduino) can run a single lone script. The raspberry pi can run multiple programs. The typical vanilla UNO Arduino has almost the same battery life of the raspberry pi due to the functional design. However, it does draw less current, and some Arduino models can last for weeks if used with the right battery.

There we have it the so saviour! Not really because it is the interplay possible between the two devices that can solve the issues. The Arduino has significant drawbacks in the area of communication/networking. First, the GSM module isn’t being sold right now because it catches on fire. The wifi module does not appear to allow for secured communication (SSH/VPN). There are a myriad of problems, that having a spare computer around for a few minutes could really help. The devices appear to be perfectly matched to solve each others shortcomings.

For Christmas, I got an Arduino starter kit and one of those 37 sensor kits. The Arduino starter came with some beginner experiments and I blew through those on a Sunday afternoon. My attention turned to the fabulous collection of cheap sensors that had no documentation. Just a bunch of boards with descriptions and not much else. Searching online brought me to this link. A poorly translated guide with some examples, and not everything worked. Nobody has the spare time to go through these all so I used the same script to test out all of them, and see what their functionality was. In the end, I cherry picked my favourites and decided that I want to make ‘something’. You know, for the internet of things and all.

LINK: Amazon Arduino Starter Kit
LINK: Amazon 37-in-1 sensor kit

1.) The ALL Sensor

Turns out that Arduino is seriously easy to use. A usb cord, some breadboarding, and a little C++ skill are all that is necessary to get something working. Although actually making something is a little more difficult.

The sensors I chose:
Button: Because a button, why not.
Sound Sensors: These guys are supposed to be able to sense loud and quiet sounds, but they aren’t microphones.
Light: It’s called a photo-resistor and it can tell if a light is on or not
Temp DHT11: This particular guy can sense temperature AND humidity
Tilt: It responds when shaken.


(click the picture for an album of pics for the project)

Wiring a breadboard is not that difficult. The one provided with the starter kit was probably meant for only a single test. Breadboards aren’t that complicated, and if you’ve never seen or used one this is a worthwhile and satisfying activity.

Plug the sensors into the breadboard. This breadboard is rather small, so everything is going to be tightly packed together. Wire up the grounds and positives to the breadboard. Red is for the 5V and black is for the Ground. The rows on the outside make it so that you can easily wire all the grounds and voltages together. Connect the signal wires. Try not to use any black/red wires for signal wires, so that you can identify them later. Also, don’t use the RX/TX connections as we’ll be using them later (warning). It would save some time to attach in the same pattern as the script I wrote below.

Downloading, installing, and running the Arduino IDE shouldn’t be very difficult. However, writing a script is somewhat difficult. I wrote a basic script that uses the IDE serial connection to show the output.


2.) Threading or faking threading

There is an issue that results from having so little computing power. On a microcontroller running two operations can be done to appear simultaneous. However, this chip can only run one operation at a time. If a loud sound hits the sensors it may not be fast enough to sense it. The tilt switch is in a similar situation. At the same time you don’t want the temperature/humidity every second, nor the light sensor. Thus something needs to be done to check the temperature/humidity/light at one rate and the sound/tilt/button at another rate.


(only 16MHz)

There are several solutions, but this one is effective for the time being. Download it and put it into your sketchbook folder under the library folder like the installation instructions say (also remove master from the folder-name).


Again, there are other ways of doing it but the documentation explains exactly what it does. If you’re planning on doing something like this I suggest reading all of the documentation on this and a subject called protothreads several times to familiarize yourself. However, if you’re just plug n’ chugging a short description will do. ArduinoThreads prioritizes various tasks and decides when to pause one and run another.

A little explanation on the script. The digital Temp/Humidity sensor made this script an absolute headache to program. This is because the interrupt that makes the script appear to have threads keeps wanting to interrupt the extremely specific timing of the reading of the digital temp/humidity sensor output.


3.) Arduino to Pi

Before we start lets install a bunch of stuff, this could take some time…

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get clean && sudo apt-get autoremove && sudo apt-get install -y arduino-core arduino-mk avrdude python-dev python-rpi.gpio python-serial python-smbus i2c-tools minicom

It turns out that connecting an Arduino to a raspberry pi is fairly easy. However, there are lots of pitfalls. One of the things you might want is that the raspberry pi have a command line interface for programming the Arduino (not the default Arduino IDE with a GUI). Apparently, this is where all hell breaks loose. The default IDE is incredibly slow on the raspberry pi and it leaves you out in the cold. The IDE no longer works on the command line (only with a monitor/VNC), and it appears that they don’t want to fix it. So there are all sorts of different instructions all over the internet. There are about a dozen different tools to choose from and some no longer work due to the IDE updating. I found a wonderful wiki (below) that lists most of the different flavour of cli solutions for programming an Arduino. I spent all of several days and ended up concluding that it is not possible to re-program the Arduino over the GPIO without a logic level converter. I did not attempt to program the Arduino from the CLI using the usb connection, but I’m sure it is possible. It is just not possible over the GPIO without extra hardware (a logic level converter). Notes on this at the bottom.


However, the raspberry pi can listen to the output of the Arduino. This is good enough to work for now. I found that there are several methods communication. I explored these to a point of exhaustion.


You might ask why wouldn’t we just hook up the usb cable that we were just using with the computer? Well, I’m working with a Raspberry Pi Model A+, and don want to use the usb for the wifi dongle, so that I can communicate with the thing. The idea here is that I have to use a USB port for communication between the raspberry pi and the Arduino. It’s pretty straightforward. There is also the least risk of breaking or bricking something because it’s USB.

On the Arduino:

Load in whatever sketch you want to run. Either of the ones from above, or anything that outputs to the serial when you have it working on the computer. The Pi will read the same output after you plug it into the USB port of the Pi.

On the Pi:

cd ~
ls /dev/tty*

Check the output of this and figure out which one is the device corresponding to the Arduino USB Something like USB, AMA, or ACM

minicom -b 9600 -o -D /dev/ttyACM0

You should be seeing the output of whatever you loaded on the Arduino. Exiting this program is a bit difficult. Press Ctrl+A, then let go and press Z in rapid succession for the menu.

TxD/RxD —

This is almost identical to the USB. However, there is a little bit of a catch. The Raspberry Pi cannot communicate directly to the Arduino. The Arduino can talk to the pi. However, there are complications in the reverse direction that could possibly brick your raspberry pi. This is the issue that specifically prevented me from being able to program the Arduino from the raspberry pi. The notes on this are below, but I must reiterate the warning. DO NOT hook the bare Arduino Rx into the raspberry pi Tx, it won’t immediately brick the thing, but it still can (and did to one of mine). T = Transmission, R = Receive. This is actually pretty wonderful because this means that the pi can ‘hear’ the Arduino talk! But can’t talk back.


Wire it Up:

Ground to Ground (6)
5V to 5V (plug into breadboard positive)
TxD (1) to RxD (14)

On the Arduino:

Load in whatever sketch you want to run, unplug it from the computer. Since the program stays on the Arduino when unplugged, it’s okay if you’re using the same sketch as before.

On the Pi:

sudo nano /etc/inittab

Comment out this line with a #

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Backup the cmdline.txt file

sudo cp /boot/cmdline.txt /boot/cmdline.txt.backup
sudo nano /boot/cmdline.txt

Remove these items completely (they might not be there)

console=ttyAMA0,115200 and kgdboc=ttyAMA0,115200

Reboot for it to take effect.

sudo reboot

Then run the same command as previous and it should result in the same output

minicom -b 9600 -o -D /dev/ttyAMA0

I2C —

This is a really fast protocol for use with 2 wires. However, the Raspberry Pi has to be master and the Arduino the slave. This means that the Arduino only responds when asked for information. If you took a look at either of the scripts above you would notice that neither would work. This is merely because I2C is more like having a dog on a chain that only barks when you pull, rather crude. I was sorely disappointed when I realized this protocol would not work for this project.

Here are some instructions on getting the I2C link working.

As far as testing out I2C. You’re going to want to learn the Wire.h library for Arduino

Also, since you are dealing with raspberry pi there is a distinct choice between python and C++. Here are two links one that links using C++ the other with python. I highly recommend the python example (2nd link).



This is an extremely fast protocol for use with 4 wires. It’s similar to I2C except it doesn’t limit the slaves to respond only when spoken to. Didn’t get time to investigate this one because the TxD (Arduino) to RxD (Raspberry pi) is sufficient for this project. However, SPI goes through the MISO/MOSO pins on the raspberry pi and could be efficient for potential low power consumption projects in the future.


4.) Notes on Housing

So now we have a raspberry pi hooked up over a wifi network and able to communicate the events in real time. However, it’s still in a breadboard form. We need to get something together so that it’s not just a concept any more. Because breadboarding is for concepts, not actual applications and usage. It turns out that these wires don’t last that long because the wires are prone to break over time. As the wires bend back and forth they corrode, weaken, lose contact, or break.


Generally, it is best to soldier the project together. Get a vanilla board, some soldier (preferably lead-less), A fine point soldiering iron, some goggles, maybe a lamp, maybe a clamp. Also, you’re going to have to have patience and steady hands.

SILICONE (not epoxy)


(Heed the 1-2cm cure depth because it melted/ate through the plastic mold.)

If you build mini wire harnesses and use the wires to hook everything up, potting might be an option. Instead of building a circuit I decided to make a little mini wire harness and pot the whole shebang in silicon.

Potting Epoxy is not an option! Why not epoxy? Because epoxy can break the chips off the circuit boards when it cures due internal stresses. However, silicon is rubbery and won’t induce such internal stresses.

There are generally two kinds of silicon, ones that will rust the heck out of your electronics, and the ones won’t. A rule of thumb is that if the silicone smells like vinegar when it is curing, it is very bad for electronics. Other than that, just about any silicon will work. However, there are drawbacks.

The particular silicon I picked for this project had to be cured outdoors (or in a shop) because the cure produces carcinogenic gasses (per label). Also, because of the 1-2 cm cure depth (in the spec but not on the label) the project was basically a bust for me. The mold for the pot that I chose was clearly deeper than that, and it caused a major problem. I salvaged what I could and it still works, but plan on potting in 1 cm layers for whatever you do.

I really wanted clear silicone, but somehow the order came through with black, and I had some clear leftover from a previous project.

CLAY – Air dry/Oven dry

There are two types of clay: oven bake clay and air dry clay. Air dry clay requires a barrier between it and electronics because it contains water, but once dry does not contain water. Bake clay is more oily and can contact directly to the electronics.

I wouldn’t recommend air dry clay for a project like this, but it might be useful for another type of project. The main problem is the water and possible corrosion, but on some projects it could work feasibly.

Oven dry clay has some interesting properties. It is more expensive (at least at the craft store I got it from). Comes in lots of colors. It is oily and pliable. However, the instructions want you to cook it at 275*F, not an option. I tried to use this clay to seal off some items in the pot prior to pouring silicone and it yielded a strange result. The clay did set, but was fairly weak and easy to crumble.

I might try this, but it’s only conjecture. It appears that the oven bake clays vary in this cooking aspect. The only advice on how hot a raspberry pi can get is one item that said 158*F for a specific component. Solider melts at roughly 180*F (but varies). The idea would be to attempt to cook a raspberry pi potted in oven dry clay at 150*F. Possibly in a crock pot, or a good oven, for an extended period. Say a 8-12 hours. It could work… maybe…

5.) Notes on ‘on the fly’ Arduino reprogramming (skip if in a hurry):

Imagine that, in order to conserve power, you want the Arduino to boot up the raspberry pi only when it needs it’s advice or help. After a few minutes it should shut down the pi for lack of need. The easiest is actually powering the Arduino from the pi. For the Arduino to control the raspberry pi is possible, but would require using a relay. There is one in the sensor kit that will work, but the intention for this project is a wall outlet for now. It is possible to purchase smaller relays that are not as power hungry for the same purpose. I’m planning on doing this, but not right now.


I learned a lot about how a raspberry pi might be used to reprogram an Arduino. Think about it, the Arduino could be optimized for a specific task, but the pi could reprogram it to be optimized for a different task automatically. The idea here is that we make a default Makefile, and copy it into a working directory for every sketch that we want to make and edit on the fly. This way we can write the .ino file and use commands “make” (to compile) and “make upload” (to send the sketch to the Arduino). No matter what compiling with “make” is going to take a some time. However, uploading shouldn’t take that much time and compiling only has to be done once. There is a standard script that blinks a light on the Arduino and most everyone uses it to check weather the thing is working properly.

cd ~
mkdir sketches
cd sketches
nano Makefile

Paste this in:

ARDUINO_DIR = /usr/share/arduino
BOARD_TAG    = uno
include /usr/share/arduino/

#xARDMK_DIR = /usr/local

For a list of the tags run this, most likely you’re using an uno. This needs to be correct for the BOARD_TAG.

ard-parse-boards –boards

Make a folder for the standard blinky script and copy everything in

cd ~
cd sketches
mkdir on
cp Makefile on/Makefile
cd on
nano on.ino

Paste the following script in. This is that blink script.

void setup() {
pinMode(13, OUTPUT);

void loop() {
digitalWrite(13, HIGH);
digitalWrite(13, LOW);