Ultrasonic distance measurement with RL78 and HC-SR04 module

Hello folks! Today I would like to show you a simple project for ultrasonic distance measurement using the RL78 and a HC-SR04 ultrasonic ranging module.

The sensor module itself is a small PCB with two ultrasonic transducers (one for transmitting and one for receiving) along with some circuitry for signal generation and reception. The sensor module is pictured below.

HC-SR04 ultrasonic module

It can be easily purchased at any electronics store and online at DealExtreme, eBay, WayEngineer, Goodluckbuy, etc.

Its usage is fairly simple: all you have to do it is to supply its power supply pins (VCC and GND pins) with 5V and then trigger a measurement by applying a pulse (minimum period is 10µs) on Trig pin. The sensor will transmit 5 ultrasonic pulses and when the echo is received the ECHO pin outputs a pulse proportional to the time elapsed between the pulse trigger and the echo receiving.

One can easily calculate the distance between the sensor and the target by using the following equation (written in Portuguese, pulso_retorno = ECHO output pulse width)


Note that the equation considers a constant value for the sound speed. In real life applications that speed varies with temperature, so it is better account for it too.

So, if the measured pulse has a 1000µs width, the target is located about 0,17m far from the sensor. In order to get the distance in centimeters one could use the following equation:


Please note that, for calculation purposes, the measured pulse should always use 1µs time base! The code listing is shown below. The TRIG pin must be wired to RL78/G13’s P4.1 pin and ECHO must me wired to RL78/G13’s P4.2 pin (TI04 input). The application was tested on the RL78/G13 RSK board.

RL78 RSK with the ultrasonic module mounted

4 thoughts on “Ultrasonic distance measurement with RL78 and HC-SR04 module

  1. Olá Fábio! Tudo bem?
    Estou começando a trabalhar com MSP430, e estou montando um medidor de distancias, usando um MSP430G2553, um LCD 16×2 e um sensor ultrassônico HC-SR04.

    Não tenho dificuldades em trabalhar com o display, mas estou encontrando algumas dificuldades com o sensor ultrassônico. Gostaria de saber se você tem exemplos com MSP430G2553 e HC-SR04 ou tutoriais com aplicações semelhantes.

    De antemão agradeço.


    1. Olá Vinicius,

      Desculpe a demora, infelizmente não tenho este programa portado para outras plataformas, mas não é difícil de fazer isso no MSP430, basta capturar o pulso com um canal do timer.



Leave a Reply

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

4 × three =