43

The Arduino website sums it up as

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

It can be a nice interface to control servos and provide other connectivity provided by its many optional shields.

How do I connect one to a Raspberry Pi? How do I setup communication?

38

Arduinos usually appear as USB serial devices. The current boards use the USB serial driver built into the main microprocessor, but older Arduinos (and clones) used separate third-party USB-serial chips.

To simply receive Serial.print data on the Raspberry Pi from the Arduino, I use the GNU Screen program as a basic terminal: screen [serial-port] [baud-rate] (for instance screen /dev/ttyACM0 9600).

I tested three different Arduinos, and one rather different clone. The newer variants all appeared as /dev/ttyACM0 ports, and the older ones /dev/ttyUSB0. This is what I found, under Raspbian:

The Raspberry Pi may not provide enough power to drive an Arduino, so you might need external power. For completeness, I also tested a Prolific PL2303, even although it's not on any Arduino I know of. It appeared quite happily as /dev/ttyUSB0.

For more complex communications with sensors, you might consider Firmata, "a generic protocol for communicating with microcontrollers from software on a host computer". It has implementations for Arduino, and Python libraries to run on the Raspberry Pi side.

Here's a small example using pyFirmata to read an LM35 and change the brightness of an LED:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# simple test of pyfirmata and Arduino; read from an LM35 on A0,
#                                       brighten an LED on D3 using PWM
# scruss, 2012-08-14 - tested on Arduino Uno & Raspberry Pi (Raspbian)

import pyfirmata

# Create a new board, specifying serial port
board = pyfirmata.Arduino('/dev/ttyACM0')

# start an iterator thread so that serial buffer doesn't overflow
it = pyfirmata.util.Iterator(board)
it.start()

# set up pins
pin0=board.get_pin('a:0:i')             # A0 Input      (LM35)
pin3=board.get_pin('d:3:p')             # D3 PWM Output (LED)

# IMPORTANT! discard first reads until A0 gets something valid
while pin0.read() is None:
    pass

for i in range(10):
    pin3.write(i/10.0)                  # set D3 to 0, 10%, 20%, ... brightness
    print "PWM: %d %% Temperature %.1f °C" % (i * 10, pin0.read() * 5 * 100)
    board.pass_time(1)                  # pause 1 second

pin3.write(0)                           # turn LED back off
board.exit()

There are some caveats when using pyFirmata:

  • Analogue reads and PWM writes are normalized to a 0 .. 1 range, and not the standard Arduino 0 .. 255 and 0 .. 1023.
  • You really need to start a separate iterator thread to stop old readings overflowing the serial buffer
  • Since the Arduino is read asynchronously, make sure that the pyFirmata connection is fully initialized before reading from ports. Otherwise, None values ensue.
  • 1
    I've also confirmed that you can run Processing on the Raspberry Pi. It's very slow, but I did manage to get it to control an Arduino over a USB link: Controlling an Arduino from Raspberry Pi using Processing. I'll see if I can port this to Python/tkinter ... – scruss Aug 13 '12 at 3:07
  • Thanks for this answer it was very useful. I'm using python serial to read from /dev/ttyACM0 and find it very inconsistent. I assumed it was the lack of power to my arduino but I've hooked it up to an external power supply and am still having issues. Sometimes the data is malformed, sometimes an exception is thrown because /dev/ttyACM0 isn't found (though subsequent runs will find it). Have you had similar issues? – Andy Smith Nov 6 '12 at 22:13
  • No, this doesn't match my experience. – scruss Nov 7 '12 at 3:46
  • Thanks for the response - interesting, must be something I'm doing wrong then! – Andy Smith Nov 7 '12 at 9:33
  • Maybe ask a non-Pi specific question on SE, or ask directly on the Arduino forum. Serial comms often gives me a headache ... – scruss Nov 7 '12 at 12:39
13

In case it's not clear, with Raspbian Wheezy, you can run the Arduino IDE and upload sketches to the Arduino when connected to the Raspberry Pi's USB port. To install on Raspbian, just type into the shell (aka command line):

sudo apt-get install arduino

It will download and install all the needed packages. I've have a couple photos of the Arduino IDE in Raspbian here:

http://www.element14.com/community/groups/raspberry-pi/blog/2012/07/03/review-of-raspberry-pi-images

(as other folks have described above, the Raspberry Pi could also program the Arduino via the serial port (UART) pins on the GPIO header. In this case you would not have to "waste" a USB port, but it would require additional configuration).

Cheers, Drew

  • I tested an Arduino Uno with RGB LED with simple fading sketch and the Arduino Uno was only powered from the Raspberry Pi's USB port. I did not have any problems, but, of course, YMMV. – pdp7 Aug 9 '12 at 18:00
8

One can opt to connect the rpi to the arduino using a usb port or by using the GPIO pins. One can use Wiringpi to do the communication between the boards.

Currently there is also a bridge being developed called Ponte.

A basic hello world can be found here.

7

The à la mode

Billed as The proper way to put an Arduino in a Raspberry Pi by Hack A Day, the à la mode is a stackable Arduino clone designed by Anool Mahidharia, Justin Shaw and Kevin Osborn from the Wyolum.com OSHW collaborative. Hack A Day described it as follows

Right off the bat, the AlaMode plugs directly into the GPIO pins of the Raspberry Pi. From there, communication with the ATMega of the Arduino is enabled, allowing you to send and receive data just as you would with an Arduino. There’s a real-time clock, servo headers, plenty of ways to power the board, and even a breakout for [a] GPS module.

A lot of unnecessary cruft is done away with in the AlaMode; There’s no USB port, but it can be programmed directly over the GPIO pins of the Raspberry Pi. Pretty neat, and we can’t wait to grab one for our [Raspberry Pi].

Kevin gives a little more details on his blog:

Here are the features including a few extra goodies:

  • Flexible power. Can be powered directly from the Pi, standalone with a battery or wall-wart, or USB power. This is important if your shield takes more power than the Pi can provide or if you want to undock it for standalone operation. Programmable via the Pi’s UART on the GPIO pins, or an FTDI USB-Serial adapter or ISP.
  • Header for connecting Fastrax UP501 GPS. DS3234 Real time Clock. The Pi doesn’t have it’s own battery backed RTC. You can set a program in the AlaMode to report the time to the Pi via serial or I2C
  • Micro-SD card slot. Useful for datalogging, and big-memory for your Arduino applications
  • Row of Servo Headers connected to the PWM pins with a configurable power and ground rail

Limited Beta

The board is currently in limited beta, but it deserves to be mentioned as a well thought out clone, which has been announced by a group with previous form.

References

  1. Hack A Day's Blog Post
  2. Kevin Osborn's Blog Post
  3. Wyolum.com
4

You might consider the Gert Board which is now Arduino based if you want a general purpose hardware interfacing platform. One advantage is that there is extensive documentation on interacting with the RPi. Available to pre-order from Element14 as of 8/8/12.

0

There is a RaspberryPi HAT that behaves like Arduino and can be programmed through its Web UI from any browser. Project called CoPiino. It comes completely with software up and running. Just enter the Arduino Sketch in the browser UI and hit "compile and run" - thats it.

  • given that you have posted at least two answers that are promoting CoPino I suspect that you are connected to this project. Any connection should be noted in your answer, – Steve Robillard Oct 11 '15 at 23:59
  • Yes, that's absolutely correct and my comment covers the topic totally. The CoPiino board is similar to the already mentioned a-la-mode board. Where CoPiino comes up with some nice additional features. Where programming the Arduino from Pi is easily covered - which was the initial question. – tswaehn Oct 12 '15 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.