Home > programming > HC-SR04 ultrasonic distance sensor testing

HC-SR04 ultrasonic distance sensor testing


http://www.alt42.com/2011/12/hc-sr04-ultrasonic-distance-sensor/

 

Playing with arduino some more, I decided to test a HC-SR04 ultrasonic ranger module I have. Ultrasonic range finders are very common with robots. There seems to be a choice of two for budget/hobbyist ultrasonic modules the HC-SR04 and the Parallax Ping))). The Ping))) is more expensive and doesn’t seem to be sold in any UK shops or eBay sellers, so I opted for the HC-SR04 which costs between £2.50 and £8 on eBay.

Either module seems pretty simple to use – the only difference being that the Ping))) module combines the trigger/echo pins into one. The Ping))) is used in an example on the arduino website here. I’ve put my modified version of the code below.

A terse datasheet for the module is available here, which details the timing of the trigger wave. The specs for the module are: 5V DC, 2-500cm effective distance, 15 degree effectual angle and 3cm resolution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Jonathan Francis Roscoe <jonathan@alt42.com>
Uses the HC-SR04 module to print a distance in CM to an LCD.
The Parallax PING))) module seems very similar but combines the trigger/echo ping into one, so this code won’t work.
The module I used is available easily and cheaply, eBay is probably the best place.
*/
// include the library code:
#include
//LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(11, 10, 4, 5, 6, 7);
//set US pins
const int us_trigPin = 45;
const int us_echoPin = 44;
//variables we’ll use
long duration, cm;
void setup() {
//set the ultrasonic pins up
pinMode(us_trigPin, OUTPUT);
pinMode(us_echoPin, INPUT);
//set the lcd
lcd.begin(16, 2);
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(getDistanceAsCM());
lcd.print(“cm”);
//a delay is necessary to prevent the echo of an earlier ping being perceived – otherwise we get inaccurately short distances
delay(100);
}
long getDistanceAsCM(){
//send trigger signal
digitalWrite(us_trigPin, HIGH);
digitalWrite(us_trigPin, LOW);
//get the time to receive rebound
duration = pulseIn(us_echoPin, HIGH);
//sonar math is nicely explained in manual for the branded PING)) module here:   http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
//results seem accurate:
return duration / 29 / 2;
}

As I had a piezo lying around, I plugged it in to try and create a crude hands free tone generator – the idea being that I want to replicate this with capsense and a better audio component for a theremin like device.

Advertisements
Categories: programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: