Skip to content

Instantly share code, notes, and snippets.

@electronut
Created May 28, 2013 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electronut/5662576 to your computer and use it in GitHub Desktop.
Save electronut/5662576 to your computer and use it in GitHub Desktop.
Get distance information from Ultrasonic Ranging Module HC-SR04 and send it via serial port.
// HC-SR04-test.ino
//
// Get distance information from Ultrasonic Ranging Module HC-SR04 and
// send it via serial port.
//
// electronut.in
#include "Arduino.h"
int pinTrigger = 2;
int pinEcho = 4;
void setup()
{
// initialize serial comms
Serial.begin(9600);
// set pins
pinMode(pinTrigger, OUTPUT);
pinMode(pinEcho, INPUT);
}
void loop()
{
// send a 10us+ pulse
digitalWrite(pinTrigger, LOW);
delayMicroseconds(20);
digitalWrite(pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrigger, LOW);
delayMicroseconds(20);
// read duration of echo
int duration = pulseIn(pinEcho, HIGH);
if(duration > 0) {
// dist = duration * speed of sound * 1/2
// dist in cm = duration in us * 1 x 10^{-6} * 340.26 * 100 * 1/2
// = 0.017*duration
float dist = 0.017 * duration;
Serial.println(dist);
}
// wait
delay(70);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment