Ultrasound Ranging Sensor module

Dependents:   Obstacle_avoidance servourfmatlab hcsr04 DistanceOnSevenSegLed ... more

Overview

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect).

Sensor (HCSR04) Details

You can find the details of the sensor at http://www.micropik.com/PDF/HCSR04.pdf /media/uploads/prabhuvd/_scaled_hcsr04.png

Working principle

The trigger input is sent a 10 usec pulse and followed by that the pulse width on the echo line is measured , the measured pulse width is then converted into distance in cm.

/media/uploads/prabhuvd/waveform.png

Quote:

Calculation : The sonic speed is 343 m/sec , when converted to cm/usec , it will 343 * 100/ 1000000 = 343 /10000 cm/usec The width of the pulse is equivalent to time taken by ultrasonic wave to reach obstacle and bounce back , hence the distance of the object will be half the pulse width.

Distance (cm) = measured pulse width * 343/20000 cm.

Schematic

/media/uploads/prabhuvd/schema.png

Code

include the mbed library with this snippet

#include "mbed.h"
#include "hcsr04.h"
#include "TextLCD.h"

DigitalOut myled(LED1);
HCSR04  usensor(p25,p6);
TextLCD lcd(p14, p16, p17, p18, p19, p20,TextLCD::LCD16x2); // rs, e, d4-d7
unsigned int dist;
int main()
{
 
    while(1) {
        usensor.start();
        wait_ms(500); 
        dist=usensor.get_dist_cm();
        lcd.cls();
        lcd.locate(0,0);
        lcd.printf("cm:%ld",dist );
 
    }
}

History

name modified default tip

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:55:03 +0000] rev 7

name modified


Changed the class name from DistMeasure to HCSR04

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:52:13 +0000] rev 6

Changed the class name from DistMeasure to HCSR04


Add a sample code

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:50:32 +0000] rev 5

Add a sample code


Updated comments

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:46:01 +0000] rev 4

Updated comments


changed the method name start_measurement to start

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:43:21 +0000] rev 3

changed the method name start_measurement to start


Updated libray

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 18:41:34 +0000] rev 2

Updated libray


version 1.1 interrupt routines associate with echo pin

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 17:32:04 +0000] rev 1

version 1.1 interrupt routines associate with echo pin


Version 1.0 , the rise and fall methods needs to be attached to pin;

2013-03-30, by prabhuvd [Sat, 30 Mar 2013 17:27:39 +0000] rev 0

Version 1.0 , the rise and fall methods needs to be attached to pin;