Page 1 of 4

Module for Ultrasonic sensor (HC-SR04)

Posted: Sun Sep 25, 2016 3:32 pm
by orsisam
Hi,

I want to create a water-level project using Wemos D1 Mini, but I don't find right module for HC-SR04 ultrasonic sensor. I found one here https://github.com/skgsergio/MicropythonLibs.git, but it is used for Pyboard. If someone has porting this code to esp8266?

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Sun Sep 25, 2016 4:27 pm
by pythoncoder
Porting that code should be very easy. It was probably written before the utime module provided microsecond level timing. The sleep_us, ticks_us and ticks_diff functions mean that there's no need to use a timer counter. Use sleep_us to time the initial 10us pulse, record ticks_us, loop waiting for the return pulse, then use ticks_diff to determine how long it took. Copy the calculations from the original code. Test and post on GitHub ;)

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 1:01 am
by orsisam
pythoncoder wrote:Porting that code should be very easy. It was probably written before the utime module provided microsecond level timing. The sleep_us, ticks_us and ticks_diff functions mean that there's no need to use a timer counter. Use sleep_us to time the initial 10us pulse, record ticks_us, loop waiting for the return pulse, then use ticks_diff to determine how long it took. Copy the calculations from the original code. Test and post on GitHub ;)
I am going to try..
next I will share if I can make it works. Thanks for your help. ;)

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 1:21 am
by mcauser
@orsisam paste your git repo when it's ready and I'll help you test it on some of my D1 minis.

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 5:37 am
by ernitron
I am afraid it won't work. Probably the current supplied from wemos d1 is not enough. Mine works with arduino but not with wemos. Micropython is not the issue.

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 5:54 am
by deshipu
HC-SR04 sensors require 5V power.

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 6:21 am
by mcauser
The D1 mini has a 5V line, but uses 3V3 logic, so you'll need to use a voltage divider for the Echo pin.
Here's an example of how to do it with a Raspberry Pi

It seems ernitron is right about the current draw being the limiting factor. HC-SR04 has a working current of 15mA but the ESP8266 maximum source/sink current is 12mA per pin.

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 6:23 am
by ernitron
Yes, WeMos has 5V supply (and also 3.3V) but the problem is that the current is not enough for HC-SR04. I did not succeed to make it working with WeMos D1.

EDIT: It seems confirmed by mcauser...

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 6:28 am
by pythoncoder
Why not power both from a common regulated 5V supply?

Re: Module for Ultrasonic sensor (HC-SR04)

Posted: Mon Sep 26, 2016 7:38 pm
by ernitron
Yes pythoncoder. I guess that powering with a stable source both WeMos and HC-SR04 should work (but I didn't try).
https://www.dropbox.com/s/5yozz2k80qb9qyl/h.jpg

Beside, simplified code will look like:

Code: Select all

# HC-SR04 Ultrasound Sensor
import time
from machine import Pin

# WeMos D4 maps GPIO2 machine.Pin(2) = TRIGGER
# WeMos D2 maps GPIO4 machine.Pin(4) = ECHO
triggerPort = 2
echoPort = 4

def loop():
    trigger = Pin(triggerPort, Pin.OUT)
    echo = Pin(echoPort, Pin.IN)
    print("Ultrasonic Sensor. Trigger Pin=%d and Echo Pin=%d" % (triggerPort, echoPort))
    trigger.low()
    while True:
      # short impulse 10 microsec to trigger
      trigger.high()
      time.sleep_us(10)
      trigger.low()
      count = 0
      start = time.ticks_us() # get time in usec
      # Now loop until echo goes high
      while not echo.value():
          time.sleep_us(10)
          count += 1
          if count > 100:
              print("Counter exceeded")
              break
      duration = time.ticks_diff(start, time.ticks_us()) # compute time difference
      print("Duration: %f" % duration)

      # After 38ms is out of range of the sensor
      if duration > 38000 :
          print("Out of range")
          continue

      # distance is speed of sound [340.29 m/s = 0.034029 cm/us] per half duration
      distance = 0.017015 * duration
      print("Distance: %f cm" % distance)
      time.sleep(2)