Ultrasound Ranging Sensor module

Dependents:   Obstacle_avoidance servourfmatlab hcsr04 DistanceOnSevenSegLed ... more

You are viewing an older revision! See the latest version

Homepage

Table of Contents

  1. Overview
  2. HCSR04
  3. Code

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).

HCSR04

You can find the details of the sensor at http://www.micropik.com/PDF/HCSR04.pdf /media/uploads/prabhuvd/hcsr04.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 );
 
    }
}


All wikipages