#“Connecting an Arduino to Raspberry Pi for the best of both worlds”
Rather than struggle with the very basic unprotected IO pins on the Raspberry Pi and the lack of real-time performance in Linux, the ideal setup for many real-world-interfacing projects is Raspberry Pi + Arduino.
After pricing a multitude of combinations of microcontroller boards and Wifi adapters, I found that the Raspberry Pi + USB Wifi + Arduino is the absolute best value for money if you need both wireless internet access and easy sensor data handling. (Note: I’ll be investigating the Beaglebone Black soon to see how it compares).
There are four basic ways to connect Arduino to Raspberry Pi:
- Buy an add-on board like the Gertboard which has an Arduino compatible IC on it. Pricey.
- Plug a standard Arduino like an Uno or Nano into the USB port of the RPi. This is by far the easiest method and minimises wiring and hassle. However it requires the more expensive Arduinos.
- Use a USB to Serial adapter with a cheaper/smaller Arduino like a Pro Mini or a self-made Shrimp. This is the best DIY option and has the same advantage of method 2 that you can power the Arduino/Shrimp from USB. For a Model B RPi, I'd recommend this route.
- Use the Serial Pins on the Raspberry Pi to connect to a cheaper/smaller Aruduino like a Pro Mini or a self-made Shrimp. This is theoretically the cheapest method but by far the most hassle. This is also the best method if you are using the cheaper Raspberry Pi Model A and its single USB port is being used for Wifi.
The basic steps are identical to those described here. In hardware terms You connect the 3.3V/GND/TX/RX pins on the Raspberry Pi via a level converter to 5V/GND/RX/TX pins on an Arduino. Alternatively you buy a 3.3V Arduino and avoid the need for a level converter. I powered the Arduino separately to avoid overloading the RPi pins (this seems to be causing intermittent issues with RPi booting tho).
RPi software changes involve commenting out this line in /etc/inittab with a #
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
and removing the following parts of the one line in /boot/cmdline.txt
You also need to create a link to the serial port so that the Arduino IDE recognises it:
sudo ln -s /dev/ttyAMA0 /dev/ttyUSB9
That last step has to be done after every reboot. I should find a way to make it permanent.
Unfortunately this still isn’t enough to be able to program the Arduino from the IDE running on the RPi. We need to be able to toggle the reset pin on the Arduino to initiate programming. Normally that’s done by the USB-Serial adapters or the USB-Serial chip on the more expensive Arduinos. To do this in our setup requires following the instructions herewhere one of the Arduino tools is wrapped in a script which does the pin toggling at exactly the right moment. In summary:
- Connect Pin 11 (GPIO 17) of the RPi to the DTR Pin on the Arduino Pro Mini via the level converter
- Run the following commands to download and configure avrdude-rpi:
sudo apt-get update sudo apt-get install python-dev sudo apt-get install python-rpi.gpio wget https://raw.github.com/deanmao/avrdude-rpi/master/autoreset wget https://raw.github.com/deanmao/avrdude-rpi/master/avrdude-autoreset sudo cp autoreset /usr/bin sudo cp avrdude-autoreset /usr/bin sudo mv /usr/bin/avrdude /usr/bin/avrdude-original sudo ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude sudo chmod 755 /usr/bin/avrdude-autoreset sudo chmod 755 /usr/bin/autoreset
I was getting a warning about “Channel already in use” from /usr/bin/autoreset so I added this to the end of the file:
Note you must run the Arduino IDE as root from now on. i.e. in LXTerminal:
If you haven’t already, reboot the RPi to make the earlier steps kick in. You should now be able to program whatever you like on to the Arduino using the IDE running on the Raspberry Pi. I even have this running headless by connecting to the RPi across the network using the Windows Remote Desktop Connection tool.
Any questions, pop them in the comments.