Raspberry Pi and Arduino Connected Over Serial GPIO

arduino-raspberry-pi-serial-gpio-connect-schematics

One way to connect the Raspberry Pi and Arduino is by connecting the GPIO on the Raspberry Pi and the Serial Pins on the Arduino.

Because there is a voltage difference between the two device on these interface, a voltage divider or logic level converter would be required.

Check my article about connecting the two using I2C if you haven’t already seen it. Before we start, we need to set up the Raspberry Pi so it’s ready for serial communication.

Raspberry Pi Serial GPIO Configuration

0. if you have not seen my article on how to remote access your Raspberry Pi, take a look here:

http://blog.oscarliang.net/setup-raspberry-pi-for-remote-access/

1. In order to use the Raspberry Pi’s serial port, we need to disable getty (the program that displays login screen) by find this line in file /etc/inittab

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

And comment it out by adding # in front of it

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

2. To prevents the Raspberry Pi from sending out data to the serial ports when it boots, go to file /boot/cmdline.txt and find the line and remove it

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

3. reboot the Raspberry Pi using this command: sudo reboot

4. Now, Install minicom

sudo apt-get install minicom

And that’s the end of the software configuration.

Connect Serial Pins and GPIO with a Voltage Level Converter

Load this program on your Arduino first:

byte number = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  if (Serial.available())  {
    number = Serial.read();
    Serial.print("character recieved: ");
    Serial.println(number, DEC);
  }
}

Then connect your Arduino, Raspberry Pi and Logic Level Converter like this:arduino-raspberry-pi-serial-gpio-connect-schematics

This is how the wires are connected.

arduino-raspberry-pi-serial-gpio-connect-wiring

And this is the GPIO pins on the Raspberry Pi. Make sure you connect the correct pin otherwise you might damage your Pi.

arduino-raspberry-pi-serial-gpio

A Simple Example with Minicom

Now to connect to the Arduino via serial port using this command in putty or terminal

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

When you type a character into the console, it will received by the Arduino, and it will send the corresponding ASCII code back. Check here for ASCII Table. And there it is, the Raspberry Pi is talking to the Arduino over GPIO serial port.

arduino-raspberry-pi-serial-gpio-connect-minicom

To exit, press CTRL + A release then press Q

Example with Python Program

Using Python programming language, you can make Raspberry Pi do many fascinating stuff with the Arduino when they are connected. Install Py-Serial first:

sudo apt-get install python-serial

Here’s a simple application that sends the string ‘testing’ over the GPIO serial interface


import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.open()

ser.write("testing")
try:
        while 1:
                response = ser.readline()
                print response
except KeyboardInterrupt:
        ser.close()

Raspberry Pi and Arduino Connected Over Serial GPIO python-serial-program-output

To exit, press CTRL + C

Connect Raspberry Pi and Arduino with a Voltage Divider

Apart from replacing the Login Level Converter with a voltage divider, the way it works is the same as above. Anyway, I will show you a different example to demonstrate this. A voltage divider is basically just two resistors.

There is something you should be aware of before we continue. The RX pin on the Arduino is held at 5 Volts even when it is not initialized. The reason could be that the Arduino is flashed from the Arduino IDE through these pins when you program it, and there are weak external pull-ups to keep the lines to 5 Volts at other times. So this method might be risky. I recommend using a proper level converter, if you insist on doing it this way, try adding a resistor in series to the RX pin, and never connect the Raspberry Pi to Arduino RX pin before you flash the program to Arduino, otherwise you may end up with a damaged Pi!

arduino-raspberry-pi-serial-connect-schematics

The Arduino serial pin is held at 5 volts and Raspberry Pi’s at 3.3 volts. Therefore a voltage divider would be required, it’s basically just two resistors.

arduino-raspberry-pi-serial-connect-schematics

Here is the program you need to write to the Arduino board.


void setup() {
   Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0) {
        int incoming = Serial.read();
        Serial.print("character recieved: ")
        Serial.print(incoming, DEC);
    }
}

Now you can connect directly from your computer to the Raspberry Pi on the tty-device of the Arduino, just like we described above. (type below into your putty)

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

And as you type in characters in the console, you should see something like this:

arduino-raspberry-pi-serial-gpio-connect-minicom

And that’s the end of this Raspberry Pi and Arduino article. There are other ways of connecting, if you don’t already know, you can also use a Micro USB cable. Check out this post: http://blog.oscarliang.net/connect-raspberry-pi-and-arduino-usb-cable/

Like us on Facebook if you haven't already.
Subscribe to our Channel for random videos :D
Don't have much on there, but follow me anyway :)
Donate any amount, to help us maintain this website.
Love Multicopters? Join our discussion Group on Facebook!

