Discuss on GitHub

Programming an Arduino from Raspberry Pi

Problem

You want to run the Arduino IDE on a Raspberry Pi so that you can write and upload programs onto an Arduino.

Solution

The Arduino IDE is available for the Raspberry Pi. It is a little bit slow, but usable. Use these commands to install it:

$ sudo apt-get update
$ sudo apt-get install arduino

At the time of writing, this installs version 1.0.1, which is not the latest version but will suit the Arduino Uno; it will not, however, work for newer boards like the Leonardo and Due. They can still be used with the Raspberry Pi, but you will need some other computer to program them before connecting them to the Raspberry Pi.

After installation, you will find an Electronics group in your Programs menu (Figure 10-1).

Figure 10-1. The Arduino IDE running on Raspberry Pi

The Arduino IDE connects to the Raspberry Pi through its USB cable to program it. This connection also requires that the serial console be disabled. You can follow “Freeing the Serial Port” to do this, but a second option is to run a script created by Kevin Osborn that both disables the serial console and configures the serial ports and Arduino profiles necessary to get things running. This has the advantage that it also sets up the aLaMode board to be ready for use (“Getting Started with an aLaMode Board and a Raspberry Pi”).

To download and run this script, follow these steps:

$ wget https://github.com/wyolum/alamode/blob/master/bundles
  /alamode-setup.tar.gz?raw=true -O alamode-setup.tar.gz
$ tar -xvzf alamode-setup.tar.gz
$ cd alamode-setup
$ sudo ./setup

If you have not previously disabled your serial console and are relying on the preceding script to do it, then you will need to reboot for this change to take effect.

$ sudo reboot

You can now connect your Arduino to your Raspberry Pi. From the Tools menu, select Board and set the board type to Arduino Uno. Then, from the Serial Port option, select /dev/ttyACM0. To upload a test program that will make the LED on the Arduino blink, select the File menu and then click “Examples, Basic,” and finally click Blink. Click on the right-arrow on the toolbar to begin the compile and upload process. If all is well, you should see a “Done Uploading” message in the status area at the bottom of the IDE window.

If you find that the device ttyACM0 is not listed even though your Arduino is plugged in, try restarting the Arduino IDE. If that doesn’t work, then you may have to reboot your Raspberry Pi. Leave the Arduino connected while you reboot and restart the Arduino IDE.

Discussion

To get the most out of using Arduino with Raspberry Pi, you need to learn a little Arduino programming. You may find the book Programming Arduino: Getting Started with Sketches (McGraw-Hill/Tab Books), by yours truly, helpful.

You can, however, make use of an Arduino without needing to write any code on the Arduino side, using a project called PyFirmata. “Setting up PyFirmata to Control an Arduino from a Raspberry Pi” explains how to use PyFirmata.

See Also

The Arduino IDE setup script came from the blog Bald Wisdom.