45

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?

Alex Chamberlain
  • 15,120
  • 13
  • 63
  • 112
Lucas Kauffman
  • 875
  • 2
  • 7
  • 15

6 Answers6

39

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.
Jacobm001
  • 11,797
  • 7
  • 45
  • 56
scruss
  • 8,928
  • 1
  • 23
  • 34
  • 1
    I've also confirmed that you can run [Processing](http://processing.org/ "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](http://scruss.com/blog/2012/08/12/controlling-an-arduino-from-raspberry-pi-using-processing/ "Controlling an Arduino from Raspberry Pi using Processing"). I'll see if I can port this to Python/tkinter ... – scruss Aug 13 '12 at 03: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 06 '12 at 22:13
  • No, this doesn't match my experience. – scruss Nov 07 '12 at 03:46
  • Thanks for the response - interesting, must be something I'm doing wrong then! – Andy Smith Nov 07 '12 at 09: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 07 '12 at 12:39
  • I've updated the Processing/Firmata/Arduino instructions for the new Java: [Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = ☺](http://scruss.com/blog/2014/01/07/processing-2-1-oracle-java-raspberry-pi-serial-arduino-%e2%98%ba/ "Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = ☺") – scruss Jan 08 '14 at 02:46
  • Tried with an Arduino Nano, it appears as /dev/ttyUSB0 on my Raspberry Pi Zero, but couldn't get it to work (sending char to the serial port using python on the RPi, and program to receive it on the arduino). Changed to a Arduino Leonardo, and it worked perfectly using the same setup. – salle55 Sep 10 '17 at 11:37
  • tried *what*, @salle55? While I didn't address reading data on the Arduino in detail, remember it's a very simple character by character process as befits such a simple device. Leonardos can do funny things with serial data, [like locking up unhelpfully if serial reading is not maintained](https://raspberrypi.stackexchange.com/questions/67641/question-about-arduino-talking-to-pi-over-usb/67673#67673). Nanos wouldn't have this problem. – scruss Sep 10 '17 at 21:21
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

xxmbabanexx
  • 3,198
  • 7
  • 32
  • 56
pdp7
  • 161
  • 2
  • 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 09 '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.

Steve Robillard
  • 34,158
  • 17
  • 102
  • 108
Lucas Kauffman
  • 875
  • 2
  • 7
  • 15
8

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
Alex Chamberlain
  • 15,120
  • 13
  • 63
  • 112
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.

Guy
  • 1,607
  • 1
  • 15
  • 18
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.

tswaehn
  • 260
  • 1
  • 6
  • 1
    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
  • Four years on you still have not edited in your affiliation. Please do so. – jonspaceharper Oct 29 '19 at 09:24