32 thoughts on “Raspberry Pi and Arduino Connected Over Serial GPIO

  1. Hi,

    we have one problem for about Rs232

    example I want give or take 1 data Pi to PC, I can send to PC no problem but I cant take Pc to Pi any data… if I send 30 times or 40 times after I can send PC to Pi… then it be normaly… but if I push caracter from keyboard after lock serial com… after I send again 30 or 40 times data then open again… after normaly…

    I tried resister then I use max3232 but no change… only I have problem for Pi to PC send wire…

    example some example I have from internet “Pi serial com. to ardiuno” but this not work…

    I will wait your helps …

    thanks a lot…

    • Hi Miralay, sorry I don’t think I can help with your problem, because I don’t own a RS232 so I can’t test it and I have never used that type of cable before either.

      have you tried google that problem? hope you can find an answer.

  2. […] login automático quando o raspberry liga, entra no terminal e é precisa digitar o comando startx para iniciar a interface gráfica. aqui um passo a passo com instruções para iniciar o sistema automaticamente: http://raspisimon.no-ip.org/rpi_autologin.php automação solução de automação que integra Raspberry PI, Arduino, Node.js, MongoDB, HTML5 e Websockets http://ni-c.github.io/heimcontrol.js/ raspberry pi + arduino  tutorial bem explicado de como conectar os Arduino e Rapsberry através da GPIO http://blog.oscarliang.net/raspberry-pi-and-arduino-connected-serial-gpio/ […]

  3. This is very interesting, You’re a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your great post.

    Also, I’ve shared your web site in my social networks!

  4. An impressive share! I’ve just forwarded this onto a coworker who has been conducting a little homework on this.
    And he actually ordered me lunch simply because I found it for him…

    lol. So allow me to reword this…. Thanks for the
    meal!! But yeah, thanks for spending time to talk about this subject
    here on your blog.

  5. Wonderful beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog web site?

    The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright
    clear concept

  6. Hello,

    The caution note at the top of the article says the Arduino RX pin is held at 5V,
    yet the pictorial diagram show the voltage divider on the Arduino TX pin.

    Regards,

    Bill Thomson

    • No. the RX is only held at 5V when it’s not initialized and that could be a problem. (but I haven’t had any issue because of this so far)
      And you only need to use voltage divider on the Arduino TX as the voltage could changes between 0V and 5V on this pin connection.
      The Arduino RX voltage should be safe for the PI connected directly, because only the PI is driving the voltage on this connection (0V to 3.3V).

  7. I have mine hooked up in a similar way, but it isn’t working for me. My multimeter shows that the high level on the logic board is at 5v, and the low level is at 3.3v. The TX from the Pi goes in the A3 pin on the logic converter, and the B3 pin then connects to the RX pin on the Arduino.

    I’m curious… when using the logic converter, does it matter where the 3.3v and 5v come from? The only difference with my setup is that the Arduino 5v output is also powering the Pi.

    So yeah, I’m not sure why mine isn’t working. Reading the serial RX pin on the Arduino always shows a value of -1.

    • you should use seperate external 5V to power the PI. have you tried using potential divider? just to rule out the possibility that your logic converter is bad.

  8. Hi Oscar!

    It seems you are using a BSS138 vor level shifting. I’m currently trying to send and receive data to a multiwii which is Arduine mega 2560 based. My Baud rate is 115200, is that too fast for that shifter? Do I need to use 9600?

    Thanks for any comments on this as I can’t proceed currently.

  9. We are a bunch of volunteers and opening a new scheme
    in our community. Your web site offered us with useful info to work on. You have performed an impressive activity and our whole community will likely be grateful to you.

  10. I connect arduino Mega serial pins and raspberry type B GPIO with a adafruit voltage level converter
    I copied and pasted your code (python for Raspberry). Unfortunately when I type 103 in the arduino serial monitor, the raspbian terminal displays:
    character recieved: 1
    character recieved: 0
    character recieved: 3

    and like that for all numbers (111, 100, 32 …). I can not see all numbers, but one by one …. It is as python can not handle the characters ‘\ n’ and ‘\ r’ even if I remove the code with
    if response == ‘\ n’ or response ==” or response == ‘\ r’

    thank you for your site he taught me a lot.

  11. First congratulations by your work that was very usefull.

    I have this problem:
    I want communicate arduino and raspberry using xbee

    I’ve tryed use this code:

    import serial
    ser = serial.Serial(‘/dev/ttyAMA0′, 9600)
    ser.open()
    string = “Hello from Raspberry pi”
    print(‘Sending “%s” ‘ %string)
    ser.write(‘%s’ %string)

    while True:
    incoming = ser.readline().strip()
    print(‘Received %s’ %incoming)
    ser.write(‘RPi RECEIVED: %S’ %incoming)

    And this error appears:
    Import error: No module named serial.

    I’ve tested and the minicom works. I see the information sent from arduíno, but just using minicom because the code doesn’t work. Can you help about this problem?

    Thank you and one more time congratulations

  12. Thanks for the info. I know this is fairly old but when connecting to an Arduino Nano, I had to swap Tx and Rx between the arduino and the LLC so that it goes Tx to Tx and Rx to Rx.

    I was beating my head against the wall trying different things and finally said screw it and swapped them. No sparks, no smoke, no flames… just a happy RPi that is casually talking to the Nano… thanks for the post. Great info!

Leave a Reply

Your email address will not be published. Required fields are marked *


− 1 = zero

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>