STM32 Nucleo Serial Communication

STM32 Nucleo

Debugging through the serial port is one of the ways to find errors in your code. In this tutorial, I'll show you how to add serial communication function to your STM32 Nucleo board.

Download the Driver

The first thing we'll need is the serial port driver. I thought this was already installed because we are already transferring .bin files to the board but this is not the case!

We will be using Putty to view the messages sent from the board to the computer so you need to download that as well.

Serial Object

For accessing the serial port of the STM32 Nucleo board, we'll be using the Serial object. This object takes in two parameters, the transmit pin and the receive pin. Since we'll be using the USB cable, those pins will be USBTX and USBRX, respectively.

So let's say the serial object is named pc, then this is how the object is initialized:

Serial pc(USBTX, USBRX);

Then to write a string to the port, the traditional printf() function is used:

pc.printf("Hello World!\n");

Simple Serial Example

I wrote a simple code that will display "Button is pressed!" everytime a button attached to D6 is pressed. Here it is:

#include "mbed.h" 

DigitalIn button(D6); 

Serial pc(USBTX, USBRX); 

int main() 
{ 
  while(1) 
  { 
    if(button)
    { 
      pc.printf("Button is pressed!\n"); 
    }
    else {;}  
  } 
}

Now we have a way to track our code and make debugging easier.