SRF02 ultrasonic sensor with STM32F4-Discovery

The SRF02 Ultrasonic range finder is a ultrasonic distance sensor, which is a transceiver with only one membrane. That’s practical, because the sensor is very small and so it is perfect for small sized hardware applications. It detects distances from 16 to 6000 cm. The Sensors needs a 5 V source, so it can be directly connected to many microcontroller. It communicates through serial interface or through i²c bus (also I2C bus called). The following article describes how to connect and use the sensor with a STM32F4 through I2C. The databus is very handy because it needs only two wires (SDA and SCL) for communication. SDA is the data wire and SCL is the clock. Components can be distinguished by a unique I2c address. In this example there are four SRF02 sensors on one bus. The configuration of the address will be described too. The I2C master is a ARM based STM32F4-Discovery (STM32F407VG) controller.

SRF02 front view

SRF02 back view

SRF02 side view

SRF02 Basics

The SRF02 measures distances with ultrasonic signals. Therefore the sensor sends a short echo impulse through the membrane and afterwards it detects the reflection of the signal. The sensor also measures the time. The speed of sonic is constant, so the distance can be calculated with the time between sending the signal and detecting the reflection. This procedure is called time-of-flight.

The SRF02 must not be calibrated, this was already done by the manufacturer. The sensor provides three different units which can be configured. The following table shows the configurations for the different configuration:


address function
0x50 measured data in inch
0x51 measured data in cm
0x52 measured data in microseconds
0x56 fake range mode (inch)
0x57 fake range mode (cm)
0x58 fake range mode (microseconds)
0xA0 1. sequence to set the I2C address
0xA5 2. sequence to set the I2C address
0xAA 3. sequence to set the I2C address

Setting an unique I2C address

The addresses of the sensors are transferred as hexadecimal bytes. Initially, every sensor has the same address 0xE0. If there is only one sensor on the bus, there is no need to change the address, but if there are a few of these sensors, the address has to be changed to an unique one. The new address has to be transferred to the command register of the sensor (0x00). To change the address, the sensor has to get into a mode, where it receives and saves the new address. So, there are three control bytes (1. – 3 sequence) which has to be transmitted. The new address will stay saved on the sensor, even if it’s switched off.

The sensor shows it’s address while starting. There is a LED, which flashes the saved address with short impulses. When the sensor is switched on, is flashes the LED once (long impulse). After this, the sensor flashes the address as short impulses. For example, with the address 0xE0 the sensor will not give short impulses, but with the address 0xF0 it will flash eight times. The mapping of the addresses and the signals can be found in the following table:

address flashes
0xE0 0
0xE2 1
0xE4 2
0xE6 3
0xE8 4
0xEA 5
0xEC 6
0xEE 7
0xF0 8
0xF2 9
0xF4 10
0xF6 11
0xF8 12
0xFA 13
0xFC 14
0xFE 15

In the repository (project: SRF02_example) there is an implementation to change the sensor address. Because of the fact that the all of the SRF02 sensors have the same initial address, it is necessary to change the address successively. The address change can be done with the function setSensorI2CAddress(). The function requires only the current and the new I2C address. An example implementation looks like this:

Connect the sensor with STM32F4

The example code in the repository shows how to configure and connect the sensor with the STM32F4 controller. It can be found in the directory SRF02_example. The project uses four sensors in total. First the I2C bus is initialized with the function initSRF02(). After this, every sensor gets initialized by a call of initUltrasonicSensorI2C() which expect the sensor’s I2C address. This function sets the measured unit additionally. Then the distance can be measured by the readDistance() function. The following code snippet is an example implementation to read the distance by one sensor:


supplier: exp-tech

About Marco Scholtyssek

Student of technical computer science at FH Dortmund, works as software developer at itemis AG in Lünen.
This entry was posted in ARM, Eingebettete Systeme, embedded systems, English, microcontroller, Sensors, STM32F4. Bookmark the permalink.

2 Responses to SRF02 ultrasonic sensor with STM32F4-Discovery

  1. Axel says:

    Guten Tag,
    ich habe nur einen SRF02, der ich mit einem STM32F4-discovery verbinden möchte.
    Wie kann ich die gemessene entfernung auf Computer bekommen?
    Können Sie mir vielleicht hefen bei einem programm code für CoIDE?

    • Hallo Axel,

      Sie können den gemessenen Wert auf mehreren Wegen zu Ihrem Rechner übertragen. Ich denke, dass es am einfachsten wäre, wenn Sie es über eine serielle Leitung, einen sogenannten USART machen würden.
      Ich habe dafür leider keinen fertigen Quellcode für Sie, allerdings gibt es für die Verwendung des USARTs mit dem STM32F4 wirklich viele Beispiele im Internet. Ich hoffe, dass Sie mit diesem Hinweis weiterkommen.

      Viele Grüße
      Marco Scholtyssek

Leave a Reply

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