Ultrasonic Sensor on Raspberry Pi Node-Red

In this example, we will show you how to

  1. Connect an HC-SR04 Ultrasonic sensor to your Raspberry Pi

  2. Sensing through Node-Red

  3. Publish data to IoTFlows console

  4. Publish Alert to IoTFlows Alert Channel

To complete this project you will need:

Hardware:

  • HC-SR04 Ultrasonic sensor

  • Raspberry Pi

  • Jumper Wires

Software:

  • IoTFlows Platform

  • Node-Red

Connect Ultrasonic Sensor

The HC-SR04 Ultrasonic sensor has 4 pins: ground (GND), Echo Pulse Output (ECHO), Trigger Pulse Input (TRIG), and 5V Supply (Vcc). As shown below

We will use the jumper wires to connect these 4 pins to the Raspberry pins GPIO 5V [Pin 2], GPIO GND [Pin 6], GPIO 23 [Pin 16], GPIO 24 [Pin 18].

  1. Plug four of your male to female jumper wires into the pins on the HC-SR04.

  2. Plug the Vcc jumper wire into GPIO 5V [Pin 2] of the Pi.

  3. Plug the GND jumper wire into GPIO GND [Pin 6] of the Pi.

  4. Plug the TRIG jumper wire into GPIO 23 [Pin 16] of the Pi.

  5. Plug the ECHO jumper wire into GPIO 24 [Pin 18] of the Pi.

IMPORTANT: The sensor output signal (ECHO) on the HC-SR04 is rated at 5V. However, the input pin on the Raspberry Pi GPIO is rated at 3.3V. Sending a 5V signal into that unprotected 3.3V input port could damage your GPIO pins, which is something we want to avoid! We recommend you to use a small voltage divider circuit, to lower the sensor output voltage to something our Raspberry Pi can handle. For simplicity, we will skip this step.

You are done! You've connected your HC-SR04 Ultrasonic sensor to your Raspberry Pi.

Sensing with Node-Red

Now that we've hooked our Ultrasonic sensor to our Pi, we need to program Node-Red to detect distance.

In your Node-Red console, make sure you have the node for a Raspberry Pi to use a SRF04 or SRF05 range finder

node-red-node-pisrf

After installation is complete, you will see the following rpi srf node in your console

Drag and drop the rpi srf node in your canvas and configure it as follows

Click Done and that is it! you can also add a debug node to view the incoming data on your Node-Red console.

Click on the debug node and select node status (32 characters) for the values to be displayed below that node

Publish Data to IoTFlows

Next, we will publish data into our IoTFlows data streams table. To do this we will Node-Red IoTFlows nodes, if you do not have these installed, go to IoTFlows Node-Red Nodes for installation details.

Create a Data Stream

In your device, create a data stream for your Ultrasonic Sensor. For more details on how to create a data stream go to Creating a Data Stream.

We will label the data stream the following:

  • Data Stream Name: Ultrasonic

  • Data Stream Description: Ultrasonic Sensor example

  • Data Stream Units: cm

Integrate Data Stream Node

Now that we have a data stream created for our Ultrasonic sensor, we will drag and drop the datastream out node into our canvas. If you do not have this installed, go to IoTFlows Node-Red Nodes for installation details.

Double-click on the datastream out node and click on the edit button for the server

Here we will add the client that will be used to publish data into our table. If you do not have a client set up or need more information on setting up a client go to Creating a Device Client.

Type in your Client Id and Password, and click Add.

Your client should now be displayed as your server.

Next, we will click the refresh button in the Topic row and select our Ultrasonic sensor

Click Done.

Next, connect your Pi SRF node to your datastream out node and click Deploy

Now your data stream node is connected and we are receiving data into our IoTFlows data streams table, as shown below.

Publishing an Alert

Next, we will publish an Alert into our IoTFlows Alerts table and send an email and text message after this alert is triggered. To do this we will use the Node-Red IoTFlows nodes, if you do not have these installed, go to IoTFlows Node-Red Nodes for installation details.

Creating Alert Logic

In order to trigger an alert, we will set up a simple function that tells us when the Ultrasonic sensor reaches a value of over 350.

First, we set up a function node to determine if the value has reached over 350. We will name it "is > 350?"

Node-Red
Node-Red
if(msg.payload > 350)
{
return msg;
}

Next, we will set up a delay node in order to not get spammed by messages when that value is reached and stayed there continuously.

Next, we will configure the delay node, this is a personal preference so adjust parameters to what fits you best.

This will limit the number of messages we receive to just 1 message per minute and it will ignore the rest.

Click Done.

Configuring Alert Message

Next, we will set up another function node to alert us that the value has reached over 350. We will name this "Ultrasonic Alert Message"

Node-Red
Node-Red
msg.payload =
{
"severity_level": "MINOR",
"subject": "Distance over reached",
"description": "Ultrasonic sensor distance is over 350"
};
return msg;

Click Done.

Integrate Alert Channel Node

Now that we have everything set up to publish our alert, we will drag and drop the alert channel out node into our canvas. If you do not have this installed, go to IoTFlows Node-Red Nodes for installation details.

Double-click on the alert channel out node and similar to the data streams node, we will double click on the edit button for the Server and configure our Client Id and Password. If you do not have a client set up or need more information on setting up a client go to Creating a Device Client.

Next, we will click the refresh button in the Topic and select the Alert Channel that we will like to publish to.

Once everything is complete, click Done.

Next, we will connect all the nodes and click Deploy

As you can see, the alert channel out node is now connected.

We can now go to our Alert Channel in the IoTFlows platform and view the alert message

If you would like to receive text messages or email with these alert messages, turn on your notifications in your Manage Alert Channel Notifications. For more details on configuring notifications, go to Manage Alert Channel Notifications.

‚Äč