# Ultrasonic Sensor and node-red-node-pisrf

**URL:** https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194
**Category:** General
**Created:** [14 April 2021 17:53 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194 "2021-04-14T17:53:41Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)
#### Post date: [14 April 2021 17:53 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/1 "2021-04-14T17:53:41Z")

</div>

Hi !

Built a project with ultrasonic sensor to check water level. Works fine.  
pisrf gives distance to water level in cm. Is there a way to change it to mm, or give the cm value a decimal?

Thanks for any advice.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [14 April 2021 18:58 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/2 "2021-04-14T18:58:01Z")

</div>

Hi, you could e.g. use [node-red-contrib-unit-converter](https://flows.nodered.org/node/node-red-contrib-unit-converter)

---

<div class="post-metadata">

### Author: ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)
#### Post date: [14 April 2021 19:23 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/3 "2021-04-14T19:23:31Z")

</div>

The problem, that the pisrf node gives a round centimeter value, without decimals.  
1,2,3,4,5,6 cm and so on. If I convert it to mm, I will get 10, 20, 30, 40.  
So the accuracy stays the same. I would like to get at least one decimal.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [14 April 2021 19:59 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/4 "2021-04-14T19:59:00Z")

</div>

I took a quick look at the underlining code and it looks like it is converting the results to an integer.

If I get a chance (be warned, this might take some time since I'm helping home school two grandsons and entertaining their 4 year old sisterl and their 7 month old sister and 'my time' quickly becomes 'their time')

I'll fix up a circuit to test and then see if the code could be changed to return a decimal place.

What sensor are you using?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [14 April 2021 21:04 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/5 "2021-04-14T21:04:04Z")

</div>

After doing some reasearch and reading about the different sensors I don't think the time to add a decimal point is worth it. You will never get mm accuracy. After all, the srf04 sensors has a precision of ~3mm (a 6mm spread) and the srf05 has ~2mm (a 4mm spread). Add to that the fact that the speed of sound changes with the amount of moisture in the air and you will never have an exact measurement.

---

<div class="post-metadata">

### Author: ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)
#### Post date: [15 April 2021 05:05 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/6 "2021-04-15T05:05:56Z")

</div>

Hi !

Home-School, tell me about it! I am with you there. About accuracy, you are probably right, those sensors will never be mm accurate. Haven't thought of the air humidity - sound traveling speed issue, but that adds to the accuracy problme as well.  
My goal was more, to "smoothen out" my graph, not to have a "sawblade".

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/407529ea82b06877aad6e9545281b20c7eb98f27.png)  
Having a decimal in the result could have done that.

Thank you for your time and effort,  
wishing you and your family good health  
and happy schooling!

---

<div class="post-metadata">

### Author: ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)
#### Post date: [15 April 2021 05:12 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/7 "2021-04-15T05:12:23Z")

</div>

Maybe I have to make a different approach. Gather 10-20 measurements, calculate their average and post this value into the graph...hmmm...

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [15 April 2021 05:49 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/8 "2021-04-15T05:49:59Z")

</div>

Look at the smooth node

---

<div class="post-metadata">

### Author: ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)
#### Post date: [15 April 2021 13:15 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/9 "2021-04-15T13:15:05Z")

</div>

That was exactly what I needed, thanks for the tip ! 👍

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 April 2021 14:03 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/10 "2021-04-15T14:03:09Z")

</div>

Or you could use a change node and context storage to smooth the value

```auto
[{"id":"7080fa23.99b904","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*3)+20","payloadType":"jsonata","x":180,"y":4580,"wires":[["b3663021.bfbe9"]]},{"id":"b3663021.bfbe9","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"smooth","pt":"flow","to":"$append([payload],$flowContext(\"smooth\"))[[0..19]]","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$round($average($flowContext(\"smooth\")),1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":4580,"wires":[["4d764ad4.3d8544"]]},{"id":"4d764ad4.3d8544","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":4580,"wires":[]}]

```

this will hold last 20 values and average the output.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [15 April 2021 14:28 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/11 "2021-04-15T14:28:39Z")

</div>

yeah - why did I bother write the smooth node...

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 April 2021 15:13 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/12 "2021-04-15T15:13:07Z")

</div>

Probably as a help for people who have difficulty with code. But for others who want to learn a bite of code, my example may help.

---

<div class="post-metadata">

### Author: ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)
#### Post date: [19 April 2021 13:49 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/13 "2021-04-19T13:49:16Z")

</div>

The Smooth node is pretty awesome, thanks for writing it.

I'm using it consistently on different graphs that represent chemical tank levels and battery charge levels, to both smooth out the results and reduce the update frequency (I need to display 1-3 days of results on variables that are read every 20-30 seconds), and the smooth node solves it perfectly.

@E1cid: Your code example is fine as reference, but if someone really wants to spend some time coding, I'd recommend to spend that time and energy somewhere else, since this issue is already solved impeccably by the Smooth node.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 April 2021 13:54 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/14 "2021-04-19T13:54:49Z")

</div>

> [@OriolFM](#):
>
> wants to spend some time coding, I'd recommend to spend that time and energy somewhere else,

I am sure I said learning to code, which my example helps with, but cheers for your opinion.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [18 June 2021 13:55 UTC](https://discourse.nodered.org/t/ultrasonic-sensor-and-node-red-node-pisrf/44194/15 "2021-06-18T13:55:08Z")

</div>

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