Skip to content

HC-SR04 — Initialization (hcsr_init)

What it does: Initializes the HC-SR04 ultrasonic range finder (TRIG/ECHO), creates a sensor instance, and sets the echo timeout to 15000 µs.

hcsr_init

Inputs

  • echo: GPIO pin for ECHO input.
  • trigger: GPIO pin for TRIG output.
  • Field hc_id: sensor instance number, for the ability to use multiple sensors simultaneously

Output

  • None (statement block).

When to use it

  • Once at program start before blocks hcsr_read_mm and hcsr_read_cm.

Tips and pitfalls

  • HC-SR04 is an ultrasonic sensor (typically 40 kHz) for measuring the distance from a reflective obstacle.
  • The normally stated range of the module is approx. 2 to 400 cm, but the timeout used here limits the range to approx. 2.5 m and thus shortens the measurement time.
  • The sensor is sensitive to the angle of obstacles, soft materials and air temperature.

Usage example

example_hcsr

Open example in ESP IDE

Generated Python example

ultraSoundSensor1 = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=15000)