Interfacing an ultrasonic sensor with NUCLEO board

For academic purpose I have to interface the F401RE NUCLEO board with the HY-SRF05 ultrasonic sensor, it uses the sonar principle to allow to measure the distance to an obstacle.

For simplicity I used the SRF05 library that provides all the methods that I needed to use the SRF05 sensor.
The sensor is composed by 5 pins:


  • Vcc
  • Trigger
  • Echo
  • OUT
  • GND

And here the scheme to connect the sensor with the NUCLEO board:

  • Vcc –> 3.3v
  • Trigger –> D4 (PB_5)
  • Echo –> D7 (PA_8)
  • OUT –> NULL
  • GND –> GND

You can find the NUCLEO-F401RE’s pins scheme here and here the code:

#include "mbed.h"
#include "SRF05.h"

int main() {

  while(1) {
    SRF05 srf(PB_5, PA_8); // Trigger, Echo
    printf("Distance = %.1f\r\n",;

  return 0;

Follow the previous instructions to compile it and to import the libraries and after that put the bin on the NUCLEO.

Once connected on the screen appears an output like this:

Distance = 125
Distance = 90
Distance = 80
Distance = 25
Distance = 13

Obviously the number after the equal sign is the distance detected by the sensor.