Welcome Guest [Login|Register]
Language
Currency
Shop by Category
Shopping Cart
Your shopping cart is empty.
0Items in cart:
0.0g / 0.00ozShipping Weight:
C$0.00Total:

Ultrasonic Distance Sensor HC-SR04 w/ Arduino Example

Location: /Miscellaneous

Ultrasonic Distance Sensor HC-SR04 w/ Arduino Example

Click image to zoom.

Shown images are ilustrative only. Please refer to manufacturer's website and/or available datasheet for accurate information.

QtyUnit PriceLot Price
1C$4.8950C$4.89
5C$3.4110C$17.05
20C$1.9910C$39.82
Stock:1 ships next workday
Canada ships daily from ON, USA 2x/wk from NY

Product Information
dipmicro Code DE4132
Added 2013-03-21
Manufacturer Generic
Manufacturer P/N HC-SR04
Stock Type New
Restockable? No
Shipping Weight 10g / 0.353oz
Ship Cost Canada CP O/S Letter C$4
Ship Cost USA USPS First Class $4
Ship Cost World Economy C$5.00
 
Technical Summary
Packaging bulk

Features

  • Power: 5V / approx 2mA
  • Trigger: 10us TTL HIGH pulse
  • Echo: TTL HIGH pulse
  • Range: 2cm - 450cm
  • Accuracy: 0.3cm

NOTE: the sensor is sensitive how it's powered up, should be powered together with your circuit or GND must be connected first.

How it Works

Upon receiving the trigger signal, sensor emits ultrasonic wave and drives ECHO output high. Sound wave bounces of the obstacle and portion of it returns back to the sensor as echo and sensor drives ECHO output low. The measured distance is proportional to output pulse length:

distance [m] = pulse [s] * 340 [m/s] / 2

distance [cm] = pulse [us] * 0.017

distance [cm] = pulse [us] / 59 // only 0.3% error and no floating point math needed

Arduino Demo Sketch

Here is an example sketch using keypad shield and HC-SR04 sensor. Originally I wrote this sketch for sensor to be inserted directly into Arduino and digital outputs would provide "soft power". However, this only worked with about 20% of sensors. Use the real 5V and GND and change your trigger and echo signals in #define section depending on your application.

/*
 * Example Arduino sketch for HC-SR04 with LCD+keypad shield.
 * dipmicro, 2013, no copyright, no guarantee, AS-IS
 *
 * The circuit:
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD BL pin to digital pin 10
 * KEY pin to analogl pin 0
 *
 * HC-SR04 Vcc to A1 (optional, may not work, use hard 5V)
 * HC-SR04 GND to A4 (optional, may not work, use hard GND)
 *
 * HC-SR04 Trig to A2
 * HC-SR04 Echo to A3
 */

#include 

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
int count = 1;

//
// CHANGE CONTROL SIGNALS HERE
//
#define HC_SR04_VCC A1
#define HC_SR04_TRIGGER A2
#define HC_SR04_ECHO A3
#define HC_SR04_GND A4

void setup() {
    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("HC-SR04 Demo");
    lcd.setCursor(0, 1);
    lcd.print("Distance:");
    lcd.setCursor(10, 1);
    lcd.print(0, 10);
    pinMode(HC_SR04_VCC, OUTPUT);
    digitalWrite(HC_SR04_VCC, HIGH);
    pinMode(HC_SR04_GND, OUTPUT);
    digitalWrite(HC_SR04_GND, LOW);
    pinMode(HC_SR04_TRIGGER, OUTPUT);
    pinMode(HC_SR04_ECHO, INPUT);
}

void loop() {
    digitalWrite(HC_SR04_TRIGGER, LOW);
    delayMicroseconds(4);
    digitalWrite(HC_SR04_TRIGGER, HIGH);
    delayMicroseconds(20);
    digitalWrite(HC_SR04_TRIGGER, LOW);
    delayMicroseconds(10);
    long pulse_us = pulseIn(HC_SR04_ECHO, HIGH);
    int dist_cm = pulse_us / 59;
    lcd.setCursor(10, 1);
    lcd.print("     ");
    lcd.setCursor(10, 1);
    lcd.print(dist_cm, 10);
    lcd.setCursor(13, 0);
    lcd.print(count, 10);
    count = (count+1) % 1000;
    delay(100);
}
	

Arduino HC-SR04 Ultrasonic Distance Sensor
Click on image to zoom.