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.
jpedrodias
Posts: 3
Joined: Fri Jun 24, 2016 10:27 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by jpedrodias » Fri Jan 20, 2017 6:25 pm

Got my wemos d1 mini working with the HC-SR04.

Also change the code to depend on the air temperature.

http://profpedrodias.pythonanywhere.com ... C-SR04.jpg
http://profpedrodias.pythonanywhere.com ... rasonic.py
http://profpedrodias.pythonanywhere.com/ESP8266/test.py


from ultrasonic import *

gpio = {"TX":01, "RX":03, "D0": 16, "D1": 05, "D2": 04,
"D3": 00, "D4": 02, "D5": 14, "D6": 12, "D7": 13, "D8": 15}

Trigger = gpio[ "D6" ]
Echo = gpio[ "D5" ]

air_temp = 12 # read from DS18B20

sensor = Ultrasonic( Trigger, Echo, air_temp)

DEBUG = True
LIMITE = 2
dist = 0

while True and LIMITE:
if DEBUG: LIMITE -= 1

# Get reading from sensor
dist = sensor.distance_in_cm()
print(dist)
time.sleep_ms(100)
Last edited by jpedrodias on Fri Jan 27, 2017 10:50 am, edited 1 time in total.

User avatar
dbalnaves
Posts: 7
Joined: Thu Jan 05, 2017 12:53 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by dbalnaves » Fri Jan 27, 2017 8:23 am

Sorry I missed your post! I wish I had have known as I just recently got that board working!!!

So, the big secret I can reveal is that I got it working on 3.3V just fine instead of the prescribed 5V.

Here is the quick and dirty code I wrote (and tested with paste-in mode):

Code: Select all

import machine
import time

while True:
  trig=machine.Pin(5,machine.Pin.OUT)
  trig.low()
  time.sleep_ms(2)
  trig.high()
  time.sleep_ms(10)
  trig.low()
  echo=machine.Pin(0,machine.Pin.IN)
  while echo.value() == 0:
    pass
  t1 = time.ticks_us()
  while echo.value() == 1:
    pass
  t2 = time.ticks_us()
  cm = (t2 - t1) / 58.0
  print(cm)
I noticed my code crashes if I put my finger too close.

User avatar
dbalnaves
Posts: 7
Joined: Thu Jan 05, 2017 12:53 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by dbalnaves » Fri Jan 27, 2017 8:45 am

Actually, sorry - I just read the thread and this has already been discussed in exhaustive depth :oops:

filipmar.mf
Posts: 3
Joined: Thu Jul 28, 2016 3:21 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by filipmar.mf » Fri Jan 27, 2017 12:34 pm

Hi,
here is my module with calibration feature:

https://github.com/Euter2/MicroPython/b ... rasonic.py

jpedrodias
Posts: 3
Joined: Fri Jun 24, 2016 10:27 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by jpedrodias » Fri Jan 27, 2017 7:50 pm

[quote="filipmar.mf"]Hi,
here is my module with calibration feature:

https://github.com/Euter2/MicroPython/b ... rasonic.py[/quote]
:shock: :idea: :D

VERY VERY NICE Work!!!

BUT I change line 11 and 12 to:
self._trig = Pin(trig_Pin)
self._echo = Pin(echo_Pin)

That whay I dont need to load "from machine import Pin" from the loading file
sensor = Ultrasonic( Trigger_integer, Echo_integer )


The DATA from this code IS also very very coherent ...

jpedrodias
Posts: 3
Joined: Fri Jun 24, 2016 10:27 pm

Re: Module for Ultrasonic sensor (HC-SR04)

Post by jpedrodias » Sat Jan 28, 2017 9:32 pm

filipmar.mf wrote:Hi,
here is my module with calibration feature:

https://github.com/Euter2/MicroPython/b ... rasonic.py

I Getting this error: OSError: [Errno 110] ETIMEDOUT

on line 26 .. any one got this error?


I working around that:
try:
dist = sensor.distance()
except:
print('ERROR: ultrasonic.sensor.distance')
dist = 0.01

