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

Revision:
0:fb0929f37ebe
Child:
2:0acb6ade091c
diff -r 000000000000 -r fb0929f37ebe hcsr04.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.h	Sat Mar 30 17:27:39 2013 +0000
@@ -0,0 +1,63 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * For more details on the sensor :
+ * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MBED_HCSR04_H
+#define MBED_HCSR04_H
+
+#include "mbed.h"
+
+/** Distance Measure Class(es)
+ */
+
+class DistMeasure
+{
+public:
+    /** Create a DistMeasure object connected to the specified pin
+    * @param pin i/o pin to connect to
+    */
+    DistMeasure(PinName TrigPin,PinName EchoPin,unsigned int maxtime);
+    ~DistMeasure();
+
+    /** Return the distance from obstacle in cm
+    * @param distance in cms and returns -1, in case of failure
+    */
+ 
+    unsigned int get_distance_cm(void);
+    void isr_rise(void);
+    void isr_fall(void);
+    void fall (void (*fptr)(void));
+    void rise (void (*fptr)(void));
+    
+
+
+private:
+
+    Timer pulsetime;
+    DigitalOut  trigger;
+    InterruptIn echo;
+    unsigned int timeout;
+    unsigned int pulsedur;
+    unsigned int distance;    
+};
+
+#endif
\ No newline at end of file