How to connect Arduino and Raspberry Pi using USB and C++

Recently, I wanted to connect my Fuelino Proto3 to the Raspberry Pi 3 (with Raspbian Jessie Lite) which I bought some weeks ago. The Pi has a very interesting feature: same as for a normal PC, it has USB ports which can be used to both communicate and power any device.

In order to connect the Fuelino, which is equipped with an Arduino Nano, to the Raspberry, I decided to use the native USB port of the Pi. Doing so, the only thing that you need to have is a Mini USB cable, like the one shown in the picture below.

In order to understand which Serial Port is reserved for the Arduino, and to understand which is the name of the USB device connected (USB to serial converter), you need to launch the following commands, before and after connecting Arduino to the Raspberry USB port.

cd /dev

After connecting Arduino Nano, and typing the same commands, the info changes as below.

By evaluating the "delta" between the 2 pictures above, it is possible to understand that there are 2 more "dev" devices: "serial" and "ttyUSB0". In addition to that, the "lsusb" command returns an additional device:

Bus 001 Device 004: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter

In fact, I am using a cheap "made in China" Arduino Nano (price: 2 euro/piece) bought on AliExpress, which equips the USB-to-serial converter chip called HL-340 (CH-340). The drivers for Windows can be easily found in Internet by searching "CH-340 drivers"; on Raspbian Jessie Lite (January 2017), I did not need to install any driver. On opposite, the original Arduino Nano use a more expensive FTDI chip, as far as I know.

In order to test the Serial communication with Arduino, at first I created a minimal and simple sketch using Arduino IDE, and flashed it on the Arduino Nano. The sketch at first initializes the Serial port: the speed is 57600 baud, and the communication is done, as default, in "8N1" format, which means 8 data bits, no parity, 1 stop bit. Then, when the loop starts, Arduino sends back every character received by Serial (like an "echo").

Before connecting Arduino to the Pi, I tested the sketch using Arduino IDE's "Serial Monitor". Arduino writes back (as an echo) every sentence that you send it.

The next step to perform is to create a C/C++ program which allows to communicate, using UART Serial protocol, between Arduino Nano and Pi. Searching on Google, I found this interesting library, made by a guy called Teunis van Beelen, which is very light and user friendly. I created the following sample code, to be compiled and run on Raspberry side. The communication is established at 57600 bit/s and transmission mode is 8N1. The Serial port used for communicating is, in this case, the port number 16 ("ttyUSB0" corresponds to port 16, as written here).

The source code is available here: Serial_Raspberry_Arduino_20170219s. The ZIP file should be copied and extracted in the "/home/pi" folder. You can use Samba to easily copy the files on the Pi using your Windows PC connected via Ethernet (guide here: samba_raspberry_pi_3_jessie). Then, run the following commands.

cd /home/pi/Serial
gcc arduino_test.c rs232.c -Wall -Wextra -o2 -o arduino_test

The commands above have the purpose to compile the source file written in C language and launch it ("gcc" is the standard C compiler, it is already installed in Raspbian Linux distribution). Once the program starts, Raspberry Pi keeps sending messages to the Arduino. After sending a message, Pi waits for 1 seconds and then checks if something has been received from Arduino; in positive case, the received message is shown on the display.

After 5 seconds, a new message is sent. You can stop the execution of the program by pressing "CTRL+C".

Author: Davide Cavaliere

I am an Italian Electrical Engineer graduated at Politecnico di Milano. My interests are motorcycles and cars, electronics, programming, Internet of Things, and Japanese culture.

6 thoughts on “How to connect Arduino and Raspberry Pi using USB and C++”

  1. Hello.

    On de cPut function, it only pasess few charactares. like 10. after that nothing is sent.
    Any idea why?

    1. Hello Carlos,

      Probably it is too late, but I had the same issue... I solved it by putting usleep(10) (argument could be lower I did not test it) in cPut:
      void RS232_cputs(int comport_number, const char *text) /* sends a string to serial port */
      while(*text != 0)
      RS232_SendByte(comport_number, *(text++));

      Don't know the reason of the bug, but it looks this eliminates it.

    2. 10us was not enough in certain cases, 100us works perfectly so far. Applied baud rate: 115200, settings: 8N1 no parity.

  2. Is it possible to plug the usb cable to the PI (via hub), with the TTL side going to another device?

    For example, how would you setup the pi to interrupt whenever data is sent by the other device? All the examples I've seen (C programs based on the WiringPi library) don't seem to use interrupts!

  3. Thanks for sharing!

    Now I am using this technique to convert an old radio into a synth with an Arduino and a Raspberry Pi! 🙂

Leave a Reply

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