Module for Ultrasonic sensor (HC-SR04)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
orsisam
Posts: 9
Joined: Mon Sep 05, 2016 5:58 pm

Module for Ultrasonic sensor (HC-SR04)

Post by orsisam » Sun Sep 25, 2016 3:32 pm

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?

User avatar
pythoncoder
Posts: 4678
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Module for Ultrasonic sensor (HC-SR04)

Post by pythoncoder » Sun Sep 25, 2016 4:27 pm

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 ;)
Peter Hinch

orsisam
Posts: 9
Joined: Mon Sep 05, 2016 5:58 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by orsisam » Mon Sep 26, 2016 1:01 am

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. ;)

User avatar
mcauser
Posts: 486
Joined: Mon Jun 15, 2015 8:03 am

Re: Module for Ultrasonic sensor (HC-SR04)

Post by mcauser » Mon Sep 26, 2016 1:21 am

@orsisam paste your git repo when it's ready and I'll help you test it on some of my D1 minis.

User avatar
ernitron
Posts: 84
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Module for Ultrasonic sensor (HC-SR04)

Post by ernitron » Mon Sep 26, 2016 5:37 am

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.

User avatar
deshipu
Posts: 1380
Joined: Thu May 28, 2015 5:54 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by deshipu » Mon Sep 26, 2016 5:54 am

HC-SR04 sensors require 5V power.

User avatar
mcauser
Posts: 486
Joined: Mon Jun 15, 2015 8:03 am

Re: Module for Ultrasonic sensor (HC-SR04)

Post by mcauser » Mon Sep 26, 2016 6:21 am

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.

User avatar
ernitron
Posts: 84
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Module for Ultrasonic sensor (HC-SR04)

Post by ernitron » Mon Sep 26, 2016 6:23 am

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...

User avatar
pythoncoder
Posts: 4678
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Module for Ultrasonic sensor (HC-SR04)

Post by pythoncoder » Mon Sep 26, 2016 6:28 am

Why not power both from a common regulated 5V supply?
Peter Hinch

User avatar
ernitron
Posts: 84
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Module for Ultrasonic sensor (HC-SR04)

Post by ernitron » Mon Sep 26, 2016 7:38 pm

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)

Post Reply