Standard library · Hardware

hw.hcsr04

Measure distance with the HC-SR04 ultrasonic sensor in millimetres or centimetres.

import hw.hcsr04

hw.hcsr04 reads the HC-SR04 ultrasonic sensor: it pulses the trigger pin for 10 microseconds, measures how long the echo pin stays high and turns that time into a distance. The usable range of the chip is about 2 cm to 4 m. The package needs the pulse measurement of hw.board, so on a computer you feed it with board.SimSetPulse.

Measuring a distance

Open the sensor with its two pins and call DistanceMm or DistanceCm. In the simulation, set the echo width with board.SimSetPulse: 583 microseconds is about 100 mm, and 0 means no echo, which both functions report as -1.

hw-hcsr04-guide-distance.salam
Try it
import hw.board
import hw.hcsr04

func main:
    sonar := hcsr04.Open(4, 5)
    each width in [0, 583, 1166, 38000]:
        board.SimSetPulse(5, width as u64)
        println width, hcsr04.DistanceMm(sonar), hcsr04.DistanceCm(sonar)
    end
end
Output
0 -1 -1
583 99 9.9
1166 199 19.9
38000 6517 651.7

No echo and the time limit

EchoMicros gives the raw time. An echo longer than MAX_ECHO_US counts as none, so far or soft objects, and a sensor that is not connected, give 0 and a distance of -1. Check for that before you use the number.

hw-hcsr04-guide-timeout.salam
Try it
import hw.board
import hw.hcsr04

func main:
    sonar := hcsr04.Open(4, 5)
    println hcsr04.EchoMicros(sonar)
    board.SimSetPulse(5, 40000 as u64)
    println hcsr04.EchoMicros(sonar), hcsr04.DistanceMm(sonar)
end
Output
0
0 -1

Types

Sensor

#
struct Sensor

An HC-SR04: the pin that triggers it and the pin the echo comes back on.

NameTypeDescription
triggerint = 0The pin wired to the trigger input.
echoint = 0The pin wired to the echo output.
hw-hcsr04-Sensor.salam
Try it
import hw.board
import hw.hcsr04

func main:
    sonar := hcsr04.Open(4, 5)
    println hcsr04.MAX_ECHO_US
    board.SimSetPulse(5, 583 as u64)
    println hcsr04.EchoMicros(sonar)
    println hcsr04.DistanceMm(sonar), hcsr04.DistanceCm(sonar)
    board.SimSetPulse(5, 0 as u64)
    println hcsr04.DistanceMm(sonar)
end
Output
38000
583
99 9.9
-1

Constants and variables

MAX_ECHO_US

#
const MAX_ECHO_US: i32 = 38000

The longest echo the driver waits for, 38000 microseconds. A longer or missing echo means no object in range.

The example for this item is shown with the ones above: hw-hcsr04-Sensor.salam

Functions

Open

#
func Open(trigger: int, echo: int): Sensor

Sets the trigger pin as an output (low) and the echo pin as an input, and returns the sensor.

The example for this item is shown with the ones above: hw-hcsr04-Sensor.salam

EchoMicros

#
func EchoMicros(s: Sensor): u64

Sends a 10 microsecond trigger pulse and returns the width of the echo in microseconds, or 0 when no echo comes within MAX_ECHO_US.

The example for this item is shown with the ones above: hw-hcsr04-Sensor.salam

DistanceMm

#
func DistanceMm(s: Sensor): int

The distance to the object in millimetres, from the echo time and the speed of sound (343 m/s, there and back). -1 when there is no echo.

The example for this item is shown with the ones above: hw-hcsr04-Sensor.salam

DistanceCm

#
func DistanceCm(s: Sensor): f64

The distance in centimetres as a float, with one decimal. -1.0 when there is no echo.

The example for this item is shown with the ones above: hw-hcsr04-Sensor.salam