User avatar
raspi3ua
Posts: 21
Joined: Sat Mar 18, 2017 4:49 pm
Location: Kiev, Ukraine
Contact:

Re: Module for Ultrasonic sensor (HC-SR04)

Post by raspi3ua » Fri Mar 24, 2017 2:38 pm

filipmar.mf wrote:Hi,
here is my module with calibration feature:

https://github.com/Euter2/MicroPython/b ... rasonic.py
Correct one. Connect distance sensor HC-SR04 to NodeMcu ESP8266 V3.

>>> import machine
>>> trig=machine.Pin(16,machine.Pin.OUT)
>>> echo=machine.Pin(0,machine.Pin.IN)
>>> sensor = ultrasonic2.Ultrasonic(trig, echo)
>>> while True:
... print(sensor.distance())
... time.sleep(0.1)
...
...
...
1.20785
1.20853
1.20887
0.7697597
0.7734997
0.7605798
0.7768998
1.21363
1.209209
1.209209
1.209209
1.21788

User avatar
raspi3ua
Posts: 21
Joined: Sat Mar 18, 2017 4:49 pm
Location: Kiev, Ukraine
Contact:

Re: Module for Ultrasonic sensor (HC-SR04)

Post by raspi3ua » Fri Mar 24, 2017 2:53 pm

Just for info.
Take output signal on trig and echo pins using oscilloscope.

No software delay between trig high - low. But we can see near 80us TRIG signal on oscilloscope.
>>> while True:
... trig.high(); trig.low(); time.sleep_ms(60)
trig_no_delay.jpg
trig_no_delay.jpg (251.19 KiB) Viewed 5774 times
10 us software delay between trig high - low. But we can see near 220us TRIG signal on oscilloscope.
>>> while True:
... trig.high(); time.sleep_us(10); trig.low(); time.sleep_ms(60)
trig_10us1.jpg
trig_10us1.jpg (251.11 KiB) Viewed 5774 times
100 us software delay between trig high - low. But we can see near 320us TRIG signal on oscilloscope.
>>> while True:
... trig.high(); time.sleep_us(100); trig.low(); time.sleep_ms(60)
trig_100us1.jpg
trig_100us1.jpg (247.64 KiB) Viewed 5774 times

User avatar
raspi3ua
Posts: 21
Joined: Sat Mar 18, 2017 4:49 pm
Location: Kiev, Ukraine
Contact:

Re: Module for Ultrasonic sensor (HC-SR04)

Post by raspi3ua » Fri Mar 24, 2017 3:01 pm

200 us software delay between trig high - low. But we can see near 400us TRIG signal on oscilloscope.
>>> while True:
... trig.high(); time.sleep_us(200); trig.low(); time.sleep_ms(60)
trig_200us.jpg
trig_200us.jpg (245.49 KiB) Viewed 5772 times

Lets look on ECHO pin.
On ECHO pin we can see near 7ms signal on oscilloscope when distance to object =120cm.
echo_distance120cm.jpg
echo_distance120cm.jpg (240.58 KiB) Viewed 5772 times

So, 'us' time in micropython is "fantom" :-) Micropython is too high-level language to measure 'us'.

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

Re: Module for Ultrasonic sensor (HC-SR04)

Post by RajaRamesh » Wed Jun 05, 2019 7:16 am

raspi3ua wrote:
Fri Mar 24, 2017 2:38 pm
filipmar.mf wrote:Hi,
here is my module with calibration feature:

https://github.com/Euter2/MicroPython/b ... rasonic.py
Correct one. Connect distance sensor HC-SR04 to NodeMcu ESP8266 V3.
Hi, After uploading ultrasonic.py on to ESP8266 .I used below code and output generate as shown below. was the output display in meters(m)?

>>> import machine
>>> trig=machine.Pin(14,machine.Pin.OUT)
>>> echo=machine.Pin(12,machine.Pin.IN)
>>> sensor = ultrasonic2.Ultrasonic(trig, echo)
>>> while True:
... print(sensor.distance())
... time.sleep(5)
...
...
...
-0.00034
0.00034
0.00051
0.00051
0.00065
0.00068
0.00068
0.00068
0.00068
0.00085

Post Reply