Ultrasonic Sensor HC-SR04 Interfacing With ARM MBED

Overview of Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor Module

 

Ultrasonic Module HC-SR04 works on the principle of SONAR and RADAR system. It can be used to determine distance of an object in the range 2cm – 400cm.

The module has only 4 pins, Vcc, Gnd, Trig and Echo.

When a pulse of 10µsec or more is given to the Trig pin, 8 pulses of 40 kHz are generated. After this the Echo pin is made high by the control circuit in the module. Echo pin remains high till it gets echo signal of the transmitted pulses back.

The time for which the echo pin remains high, i.e. the width of the Echo pin gives the time taken for generated ultrasonic sound to travel to the object and back.

Using this time and the speed of sound in air, we can find the distance of the object using simple formula for distance using speed and time.

For more information about ultrasonic module HC-SR04 and how to use it, refer the topic Ultrasonic Module HC-SR04 in the sensors and modules section.

 

Example/Application

Liquid level detection using ultrasonic sensor module HC-SR04.

Here, it is assumed that the tank in which the liquid level is to be monitored has a fixed height.

The level of liquid in the tank is displayed on an LCD.

If the liquid level falls below a predefined height, a buzzer is turned on to alert the user and a message is displayed on the LCD asking the user to fill the tank.

If the liquid level rises beyond a predefined height, a buzzer is turned on to alert the user and a message is displayed on the LCD asking the user to stop filling the tank.

 

Connection Diagram of HC-SR04 Module with ARM MBED (LPC1768)

Interfacing Ultrasonic Sensor Module with ARM MBED

 

Measure Distance using HC-SR04 and LPC1768 ARM MBED

We are also using the Nucleo_UltrasonicHelloWorld by EJ Teb’s for ultrasonic sensors.

You can find information about the Nucleo_UltrasonicHelloWorld code and library download it from here.

 

HC-SR04 Ultrasonic Code for LPC1768 ARM MBED

#include "mbed.h"
#include "ultrasonic.h"

void dist(int distance)
{
    //put code here to happen when the distance is changed
    printf("Distance changed to %dmm\r\n", distance);
}

ultrasonic mu(p8, p9, .1, 1, &dist);    //Set the trigger pin to p8 and the echo pin to p9
                                        //have updates every .1 seconds and a timeout after 1
                                        //second, and call dist when the distance changes

int main()
{
    mu.startUpdates();//start mesuring the distance
    while(1)
    {
        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
                                //the class checks if dist needs to be called.   
	}
}

 

Output Window


Components Used

ARM mbed 1768 LPC1768 Board
ARM mbed 1768 Board
1
Ultrasonic Module HC-SR04
Ultrasonic module HC-SR04 is generally used for finding distance value and obstacle detection. It can operate in the range 2cm-400cm.
1

Downloads

Nucleo_UltrasonicHelloWorld_uvision5_lpc1768 Download
Ad