# Ultrasonic Distance Sensor node - revisited

**URL:** https://discourse.nodered.org/t/ultrasonic-distance-sensor-node-revisited/87233
**Category:** Share Your Projects
**Created:** [14 April 2024 12:38 UTC](https://discourse.nodered.org/t/ultrasonic-distance-sensor-node-revisited/87233 "2024-04-14T12:38:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [14 April 2024 12:38 UTC](https://discourse.nodered.org/t/ultrasonic-distance-sensor-node-revisited/87233/1 "2024-04-14T12:38:06Z")

</div>

Just found an ultrasonic distance sensor node one of my IoT students built a few years ago.

 ![ultrasonic_BB_a](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06e1a612f6f8d722fb0ec7e59f58116148960180.jpeg)

 ![ultrasonic_BB_b](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b4bd8bceff17ad3fcd023e0ad89f2e253416c6a.jpeg)

As an exercise I've flashed the Wemos D1 Mini (the microcontroller the student used) with Micro-Python and uploaded this script. I've made use of a 'Class' to make the code easier to understand.

```auto
from machine import Pin, Timer
import time

class SRF05:
    def __init__ (self, trigger_pin, echo_pin):
        self.trigger = Pin(trigger_pin, Pin.OUT)
        self.echo = Pin(echo_pin, Pin.IN)
        self.trigger.value(0)
        self.timer = Timer(0)

    def _send_pulse(self):
        self.trigger.value(1)
        time.sleep_us(10) # Send a 10us pulse
        self.trigger.value(0)

    def measure(self):
        self._send_pulse()
        while self.echo.value() == 0:
            signaloff = time.ticks_us()
        while self.echo.value() == 1:
            signalon = time.ticks_us()

        timepassed = signalon - signaloff
        distance = (timepassed * 0.0343) / 2
        return distance

# Initialise sensor
sensor = SRF05(trigger_pin=13, echo_pin=12)

# Main loop
while True:
    distance = sensor.measure()
    print("Distance:", distance, "cm")
    time.sleep(1) # Delay between measurements

```

The `_send_pulse()` method in the `SRF05` class sets the trigger pin high for 10 microseconds, which initiates the measurement by sending a burst of ultrasonic pulses.

After sending the pulse, the code measures how long it takes for the echo to return. This duration is converted into a distance using the formula for the speed of sound (approx. 0.0343 cm/μs), and then it is divided by 2 to account for the echo's travel to the object and back.

As you can see it produces distance readings...  
 ![ultrasonic_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21759e6230ff399db335e4b170f16742e1c4367b.png)

Detection distance is 2-450 cm  
[https://www.velleman.eu/products/view/?id=435526&country=be&lang=en](https://www.velleman.eu/products/view/?id=435526&country=be&lang=en)

---

<div class="post-metadata">

### Author: ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)
#### Post date: [14 April 2024 23:02 UTC](https://discourse.nodered.org/t/ultrasonic-distance-sensor-node-revisited/87233/2 "2024-04-14T23:02:24Z")

</div>

I'm running my watertank [sensor](https://www.aliexpress.com/item/1005004551137969.html?aff_platform=true&aff_short_key=UneMJZVf&isdl=y&src=bing&pdp_npi=3%40dis%21AUD%212.03%212.03%21%21%210%21%21%40%2112000029579027464%21ppc%21%21) for over 2 years now

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe71116b33a9215843738cf6c48fe19baa311050.jpeg)

.. works flawless and mostly out-of-the box with TASMOTA  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/8265590ea68b37f9b35abee03e527239ff6d9d6a.png)

> [@Water Level Meter - SR04M-2](https://discourse.nodered.org/t/water-level-meter-sr04m-2/56580/19):
>
> Hello, I am using the same sensor for my watertank and had initially a lot of reading errors especially when the tank was nearly full. I'm using the sensor on an ESP running Tasmota but the code you shared is following the same approach. The ESP and the sensor is powered by a 12v Solar System with battery . Tasmota provides you all the data you need out of the box . Just connect the sensor to the pins and configure as below The issue with this sensor is that it gives a W…

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [15 April 2024 07:31 UTC](https://discourse.nodered.org/t/ultrasonic-distance-sensor-node-revisited/87233/3 "2024-04-15T07:31:52Z")

</div>

I'll have to order a waterproof version for the water storage tank in my greenhouse. If I remember correctly, the students combined the ulrasonic distance sensor with a string of NeoPixel RGB lights on a Christmas tree. So as someone walked towards the tree, the NeoPixels changed colour.
