Arduino <==> Raspberry Pi communication

When Terry King visited last fall, he mentioned that one of the things missing on his site (http://arduino-info.wikispaces.com) was how to communicate with a Raspberry Pi.  He mentioned this to me because I showed him my RS485 based home automation node and said that a Pi was the master.  So, I prepared a presentation for the January 2016 TriEmbed meeting.

slides

video

One issue that came up during the presentation was the dropping of every fourth character sent to the Pi in demoB.  After the presentation, someone from the audience (I am sorry to say I do not know your name)  explained to me what he thought was happening.  The issue is with Software Serial disabling interrupts while it is bit banging the data to the Pi.  So, if I type “raspberry\n” into the Serial Monitor on the PC, it gets transmitted at 38400 baud to the Arduino UART (pin 0 RX).  Once the sketch gets the first character “r” from Serial, it writes it using mySerial at 9600 baud out pin 3 to the Pi.  During that time, interrupts are disabled.  Additional characters arrive on pin 0 with interrupts disabled, so the UART is not unloaded, causing an overflow of the internal buffers.  I found this line in the atmega 328 datasheet:

A Data OverRun occurs when the receive buffer is full (two characters), there is a new character waiting in the Receive Shift Register, and a new start bit is detected.

Since there is only room for three characters and four (4 = 38400/9600) characters arrive while interrupts are off, that explains why one out of four characters is dropped.

Here is a logic analyzer picture (Click on it for high res view) of what is happening on pins 0 and 3.  While ‘r’ is being sent out on D3, “aspb” shows up on pin D0.

Screenshot 2016-01-16 21.39.59

 

The solution is to reduce the baud rate between the PC and Arduino so that three or fewer characters arrive while interrupts are turned off.

The one thing that still puzzles me is that the letter ‘p’ is dropped, not the letter ‘b’.  The letter ‘r’ has been unloaded, so “asp” should be in the internal UART buffers and ‘b’ should cause the overflow.