Skip to content

Commit 45aa67a

Browse files
committed
add example code for HC-04 ultrasonic range finder
1 parent 961ff73 commit 45aa67a

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

examples/ping-hc04/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../sduino.mk

examples/ping-hc04/ping-hc04.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* SR-HC04 ultrasonic distance Sensor
2+
3+
This sketch reads a SR-HC04 ultrasonic rangefinder and returns the
4+
distance to the closest object in range. To do this, it sends a pulse
5+
to the sensor to initiate a reading, then listens for a pulse
6+
to return. The length of the returning pulse is proportional to
7+
the distance of the object from the sensor.
8+
9+
The circuit:
10+
* +V connection of the HC-04 attached to +5V
11+
* GND connection of the HC-04 attached to ground
12+
* Echo connection of the HC-04 attached to digital pin 0/PA1
13+
* Trig connection of the HC-04 attached to digital pin 1/PA2
14+
15+
This sketch is derived from the example sketch for the Ping)) sensor.
16+
The Ping)) sensor uses the same pin connection for the trigger and the echo
17+
signal, the HC-04 uses two separate connections.
18+
http://www.arduino.cc/en/Tutorial/Ping
19+
20+
created 3 Nov 2008
21+
by David A. Mellis
22+
modified 30 Aug 2011
23+
by Tom Igoe
24+
modified 1 Mar 2017 for use with HC-04 and sduino
25+
by Michael Mayer
26+
27+
This example code is in the public domain.
28+
29+
*/
30+
31+
#include <Arduino.h>
32+
33+
// this constant won't change. It's the pin number
34+
// of the sensor's output:
35+
const int trigPin = PA2;
36+
const int echoPin = PA1;
37+
38+
long microsecondsToInches(long microseconds);
39+
long microsecondsToCentimeters(long microseconds);
40+
41+
void setup() {
42+
// make sure we start with a high level when the pin is set to output
43+
digitalWrite(trigPin, HIGH);
44+
pinMode(trigPin, OUTPUT);
45+
pinMode(echoPin, INPUT);
46+
// initialize serial communication:
47+
Serial_begin(9600);
48+
}
49+
50+
void loop() {
51+
// establish variables for duration of the ping,
52+
// and the distance result in inches and centimeters:
53+
long duration, inches, cm;
54+
55+
// The HC-04 is triggered by a falling edge on the trigger input. The
56+
// following low period has to be at least 10us long.
57+
digitalWrite(trigPin, LOW);
58+
59+
// approx. 450us later the Echo pins gets high. It stays high until the echo
60+
// is received. Maximum timeout is 200ms (no echo received)
61+
duration = pulseInLong(echoPin, HIGH, 200000L);
62+
digitalWrite(trigPin, HIGH);
63+
64+
// convert the time into a distance
65+
inches = microsecondsToInches(duration);
66+
cm = microsecondsToCentimeters(duration);
67+
68+
Serial_print_u(duration);
69+
Serial_print_s("us\t");
70+
Serial_print_u(inches);
71+
Serial_print_s("in\t");
72+
Serial_print_u(cm);
73+
Serial_println_s("cm");
74+
75+
// maximum is one measurement every 20ms
76+
delay(100);
77+
}
78+
79+
long microsecondsToInches(long microseconds) {
80+
// According to Parallax's datasheet for the PING))), there are
81+
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
82+
// second). This gives the distance travelled by the ping, outbound
83+
// and return, so we divide by 2 to get the distance of the obstacle.
84+
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
85+
return microseconds / 74 / 2;
86+
}
87+
88+
long microsecondsToCentimeters(long microseconds) {
89+
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
90+
// The ping travels out and back, so to find the distance of the
91+
// object we take half of the distance travelled.
92+
//
93+
// The exact value for speed sound depends on the air temperature:
94+
// v = (331.5 + 0.6*temp/°C) m/s
95+
// v(temp=20°C) = (331.5 + 0.6*20)m/s = 343.5m/s
96+
return microseconds / 29 / 2;
97+
}

examples/ping-hc04/ping-hc04.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Detecting objects with an ultrasonic range finder.

0 commit comments

Comments
 (0)