Sponsored by

Navigate

Check these out

Arduino and Raspberry Pi Serial Communication

Today’s the last day of my summer holiday, and I had some free time on my hands. So I decided to see if I could get my Arduino Uno and Raspberry Pi to talk to each other. It turned out the task was even easier than my previous Pi to RS-232 project – all that was needed between the two devices was some jumper wire and two 1 kOhm resistors to form a voltage divider between Arduino TX pin and Pi RX pin – Arduino understands Pi’s 3.3V signal levels just fine so Pi TX to Arduino RX needed no voltage shifting at all.

IMPORTANT UPDATE! It turns out that the RX pin on the Arduino is held at 5V even when that pin is not initialized. I suspect it is due to the fact that the Arduino is programmed via these same pins every time you flash it from Arduino IDE, and there are external (weak) pullups to keep the lines to 5V at other times. So the method described below may be risky – I suggest either add a resistor in series to the RX pin, or use a proper level converter (see this post for details how to accomplish that). And if you do try the method below, never connect the Pi to Arduino RX pin before you have already flashed the program to Arduino, otherwise you may end up with a damaged Pi!!!

Setting Raspberry Pi up for serial communications

In order to use the Pi’s serial port for anything else than as a console, you first need to disable getty (the program that displays login seen) by commenting the serial line out of Pi’s /etc/inittab:

If you don’t want the Pi sending stuff over the serial line when it boots, you can also remove the statements console=ttyAMA0,115200 and kgdboc=ttyAMA0,115200 from /boot/cmdline.txt. You’ll need to reboot the Pi in order for the changes to take effect.
If you have a 3.3V compatible serial adapter in your computer (or a MAX3232 and normal RS-232 adapter, in which case see my tutorial on building one), it’s a good time to try out if everything is working on the Pi side. Install minicom (sudo apt-get install minicom) and attach is to serial terminal:

minicom -b 9600 -o -D /dev/ttyAMA0

Then open Putty or similar serial terminal on PC side. Everything you type into minicom should appear in the serial terminal on PC side, and characters typed to serial terminal should appear on minicom.

Connecting Arduino Uno to Raspberry Pi

There’s basically two ways to link the Arduino to the Pi. Easier route would be to just plug Arduino into the Pi via USB. The USB to serial bridge should be automatically recognized and be available at /dev/ttyACM0. But if you want to do it the hard way like I did, you can also connect the Pi GPIO pins (seen on the right, 3.3V not needed this time) to Arduino:

Connect grounds of both devices (triple check first that they are the grounds!)

As a voltage divider, I used a 1 kOhm resistor between the Arduino TX and Pi RX, and another 1 kOhm between Pi RX and ground. That way, the 5V Arduino signal voltage is effectively halved. Connect the resistor ladder first, and then the Pi RX between the two resistors, so there’s at no point a voltage over 3.3 volts that could damage the Pi! You can see the connection in action here.

Communication between Pi and Uno

For RaspPi side, I’d recommend minicom (see the command-line above) for testing, and pySerial (sudo apt-get install python-serial) for interaction. Using Python, you can easily make the Pi do lots of interesting things when commands are received from Arduino side. Here’s a simple ROT-13 application that works with the GPIO serial interface:

Note that you shouldn’t use these programs together, or nothing happens (and you won’t see anything either) – Pi side programs can be tested with a PC serial adapter and Putty, and Arduino side programs with minicom running in the Pi.

That’s it for this time. Now that I have the two things communicating, I think I’ll do something useful with the link next. Maybe a simple Arduino “HDMI shield” using RaspPi!

Published by

Joonas Pihlajamaa

Coding since 1990 in Basic, C/C++, Perl, Java, PHP, Ruby and Python, to name a few. Also interested in math, movies, anime, and the occasional slashdot now and then. Oh, and I also have a real life, but lets not talk about it!
View all posts by Joonas Pihlajamaa

41 thoughts on “Arduino and Raspberry Pi Serial Communication”

Do you know if it’s actually safe to program the Arduino while it’s connected to the Raspberry Pi like this? As this happens via the TX/RX pins, you get 5V on the RX pin of the Arduino, thus 5V on the TX pin of the Raspberry Pi.

Extremely observant note! I had not realized this. And actually, when I measured, the pin is at 5V even when the pin is set as input and no internal pullup! I suspect it has a weak external pullup due to the double function as programming interface.

After noticing this, the method described in this article doesn’t seem so good anymore. A series resistor (I don’t know if 1k would still work) might be a good idea, or a proper level conversion buffer.

Thanks for pointing it out, I updated the text above to include a warning.

I struggle already several hours but i can not get it working. Yesterday it works once, but today only output works. I know that the serial interface is OK, because i can see boot information and can login. Done disable items in /etc/inittab and /boot/cmdline.txt What i am forgotten to do? And there is something strange with readline, this command does not accept arguments? Maybe wrong version or so? Do you have an idea?

First, hopefully you read the warning I added last week, as the Arduino programming can put 5 volts to RX, so having a MAX3232 or a level converter chip between the Arduino and Pi is preferred, or at least a series resistor (1k may work, haven’t tried that).

Second, make sure you don’t have flow control on anywhere, as it seems the Pi UART doesn’t have that.

Also note, that the Arduino-side example and Pi-side example cannot be connected together – instead, you can test the Pi-side Python script with Putty terminal on PC side, and Arduino side echo with minicom or similar on Pi side.

Strange that serial library doesn’t have the eol-parameter, it might be a different version. I cannot help you there much – Putty sends only \r and not \n on newline, so unless that parameter is there, readline() never returns, because it’s waiting for \n forever.

Putty sends only \r and not \n on newline, so unless that parameter is there, readline() never returns, because it’s waiting for \n forever. <== That's the problem, with hyperterminal it works well. But why that parameter does not work with my Raspbian installation? Can i do somewhere a version check or so?

Considering the python serial library is installed with “apt-get install python-serial”, you can probably use the apt commands to check version number. You might want to check pySerial home page for installation instructions: http://pyserial.sourceforge.net/index.html

Hmm, unfortunately I don’t have my Pi at hand.. You could first try to not disable serial login (leave inittab as it is) and connect from PC to the Pi with Putty, using 115200 as the baud rate. If serial terminal doesn’t work “out of the box”, it’s probably either an operating system issue (different device name, settings, etc.) or there might be some issue with your PC serial port (or USB-serial converter or whatever you use). RaspPi forums probably can also offer more help!

I’ve seen the same happen when I’ve had invalid flow control (there should be none, no RTS/CTS or XON/XOFF, IIRC) or other connection settings (parity, speed, etc.) – on the positive side, getting garbage has usually resulted in working connection in a few minutes after I’ve fixed the settings.

I am trying to make an Ethernet serial bridge out of the Pi. I loaded minicom and have the serial port of the Pi working. I then typed in “minicom -b 9600 -o -D /dev/ttyAMA0” I didn’t disable the startup screen, and I can login, etc. just fine over RS-232.
Then, I used Putty to login with the same name/pw over SSH. That, of course works also. While the serial port does echo my keystrokes, I see nothing in the Putty window when I type into the serial port, and when I type into Putty, I see nothing on the serial side. What am I doing wrong? I did notice that the serial port is not running at 9600 baud – it is actually running at 115Kbaud.

Only one program (I think) can use the ttyAMA0 serial device at a time, so if you fire up minicom on the Pi side (e.g. from a SSH session), you’ll need to disable the login on ttyAMA0 first and reboot, otherwise it might not work or work in strange ways (e.g. you can send characters but getty will receive them…).

Once you have login disabled and minicom running, just use the same settings (baud rate) for Putty on your PC. Characters won’t echo on either side without additional settings, but everything you type on one end will show up in the other – things typed in minicom should show in Putty and vice versa.

Baud rate can be set differently on both sides, but it will only work properly if it’s the same.

Also, note that if you run the Pi headless, you use one Putty to open SSH connection to Pi and run minicom there, and /another/ Putty to open _serial_ connection to COMxx to the Pi. :)

I have the arduino set up so that it is powered by the pi (pi 5V>uno Vin;pi GND>uno GND). do i have to connect the two grounds when i have it like this, and can i connect the 1 kohm resistor to the arduino GND instead of the pi’s GND, since they are essentially the same thing?

No, you don’t need to have several GND->GND connections when powering the Arduino from Pi. Actually in some cases several ground paths may be bad. And likewise, 1 kohm can be connected to Arduino GND, too.

Yes, that’s why there’s the warning in the very first chapter. Using resistors as voltage divider should be safe, but the problem of course is that RaspPi TX pin (Arduino RX) is only 3.3V voltage divider cannot be used.

In any case, I’ll add a link to a later post containing the use of a level shifter with Arduino.

Appreciating the persistence you put into your site and in depth information you offer.
It’s good to come across a blog every once in a while that isn’t the same
unwanted rehashed information. Fantastic read! I’ve bookmarked your site and I’m including your
RSS feeds to my Google account.

in place of raduino i am connecting raspberry pi and now i am communicating between two pi via RX TX but while running the code if i am sending 1 then i am receiving 9 or some garbage value like @ $ like this. can any one help me with that??
thank you so much

Nice work! I have done a similar serial communication project between Raspberry Pi and Arduino, but as I am not familiar with Python, I did the coding in Simulink. Also, MATLAB has a neat way for editing the /boot/cmdline.txt.

If you have the necessary versions, running the example should be just “python rot13example.py” (or whatever you name it). Or if you have troubles with that, you can just google for using Minicom or something similar with Pi (“raspberry pi serial terminal” or maybe add “external” there). Unfortunately I cannot help you much with any technical issues, so if it doesn’t work, some Pi forum might work. Good luck!

Hi,
3.3V versions of the Arduino do exist. You can even build one on a breadboard. The only disadvantage is you need to run the Atmega328P-PU at a lower speed. I run mine at 8MHz with an external crystal on an Adafruit prototyping Pi plate.
This makes the RP-Arduino serial communication a lot simpler.

I have one question though. The latest versions of Raspbian don’t use the /etc/initab file any more. How do I set up the serial gpio port?