HCSR04

Module to use the untrasonic sensor HC-SR04.

Datasheet: https://www.mpja.com/download/hc-sr04_ultrasonic_module_user_guidejohn.pdf

class devices.hcsr04.hcsr04.HCSR04(trigger_pin, echo_pin, echo_timeout_us=30000)

Bases: object

Driver to use the untrasonic sensor HC-SR04.

The sensor range is between 2cm and 4m. The timeouts received listening to echo pin are converted to OSError(‘Out of range’).

By default is based in sensor limit range (4m).

Parameters:
  • trigger_pin – Output pin to send pulses
  • echo_pin – Readonly pin to measure the distance. The pin should be protected with 1k resistor.
  • echo_timeout_us – Timeout in microseconds to listen to echo pin.
_send_pulse_and_wait()

Send the pulse to trigger and listen on echo pin.

We use the method machine.time_pulse_us() to get the microseconds until the echo is received.

distance_cm()

Get the distance in centimeters with floating point operations.

Returns:distance in cm (float)
distance_mm()

Get the distance in milimeters without floating point operations.

Returns:distance in mm (int)