I am new to Raspberry Pi (just brought mine before writing this post), and I am looking to see what is the most ideal way for two way communication between an Arduino and a Raspberry Pi (USB, SPI or I2C).

The reason I am asking is because I need to build a small, semi-autonomous robot and plan on using both the Arduino and Pi to control it, using the pins of the Arduino for the motor control and sensors and the Pi for video processing and autonomous part. Both will be on the robot itself. I am pretty good with the Arduino but never used a Raspberry Pi before.

So my biggest question is what would be the best form of communication for the two boards taking into account speed and safety (there is a voltage difference between the Pi and the Arduino).

up vote 1 down vote accepted

Hands down, I can rule out the I2C. This post sums it all up:

  • SPI is faster.
  • I2C is more complex and not as easy to use if your microcontroller doesn't have an I2C controller.
  • I2C only requires 2 lines.

Since speed is your first priority, either do USB or SPI. I'd personally go with USB, just for the reason that it's simple. Nearly any programming language can inference with COM ports, but that's not the case with SPI. Also, for SPI, you would have to either use a level shifter, or do a voltage divider for the Arduino to RPi wires (for RPi to Arduino wires, Arduino can still sense 3.3V as HIGH.)

Also, serial can be pretty fast. Technically, the Arduino can go up to 2 Mbps (not very fast compared to a lot of things, but fast enough for what the Arduino can even handle). One thing to note, the writer of the post stated that this is the highest you can practically achieve with the poorly optimized libraries:

Serial.begin(500000);

Firsr of all, When you say "there is a voltage difference", I assume you are referring to the raspberry pi being a 3.3v device vs the arduino's 5v, and not to some kind of ground potential difference. If theres a ground potential difference(which there shouldn't be in a robot), neither i2c or spi or usb will work without extra circuitry.

My short answer would be USB all the way because its easy and built into the arduino and pi. The only issue is that if anything mechanically bumps the usb enough to lose connection, sometimes linux is a little unpredictable with regards to naming serial ports, and when it reconnects it might give the arduino a different serial port name. But this isnt an issue if your cable isnt a cheap worn out one with a flaky connection like the one that bothered me for a hour today.

USB does have about a millisecond of latency because devices are usually polled at 1khz iirc. Using the raspberry pi hardware serial might be a good idea if milliseconds count which I doubt they do.

But on the software side, raspi hardware serial and USB serial are the same. So there should be no problem switching between them if you need to.

I wouldnt bother with i2c or spi. Why fuss around when serial works so well?

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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