How to use Ultrasonic Distance Sensor with Arduino

An ultrasonic distance sensor (HC-SR04) is included in Hackabot Uno Robotic Kit.

arduino_ultronic_sensor_hc_sr04

Connections

It is fairly simple to use it to measure distance with Arduino. Since most of the pins are used by the motor shield, we are going to use A0 and A1 pins in this example.

There are 4 pins in HC-SR04 module: VCC, Trig, Echo, GND.

The connections are as follows:

Sensor: VCC   (Arduino Uno: +5V)

Sensor: Trig  (Arduino Uno: A0)

Sensor: Echo (Arduino Uno: A1)

Sensor: GND (Arduino Uno: GND)

You may mount the sensor in the front of the robot using the breadboard from the kit:

arduino_sensors_breadboard

hackarobot_youtube

You may notice that there is no header pins on A0-5 pins on the motor shield:

motor_shield_connection

You will need to purchase some header pins and solder them for easy connection with Dupont cables.

header_pins

 

Programming

Method 1 (basic)

According to the datasheet, we need to first send out a 10uS pulse on Trig pin (A0). This makes the sensor send out 8 cycle sonic burst. Echo pin goes high once the burst is reflected back to the sensor. The pulse width on Echo pin is proportional to the distance measured.

The following program is an example of how to measure the distance.

#define trigPin 14 //A0
#define echoPin 15 //A1

long distance;

void setup() {
  Serial.begin (9600);
  // Set the pin directions
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}

void loop() {
  distance=getDistance();
  Serial.print("Distance :");
  Serial.print(distance);
  Serial.print(" cm");
  // wait for 1 second
  delay(1000);
}

long getDistance() {
  long duration, distance;
  // Start the operation by sending out a 10uS pulse on Trig Pin
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  
  // Wait for Echo pin to pulse (high)
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;   // in cm
  return distance;
}

Method 2 (new ping)

One of the drawbacks of method 1 is that the operation is slow. Upon searching the net, I found that Tim Eckel has written a new library to speed it up (upto 30 measurements per second). You may download the Arduino New Ping library here.

The following is an example of how to use the new ping library:

#include <NewPing.h>
long distance;

#define TRIGGER_PIN  14  // (A0) Arduino pin tied to trigger pin on the ultrasonic sensor. 
#define ECHO_PIN     15  // (A1) Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() { 
Serial.begin(9600); // set up Serial library at 9600 bpsvoid loop() { 
  distance=getDistance();
  Serial.print("Distance :");
  Serial.print(distance);
  Serial.print(" cm");
  // wait for 1 second
  delay(1000);
} 


long getDistance() {
  long distance;
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  distance = uS / US_ROUNDTRIP_CM;
  return distance;
}