Sunday, December 15, 2013

Raspberry Pi with Arduino Slave

The power of the Raspberry Pi is incredible, but amazingly there are tasks which it is not fully equipped to handle.  Such items are 5V Logic (for certain chip sets and controllers), Analog I/O (for variable level sensors) and Real Time operations (for motor controllers).

Arduino Uno

A common solution is to attach another micro-controller board such an Arduino.  The Arduino is a versatile micro-controller that comes in a variety of sizes and packages with "Shields" for advanced expansion.  The most common version is the Arduino Uno (pictured below).

Arduino Uno Packaging
Arduino Uno fits in the palm of your hand
There are several methods to communicate with an Arduino from the Raspberry Pi. Serial, I2C and GPIO are common methods.  The method I prefer is to communicate over Serial, through the USB port, that I program and power the Arduino with (not to be confused with Serial through the serial pins on both boards).

To connect the Arduino Uno, using a USB cable (USB type B to A cable) I connected it to a powered USB hub.  Then I connected the USB hub to my Raspberry Pi.  If your power adapter is sufficient amperage, you should be able to connect the Arduino directly to the Raspberry Pi.  I will have several other USB device attached, so the USB powered hub works best for me.

Arduino Uno connected to USB
connected to Raspberry Pi
Arduino Uno close up

Arduino IDE

Now that the Arduino is attached to the Raspberry Pi, we can program the Arduino using the Arduino IDE.  This can be installed as easy as:
# sudo apt-get install arduino

And then running the Arduino IDE, from within a Windows Manager, with:
# arduino

Arduino Getting Started (Blink)

The Arduino Getting Started Guide has excellent instructions for getting started with the Arduino.

As the Arduino Uno is the "default" for the Arduino IDE, we do not need to make any changes to the IDE to be able to upload our program (Arduino calls the programs Sketches) to Arduino.

From the template Sketches, select the "Blink" example.  This sketch will cause the little LED (labeled "L" on my Arduino Uno) to blink on and off.
For now, go ahead and click the "Build/Verify" button to compile this sketch into Arduino binary code.  Finally click the "Upload" button to send the code to the Arduino.  A few seconds later the Arduino should blink on and off.

Congratulations, you can now program the Arduino from your Raspberry Pi.

What's Next?

Next, I will cover how to program the Arduino from the Command Line, and how to talk to the Arduino through Serial (over the USB port)

No comments: