Arduino to Raspberry Pi Serial Communication for Ultrasonic Sensor
Connecting Arduino to Raspberry pi through a UART and displaying data
In this example, we will show you how to
Connect an HC-SR04 Ultrasonic sensor to your Arduino
Program the Arduino to read the Ultrasonic Sensor
Sensing through Node-Red
Publish data to IoTFlows console
Publish Alert to IoTFlows Alert Channel
To complete this project you will need:
HC-SR04 Ultrasonic sensor
Arduino ( Model: MKR.WIFI 1010 is used in this example)
Raspberry Pi (Model: Raspberry Pi 4 is used in this example)
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 Arduino pin 5V, pin GND, pin 4, and pin 5.
Plug four of your male to female jumper wires into the pins on the HC-SR04.
Plug the Vcc jumper wire into the 5V pin of the Arduino.
Plug the GND jumper wire into the GND pin of the Arduino.
Plug the TRIG jumper wire into pin 4 of the Arduino.
Plug the ECHO jumper wire into pin 5 of the Arduino.
Next, we will program our Arduino to read data from the Ultrasonic sensor. Copy the following code into your Arduino IDE and upload it into your Arduino.
long duration, distance;
distance = duration /58;
Sensing through Node-Red
Now that we have our Arduino set-up, connect the USB directly into one of the Raspberry Pi USB ports.
Next, in your Node-Red console, drag and drop a serial in node into your canvas
Double-click on the serial in node and click the edit button for Serial Port
Next, click the search button for the Serial Port and select the port that is connected to your Arduino, in our case it is /dev/ttyACM0
Click Update once complete, and click Done on the next screen. Your serial in node is now configured!
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
Click Deploy and you will be able to view the data coming into your Node-Red console.
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
Next, connect your serial in 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?"
if(msg.payload > 350)
Next, we will set up a delay node in order to not get spammed by messages when that value is reached and stays there continuously.
Next, double-click the delay node and set up its configuration, 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.
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"
"subject": "Distance over reached",
"description": "Ultrasonic sensor distance is over 350"
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.