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 );
 
    }
}

Committer:
prabhuvd
Date:
Sat Mar 30 17:27:39 2013 +0000
Revision:
0:fb0929f37ebe
Child:
1:093521f56089
Version 1.0 , the rise and fall methods needs to be attached to pin;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
prabhuvd 0:fb0929f37ebe 1 /* Copyright (c) 2013 Prabhu Desai
prabhuvd 0:fb0929f37ebe 2 * pdtechworld@gmail.com
prabhuvd 0:fb0929f37ebe 3 *
prabhuvd 0:fb0929f37ebe 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
prabhuvd 0:fb0929f37ebe 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
prabhuvd 0:fb0929f37ebe 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
prabhuvd 0:fb0929f37ebe 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
prabhuvd 0:fb0929f37ebe 8 * furnished to do so, subject to the following conditions:
prabhuvd 0:fb0929f37ebe 9 *
prabhuvd 0:fb0929f37ebe 10 * The above copyright notice and this permission notice shall be included in all copies or
prabhuvd 0:fb0929f37ebe 11 * substantial portions of the Software.
prabhuvd 0:fb0929f37ebe 12 *
prabhuvd 0:fb0929f37ebe 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
prabhuvd 0:fb0929f37ebe 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
prabhuvd 0:fb0929f37ebe 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
prabhuvd 0:fb0929f37ebe 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
prabhuvd 0:fb0929f37ebe 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
prabhuvd 0:fb0929f37ebe 18 */
prabhuvd 0:fb0929f37ebe 19
prabhuvd 0:fb0929f37ebe 20
prabhuvd 0:fb0929f37ebe 21 #include "hcsr04.h"
prabhuvd 0:fb0929f37ebe 22
prabhuvd 0:fb0929f37ebe 23
prabhuvd 0:fb0929f37ebe 24 DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin,unsigned int maxtime):
prabhuvd 0:fb0929f37ebe 25 trigger(TrigPin), echo(EchoPin), timeout(maxtime)
prabhuvd 0:fb0929f37ebe 26 {
prabhuvd 0:fb0929f37ebe 27 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 28 pulsetime.reset();
prabhuvd 0:fb0929f37ebe 29 // this.rise(&DistMeasure::isr_rise);
prabhuvd 0:fb0929f37ebe 30 // this.fall(&DistMeasure::isr_fall);
prabhuvd 0:fb0929f37ebe 31 }
prabhuvd 0:fb0929f37ebe 32
prabhuvd 0:fb0929f37ebe 33
prabhuvd 0:fb0929f37ebe 34 DistMeasure::~DistMeasure()
prabhuvd 0:fb0929f37ebe 35 {
prabhuvd 0:fb0929f37ebe 36 }
prabhuvd 0:fb0929f37ebe 37
prabhuvd 0:fb0929f37ebe 38
prabhuvd 0:fb0929f37ebe 39 void DistMeasure::isr_rise(void)
prabhuvd 0:fb0929f37ebe 40 {
prabhuvd 0:fb0929f37ebe 41 pulsetime.start();
prabhuvd 0:fb0929f37ebe 42 }
prabhuvd 0:fb0929f37ebe 43
prabhuvd 0:fb0929f37ebe 44 void DistMeasure::isr_fall(void)
prabhuvd 0:fb0929f37ebe 45 {
prabhuvd 0:fb0929f37ebe 46 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 47 pulsedur = pulsetime.read_us();
prabhuvd 0:fb0929f37ebe 48 distance= (pulsedur*343)/20000;
prabhuvd 0:fb0929f37ebe 49 pulsetime.reset();
prabhuvd 0:fb0929f37ebe 50 }
prabhuvd 0:fb0929f37ebe 51 void DistMeasure::rise (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 52 {
prabhuvd 0:fb0929f37ebe 53 echo.rise(fptr);
prabhuvd 0:fb0929f37ebe 54 }
prabhuvd 0:fb0929f37ebe 55 void DistMeasure::fall (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 56 {
prabhuvd 0:fb0929f37ebe 57 echo.fall(fptr);
prabhuvd 0:fb0929f37ebe 58 }
prabhuvd 0:fb0929f37ebe 59
prabhuvd 0:fb0929f37ebe 60 unsigned int DistMeasure::get_distance_cm()
prabhuvd 0:fb0929f37ebe 61 {
prabhuvd 0:fb0929f37ebe 62 if(distance > 65530) {
prabhuvd 0:fb0929f37ebe 63 return -1;
prabhuvd 0:fb0929f37ebe 64 } else {
prabhuvd 0:fb0929f37ebe 65 return distance;
prabhuvd 0:fb0929f37ebe 66 }
prabhuvd 0:fb0929f37ebe 67 }