I was wondering if its possible to communicate between Arduino ann RPi using Serial Communication in C/C++, it's possible with python but didn't find a suitable library for C/C++ ?

  • Also note that the Serial library that comes with the Arduino takes care of the hardware implementation. what type of communication do you need? Text or binary?
    – user29510
    Dec 18, 2016 at 23:03

3 Answers 3


Please be aware that the Pi UART is 3V3 whereas most Arduinos are 5V. So take precautions if you connect directly to the Pi's UART (e.g. level changer from Arduino 5V TX to Pi's 3V3 RX).

In C/C++ you would typically use the raw device.

So if you were connecting the Pi UART to an Arduino (Pi UART to Arduino UART, or Pi UART to Arduino USB) open /dev/ttyAMA0.

If you were using a USB serial dongle at the Pi end then open /dev/ttyUSBx or /dev/ttyACM0, depending on the dongle.

There are plenty of Linux serial tutorials on the www.

The wiringPi library has wrappers for serial links, as does my pigpio library. I would not expect a C/C++ programmer to need the wrappers though.


Here's sample code I'm using to write messages from a pi to an Arduino-compatible teensy board (which is driving an LCD display unit). You can replace the code that says LINE1=... and LINE2=... with whatever you want to feed into the serial buffer.

echo "TEENSY:         " $TEENSY
echo "......"

LINE1="$($UTIL/GetProperty line1 $QUERY_STRING)"
LINE2="$($UTIL/GetProperty line2 $QUERY_STRING)"

echo "line1 = " $LINE1
echo "line2 = " $LINE2

echo -n $LINE1 > $TEENSY
sleep 0.05
echo -n $LINE2 > $TEENSY

On the Arduino side, I've created a reusable class for reading the serial port that looks like this:

// SerialReader.h

#define BUFFER_SIZE 256

#include "WProgram.h"

class SerialReader

    char *read(char *buffer, int bufferSize);




// SerialReader.cpp
#include "SerialReader.h"
#include "WProgram.h"

char *SerialReader::read(char *buffer, int maxLength)
    int i = 0;
    int bufferIndex = 0;

    if (Serial.available() > 0)
        // Read as long as there is data to be read
        while (i>=0 && bufferIndex < (maxLength-1))
            i = Serial.read();
            if ( i >= 0 ) buffer[bufferIndex++] = (char)i;
        buffer[bufferIndex] = 0; // Null-terminate
    return buffer;

Then you just have to invoke the method above on Teensy with a byte array to fill with data:

#define BUFFER_LENGTH 40
char serialBuffer[BUFFER_LENGTH];
serialReader.read(serialBuffer, BUFFER_LENGTH);

Hope that helps. I'm using this code to post messages on a LCD display board via a web service from my phone ( iPhone Safari --> Pi Web Server (OliWeb) --> cgi script --> serial write --> teensy serial read --> lcd display). The pi is connected directly to the Teensy board via USB cable.


As @joan states wiringpi has a wiringSerial library which includes a number of functions (which are easier than using fcntl). E.g. it provides serialOpen etc. Unfortunately no serialGets although I have written my own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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