HC-SR04 ultrasonic sensor - manual function

Good morning sir,

is there any possible to make a flow on node-red manually without using rpi-srf. This flow should exactly do the work of rpi-srf. That sounds not a logic question but I am trying to connect that sensor to an IoT gateway. Node-red is ready installed on this gateway but the GPIO has a different form as on rpi (for example digital port nr:8 defined as “D8” ). By using rpi-srf it’s not possible to assign trigger and Echo as D16,D18.

As you see in the attached photo I was able to make a flow to control trigger and my question how can this flow be extended to read the ECHO signal and calculate the distance?


  1. what device are you running NR on?
  2. what device (hardware) is the HC-SR04 physically connected to?

node-red-node-pisrf is a Pi specific node so if you are not using a Pi, you r work is cut out for you.

You could (possibly) use an exec node to call out to a program (that you would have to find or code) to access the HC-SR04 or branch node-red-node-pisrf and see if you could get it to work on the device the HC-SR04 and change the echo port.

That is about the end of my suggestions. It is possible someone else will have another idea, good luck.

1- Node-red (version 2.0.6 + Node.js v14.17.6) is running over Raspberry pi 4 (4GB Ram).
2- HC-SR04 is connected to RPi over GPIO 23, GPIO 24.
3- if I use the following program (python code) running on RPi without Node-red, it runs fine without any problems.

import RPi.GPIO as GPIO
import time
TRIG = 23
ECHO = 24
print("Distance Measurments in Process")

    while True:
        GPIO.output(TRIG, False)
        print("Watting for sensor to Settle")
        GPIO.output(TRIG, True)
        GPIO.output(TRIG, False)
        while GPIO.input(ECHO) == 0:
            pulse_start = time.time()
        while GPIO.input(ECHO) == 1:
            pulse_end = time.time()
        pulse_duration = pulse_end - pulse_start
        distance = pulse_duration * 17150
        distance = round(distance, 2)
        print("Distance: ", distance, " cm")
except KeyboardInterrupt:
    print("Cleaning up!")

4- if I use ,, node-red-node-pisrf " over Node-red, it runs without any problem.

5-question is : how to convert the code at point 3 completly to a flow (as i started this topic) over Node-red in order to control manually GPIO 23,24 or PIN 16.18.

thanks for your input

In order to get data from the HC-SR04 some code needs to be run to read it. The base nodes of Node-RED are not able to do that. You can use a specilized note (like node-red-node-pisrf), Code a new node or you could write or finds some code in another language (python, javascript etc) and use an exec node to run it and return the results.

But no, Node-RED can't do it without something else to talk to the device.

Have you thought about using an Arduino or WeMos D1 to do the readings and then send the results to NR via MQTT?

Following up on what Paul @zenofmud said... here's a link to a tutorial that explains using a Wemos D1 Mini to send distance measurements to Node-RED via MQTT.


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.