Connecting Microcontrollers: Direct Serial Connection

In complex projects, you will need to connect multiple microcontrollers with each other. This connection can be made with different protocols and libraries. This article explores available options and presents easy to follow code examples.

Specifically, you will learn how to connect an Arduino Uno to a Raspberry Pi using a USB connection.

This article originally appeared at my blog


We start with a simple example. On starting, the Arduino will open a serial connection and blink an LED to show that it is alive.

#include <Arduino.h>#define LED 4void setup()
pinMode(LED, OUTPUT);
void blink() {
digitalWrite(LED, HIGH);
digitalWrite(LED, LOW);
void loop()

Then, inside the loop() function, we will use the Serial object to check if some data has been provided. If yes, the data will be read byte by byte, and printed to the serial console as well.

void checkAndProcessSerialData() {
if (Serial.available()) {
char ch =;
if (ch > 0) {
// ...void loop()

The Arduino Source Code is finished — now let’s take a look the Raspberry Pi.

Raspberry Pi

The code that runs on the Raspberry Pi needs to listen for incoming data over the serial connection. Several libraries exist, we will choose the Python library pyserial.

First of all, we install the library with the pip3 tool.

> pip3 install pyserialLooking in indexes:,
Collecting pyserial
Downloading (90kB)
100% |████████████████████████████████| 92kB 1.3MB/s
Installing collected packages: pyserial
Successfully installed pyserial-3.5

Using this library, we create a Python script — see the following code listing.

from serial import Serial
from time import sleep
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=1)while True:

Let’s go through this script with mire detail:

  • Line 1 & 2: Import the required libraries, the just now installed serial and the core library time
  • Line 4: Using the imports Serial class, we create an instance that connects to the attached Arduino - check whether its /dev/ttyACM0 or /dev/ttyUSB0 - sets the baud rate, and a timeout value to control when the connection should be recreated again.
  • Line 6: Similar to an Arduino code, we start a loop that never terminates…
  • Line 7: We send the message “Hello” to the Serial connection and encode it so that the characters are send as bytes
  • Line 8: We read from the serial connection and print it to the console
  • Line 9: Wait for 2 seconds.

From the console, we can simply start the program and then see the output:


Reading Serial Data Correctly

However, the output shows a flaw: It prints the byte representation instead of the strings. Therefore, we need to typecast the bytes to a string, and also remove carriage returns and newline symbols. Change the print statement to this line:

print(str(arduino.readline(), 'utf-8').rstrip('\r\n'))

And now the output is:



In this short article, I showed how to connect an Arduino to a Raspberry Pi via USB, and read data via USB-To-Serial connection. The Python3 library pyserial is very easy to setup and use. This is just the first method to exchange information between two microcontrollers. In the next article, we will explore how to setup the connection by using the I2C protocol.




IT Project Manager & Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium


Can developer productivity be measured?

Powerful Python one-liners to look like a pro

Will write the security module again? Oh no, oh no, oh no no no… (Part 1)

OSPF (Open Short Path First) Routing Protocol implemented using Dijkstra Algorithm

Algorithmic Trading Simulations Part I

Catches in Caching dynamic pages

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


IT Project Manager & Developer

More from Medium

How ROS Works On a Physical Robot

RADU: How to build a Robot with Arduino and Raspberry Pi

Core Embedded Systems Skill: Bitwise Operation

ESP32 101 : Input and Output