Hi everyone. I am relatively new to the Arduino Community but have more experience with Raspberry Pis.
I am intending to interface to a slave Arduino with my Raspberry pi through the GPIO pins. I was wondering whether it was better to connect a Pi via I2C or Serial connection? Why would one be better than the other? And what use cases is each one better at?
using serial is probably simpler than I2C
you can send a text string which are then parsed at the receiver, e.g.
an Arduino sending text to a pi using Serial1.print()
char text[100]={0};
sprintf(text,"ADRM1 %d %d Time %ld microseconds, distance %ld cm %ld %ld %ld\r\n",
numberOfSteps, stepCount, HighLevelTime, distance, last, test, count);
Serial1.print(text);
and the pi parsing (using Java) the received text in StringBuilder line
if(comPort.bytesAvailable() > 0) {
byte[] data = new byte[10];
comPort.readBytes(data,1);
System.out.print((char)data[0]);
if(data[0] != 13)
line.append((char)data[0]); // add character to line
else
{ // have a line of text, split line and parse it for radar plot data
if(line.indexOf("ADRM")<=0) { line.setLength(0);continue;}
String data1[]= line.toString().split(" ");
// parse vlues from the array data1
numberOfSteps = Integer.parseInt(data1[1]);
stepCount = Integer.parseInt(data1[2]);
etime = Integer.parseInt(data1[4]);
Unless you are using a 3.3V Arduino it is wise to use a level converter for any interfacing between a Raspberry pi and an Arduino as the pi pins are not 5v tolerant, e.g.
in the example of #1 I used an Arduino Mega as it had hardware serial
you could use I2C but it is more complex, e.g. one side has to be programmed as master and the other as a slave
unless the data exchange is very simple even in the case of serial you still require a protocol to make sure data is not lost
loyalburrito:
I am intending to interface to a slave Arduino with my Raspberry pi through the GPIO pins. I was wondering whether it was better to connect a Pi via I2C or Serial connection? Why would one be better than the other? And what use cases is each one better at?
What distance do you want to bridge?
In general, I/O lines should not be used to bridge long distances from one board to another. Microcontroller I/O pins have little protection against electrostatic discharge, and they should not drive large capacitive or inductive loads (long wire have both). These issues are solved by using transceivers that are designed to drive wires, create waveforms that are immune to noise e.g. differential signals and can have additional protection circuits build in. These transceivers are typically built for a specific bus protocol e.g. RS-232, CAN, ....
Serial is asynchronous (no clock). They are basically two independent signals that can send any time they want. Because there is no clock signal both sides must agree on the speed.
I2C has a clock signal which makes it more robust but as others noted one side is the master and controls when data is send or received. Some Arduino's have issues with being a slave. This could be a library issue. I2C has only one data line and needs pull-up resistors on both lines (clock and data) to work.
I do not think wire length would be an issue in my case. They are essentially stacked up right next to each other. The longest wire length would be about 2-3 cm at most.
You can also use SerialTransfer.h (Arduino side) and pySerialTransfer (RPi/Python side) to automatically transfer serial data reliably. Check out the readmes and examples included in each lib for further reference