Skip to content

Commit 04be232

Browse files
authored
Added HC-SR04 ultrasonic distance sensor. (#143)
* Added HC-SR04 ultrasonic distance sensor.
1 parent b1529dc commit 04be232

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ smoke-test:
4949
@md5sum ./build/test.hex
5050
tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
5151
@md5sum ./build/test.hex
52+
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
53+
@md5sum ./build/test.hex
5254
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
5355
@md5sum ./build/test.hex
5456
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The following 46 devices are supported.
7373
| [ESP8266/ESP32 AT Command set for WiFi/TCP/UDP](https://github.com/espressif/esp32-at) | UART |
7474
| [GPS module](https://www.u-blox.com/en/product/neo-6-series) | I2C/UART |
7575
| [HD44780 LCD controller](https://www.sparkfun.com/datasheets/LCD/HD44780.pdf) | GPIO |
76+
| [HC-SR04 Ultrasonic distance sensor](https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf) | GPIO |
7677
| [HUB75 RGB led matrix](https://cdn-learn.adafruit.com/downloads/pdf/32x16-32x32-rgb-led-matrix.pdf) | SPI |
7778
| [ILI9341 TFT color display](https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf) | SPI |
7879
| [L293x motor driver](https://www.ti.com/lit/ds/symlink/l293d.pdf) | GPIO/PWM |

easystepper/easystepper.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ type DualDevice struct {
1818
devices [2]Device
1919
}
2020

21-
// New returns a new easystepper driver given 4 pins numbers (not pin object),
22-
// number of steps and rpm
21+
// New returns a new easystepper driver given 4 pins, number of steps and rpm
2322
func New(pin1, pin2, pin3, pin4 machine.Pin, steps int32, rpm int32) Device {
2423
return Device{
2524
pins: [4]machine.Pin{pin1, pin2, pin3, pin4},
@@ -34,8 +33,7 @@ func (d *Device) Configure() {
3433
}
3534
}
3635

37-
// NewDual returns a new dual easystepper driver given 8 pins numbers (not pin object),
38-
// number of steps and rpm
36+
// NewDual returns a new dual easystepper driver given 8 pins, number of steps and rpm
3937
func NewDual(pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin8 machine.Pin, steps int32, rpm int32) DualDevice {
4038
var dual DualDevice
4139
dual.devices[0] = Device{

examples/hcsr04/main.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package hcsr04
2+
3+
import (
4+
"machine"
5+
"time"
6+
7+
"tinygo.org/x/drivers/hcsr04"
8+
)
9+
10+
func main() {
11+
sensor := hcsr04.New(machine.D10, machine.D9)
12+
sensor.Configure()
13+
14+
println("Ultrasonic starts")
15+
for {
16+
println("Distance:", sensor.ReadDistance(), "mm")
17+
18+
time.Sleep(100 * time.Millisecond)
19+
}
20+
}

hcsr04/hcsr04.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Package hcsr04 provides a driver for the HC-SR04 ultrasonic distance sensor
2+
//
3+
// Datasheet:
4+
// https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
5+
package hcsr04
6+
7+
import (
8+
"machine"
9+
"time"
10+
)
11+
12+
const TIMEOUT = 23324 // max sensing distance (4m)
13+
14+
// Device holds the pins
15+
type Device struct {
16+
trigger machine.Pin
17+
echo machine.Pin
18+
}
19+
20+
// New returns a new ultrasonic driver given 2 pins
21+
func New(trigger, echo machine.Pin) Device {
22+
return Device{
23+
trigger: trigger,
24+
echo: echo,
25+
}
26+
}
27+
28+
// Configure configures the pins of the Device
29+
func (d *Device) Configure() {
30+
d.trigger.Configure(machine.PinConfig{Mode: machine.PinOutput})
31+
d.echo.Configure(machine.PinConfig{Mode: machine.PinInput})
32+
}
33+
34+
// ReadDistance returns the distance of the object in mm
35+
func (d *Device) ReadDistance() int32 {
36+
pulse := d.ReadPulse()
37+
38+
// sound speed is 343000 mm/s
39+
// pulse is roundtrip measured in microseconds
40+
// distance = velocity * time
41+
// 2 * distance = 343000 * (pulse/1000000)
42+
return (pulse * 1715) / 10000 //mm
43+
}
44+
45+
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
46+
func (d *Device) ReadPulse() int32 {
47+
t := time.Now()
48+
d.trigger.Low()
49+
time.Sleep(2 * time.Microsecond)
50+
d.trigger.High()
51+
time.Sleep(10 * time.Microsecond)
52+
d.trigger.Low()
53+
i := uint8(0)
54+
for {
55+
if d.echo.Get() {
56+
t = time.Now()
57+
break
58+
}
59+
i++
60+
if i > 10 {
61+
if time.Since(t).Microseconds() > TIMEOUT {
62+
return 0
63+
}
64+
i = 0
65+
}
66+
}
67+
i = 0
68+
for {
69+
if !d.echo.Get() {
70+
return int32(time.Since(t).Microseconds())
71+
}
72+
i++
73+
if i > 10 {
74+
if time.Since(t).Microseconds() > TIMEOUT {
75+
return 0
76+
}
77+
i = 0
78+
}
79+
}
80+
return 0
81+
}

0 commit comments

Comments
 (0)