skip to Main Content

RPi Node-Red: Ultrasonic Distance Sensor (HC-SR04)

Goal:
Read distance measurements from the sonic sensor within Node-Red

What You Will Learn:
How the ultrasonic sensor is wired and how it can be programmed.

Parts List:

What is an Ultrasonic Distance Sensor?

It is a sensor that works based on the principles of echo location.  The sensor is able to emit ultrasonic pulses and read ultrasonic pulses, using this timing information along with the speed of sound and some math we can extrapolate distance to the surface the pulses have reflected against.

\text{distance} = \frac{\text{(time)}\text{(speed of sound)}}{2}

Thankfully the node we will be using will take care of the math for us :).

Getting Started:

Wiring:

  • VCC   -> 5V
  • Trig   -> PIN16 #23
  • Echo -> PIN18 #24
  • GND -> GND

For this sensor you MUST use the same pins as shown for Trig and Echo – it will not work with any other pins. 

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880  using the web browser.  Drag the “rpi srf” node and a “debug” node into the flow area.

Double click the “rpi srf” node to open its configuration settings.  Set Pins to “16,18” (which corespond to GPIO 23,24).  The programmer of this node decided that it was better to use the Raspberry Pi’s board pinout rather than the BCM standard, this web page shows both.

Next, open the “debug” nodes configuration settings.  Check the debug window and node status options.  This will display the last sent message beneath the debug node.

Link the nodes together and deploy the flow.

If everything has gone together properly, you should get numbers appearing below the “debug” node.  These are distance measurements in centimeters, you can change the frequency of the measurements within the “rpi srf” node or see the entire measurement history in the debug tab.

Back To Top