How to interface a Microcontroller with a Raspberry Pi through UART

/, Education/How to interface a Microcontroller with a Raspberry Pi through UART

How to interface a Microcontroller with a Raspberry Pi through UART

The main engine of the Modis Lock host module is a Raspberry Pi Compute Module 3.  The CM3 module also uses an ARM core microcontroller to facilitate reliable communication with its remote reader modules.  


Setting up an interface between a Raspberry Pi and a remote peripheral is pretty straight forward with the help of a few libraries such as RPi.GPIO and WiringPi.  The final protocol used to communicate with the peripheral will depend on the specific needs of individual scenario as well as the protocols supported by the peripheral.

Most basic peripherals support protocols like UART, I2C, CAN, SPI and USB.  Even wireless communication is possible now using WiFi and Bluetooth.


This guide will take a look at establishing a simple UART connection between a Raspberry Pi and an ARM core MCU using pyserial python library.  We will use a Raspberry Pi 3 Model B running the Raspberry Foundation recommended Raspbian OS and a NUCLEO-F401RE development board by ST.  This nucleo board has a STM32M401RE MCU.  We’ll use ARM’s mbed platform to speed up the development on the MCU.

Mis en Place (Things you will need before starting):

  1. One Raspberry Pi (3 Model B in our case.  Others are fine.) running Raspbian
  2. One NUCLEO-F401RE board
  3. One Mini USB cable to program and power the Nucleo board
  4. Three or more male-female jumper wires to connect Nucleo board with RPi 3
  5. One Micro USB cable and power supply for RPi3

Setting up the Nucleo Board using mbed

1.Connect the Nucleo board to your computer using a mini USB cable.

2. Sign up with ARM mbed developer account here if needed.

Log-in / Sign-up

3. Log in to mbed and select ‘Compiler’ to open up online IDE.


4. Select ‘NUCLEO-F401RE’ on the top right of the IDE.  and click ‘Select Platform’.


5. Select ‘New’ on the top right of the IDE, and ‘New Program.’


6. From the pop-up window double check the platform is selected as Nucleo-F401RE.  Then from the ‘Template option’, select ‘Display a message on PC using UART.’


7. Name your program.  The default is ‘Nucleo_printf’ in this case.  

8. Select ‘OK’ to generate a template.  There should be one file called main.cpp.


9. Change line 8 to read as ‘serial pc(PA_11, PA_12, 9600); // tx, rx’ so that we can use UART6 of the MCU for communicating with the RPi 3.


10. Open the main.cpp and click over the word ‘Serial’ on line 8.  Click ‘A’ that appears on the top right to show more information about the UART function.


11. Clicking on the icon that appears next ‘A’ will reveal even more information about UART implementation in mbed environment.


12. Select ‘Compile’ from the top menu or hit ‘Ctrl-D’.  It will save, compile the demo code and download the binary file to your computer.  Now we can program or ‘flash’ the Nucleo MCU with that binary file.


13. Open a file browser and go to the download folder.

14. Locate the downloaded binary file (Nucleo_printf_NUCLEO_F401RE.bin in this case) as well as the Nucleo board.  The Nucleo board should show up as a USB mounted storage device as if it was a USB thumbdrive.


15. Drag the binary file and drop it over the Nucleo board.

16. One of the LEDs on the Nucleo board will alternate between two colors while it’s ‘flashing’ the binary onto the MCU.  Once flashing is complete, our program should start working right away.  You should see another green LED (LD2) flash toggle every second as it’s instructed in the code (line 19).  

17. You can now disconnect the board from your PC for the moment.  The program will run as soon as it’s powered up next time.  Now we will set up our RPi 3 to receive the signal in the next part.

Setting up the Raspberry Pi 3

Now we will walk through the steps necessary to configure the Raspberry Pi to receive the UART signal from the Nucleo board.  Almost there!

1. Power up the RPi and edit /boot/config.txt and add ‘enable_uart=1 to the end of the file to enable UART on RPi 3.  Reboot.

2. We’ll use pyserial for UART.  run ‘python3 -m pip install pyserial’ on the command line to install pyserial.

3. Run ‘sudo usermod -a -G dialout pi’ to make sure we can use the RPi’s UART.

4. Create a file called ‘’ with ‘nano’ and add the following lines;

import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)

while True:
    rcv = port.readline()

5. This is a very important step.  Carefully connect the jumper wires as follows.   Take time and double check your work afterwards.  UART connections are always ‘crossed’ so that one’s transmission becomes the other’s receptions.

RPi Pin 10 -> Nucleo PA7 : UART RX -> UART TX

RPi Pin 8 -> Nucleo PA6 : UART TX -> UART RX

RPi Pin 6 -> Nucleo PA5 : Ground -> Ground

Here’s a picture of the RPi’s pinouts and the pinout for the Nucleo is here.

6. Power up the Nucleo board with the mini USB cable.  The LED2 should blink once every second, indicating our program from the part one is running.

7. Run the python program with ‘sudo python3’ and it should produce something like below every second;

'This program runs since 589 seconds.\n'
'This program runs since 590 seconds.\n'
'This program runs since 591 seconds.\n'
'This program runs since 592 seconds.\n'
'This program runs since 593 seconds.\n'
'This program runs since 594 seconds.\n'

8. Enjoy your M2M communication using UART!  If something doesn’t look right, double check your jumper cables first.  

9. Now what?  Feel free to try other libraries as well as other protocols.  WiringPi has a diverse set of protocols implemented for RPi for C, and its python interface is found here but that’s only the beginning!  Please let me know in the comments how it went!

By | 2018-05-17T20:33:37+00:00 23/07/2017|Categories: Coding, Education|Tags: , , , , , , |Comments Off on How to interface a Microcontroller with a Raspberry Pi through UART

About the Author:

Professional maker of things in mostly copper and binary numbers

Privacy Preference Center