OCFreaks!

Interfacing HC-SR04 Ultrasonic Distance Sensor with LPC2148

In this tutorial we will see how to interface an Ultrasonic distance sensor (HC-SR04) with ARM7 LPC2148 microcontroller. As the name suggests, the HC-SR04 Ultrasonic Distance/Ranging Sensor uses ultrasound to measure distance from a object ahead of the sensor. Ultrasound is a sound wave with frequency greater than the audible limit i.e. > 20Khz. HCSR-04 uses 40Khz ultrasound to measure distance between itself and any object ahead of it with a sensing range of 2 centimeters to 4 meters.

Pinout: The module has got 4 pins viz. VCC(+5V), TRIG, ECHO, GND.

Working principle

Ultrasonic Distance/Ranging Sensors are based on similar working principle to what is used in SONAR. It has got two transducers, one for transmitting ultrasound and second one for receiving the echo. Based on the time it takes for echo to arrive we can compute the distance, since we already know the speed of sound in air which is around 343 m/s or 1235 km/h (at 20°C).

Interfacing Steps:

  1. To start distance measurement a short pulse of 10us is applied to Trigger pin.
  2. After this the HC-SR04 Module will send a burst of 8 ultrasonic pulses at 40Khz.
  3. It will then output a HIGH for the amount to time taken for the sound waves to reach back.
  4. The pulse HIGH time is measured and then used to calculate the distance.

Distance calculations:

We know speed of sound in air,

Vs = 343 m/s = 0.0343 cm/us

We also know the time it took to sound waves to emit and echo back, lets call it T. Now, by using basic distance formula we can find the distance as:

Distance Traveled = Speed x Time taken
DT = 343 m/s x T seconds

Now, since we will be measuring ECHO ON-Time in microseconds and also to get distance in centimeters we can change the units as follows:

DT in cm = 0.0343 cm/us x T us

After this we divide the computed value by 2 since the waves have traveled double distance.

D =
DT/2

=

0.0343 x T/2

cm

HC-SR04 Ultrasonic sensor Interfacing with ARM7 LPC2148 Example

Now, lets do a programming exercise. For the interfacing example given below, P0.2 is configured as output and connected to TRIG pin and P0.3 is configured as input and connected to ECHO pin of the Ultrasonic Distance sensor. Timer0 module is used for generating delays with 1 us resolution. It is also used to measure time for ECHO pulse using two simple functions viz. startTimer0() & stopTimer0(). You can check the project source code linked below for implementation details. The distance data is sent to Terminal via UART0. You can refer my LPC2148 UART Tutorial for more. The example source code also contains retargeted printf() for KEIL which redirects its output to UART0.

The HC-SR04 Ultrasonic module operates on 5Volts and hence the output HIGH on ECHO pin is also 5V. We can directly interface this on any of the GPIO pin which has a 5V tolerant pad. But its better to use a voltage divider (using 2KΩ and 1KΩ resistors) to get input from ECHO pin for additional safety. Note that we don’t need to translate 3.3V to 5V for TRIG pin since 3.3V is already a HIGH for TTL compatible input pins.

Schematic:

Source Code:


/*(C) Umang Gajera- www.ocfreaks.com
Interfacing HC-SR04 Ultrasonic Distance/Ranging sensor with LPC2148 - Example Source Code for KEIL ARM.
More Embedded tutorials @ www.ocfreaks.com/cat/embedded/
License: GPL.*/

#include <lpc214x.h>
#include <stdio.h> //visit http://www.ocfreaks.com/retarget-redirect-printf-scanf-uart-keil/ 
#include "lib_funcs.h" //OCFreaks LPC214x Tutorials Library Header

#define TRIG (1<<2) //P0.2
#define ECHO (1<<3) //P0.3

int main(void)
{
	initClocks(); //Set PCLK = CCLK = 60Mhz - used by: UART, Timer and ADC
	initUART0();  //Initialize UART0 for retargeted printf()
	initTimer0(); //Init Timer for delay functions
	int echoTime=0;
	float distance=0;

	IO0DIR |= TRIG;    //Set P0.2(TRIG) as output
	IO0DIR &= ~(ECHO); //Set P0.3(ECHO) as input (explicitly)
	IO0CLR |= TRIG;    //Set P0.2 LOW initially

	printf("OCFreaks.com LPC214x HC-SR04 Sensor Interfacing Tutorial\n");

	while(1)
	{
		//Output 10us HIGH on TRIG pin
		IO0SET |= TRIG;
		delayUS(10);
		IO0CLR |= TRIG;

		while(!(IO0PIN & ECHO)); //Wait for a HIGH on ECHO pin
		startTimer0(); //Start counting
		while(IO0PIN & ECHO); //Wait for a LOW on ECHO pin
		echoTime = stopTimer0(); //Stop counting and save value(us) in echoTime

		distance = (0.0343 * echoTime)/2; //Find the distance

		printf("Distance = %0.2fcm\n",distance);
		
		delayMS(1000); //wait 1 second for next update
	}
	
	//return 0; //This won't execute normally
}

Screenshot:

Download Project:

KEIL ARM uV5/uV4 Project for example given above is on GitHub @ HCSR04 Ultrasonic Distance Sensor Interfacing with LPC2148 [Successfully tested on Keil uV5.23], Download Project Zip. You can find the HEX file inside objects folder.