Module for Ultrasonic sensor (HC-SR04)
-
- Posts: 3
- Joined: Fri Jun 24, 2016 10:27 pm
Re: Module for Ultrasonic sensor (HC-SR04)
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)
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.
Re: Module for Ultrasonic sensor (HC-SR04)
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):
I noticed my code crashes if I put my finger too close.
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)
Re: Module for Ultrasonic sensor (HC-SR04)
Actually, sorry - I just read the thread and this has already been discussed in exhaustive depth
-
- Posts: 3
- Joined: Thu Jul 28, 2016 3:21 pm
Re: Module for Ultrasonic sensor (HC-SR04)
Hi,
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py
-
- Posts: 3
- Joined: Fri Jun 24, 2016 10:27 pm
Re: Module for Ultrasonic sensor (HC-SR04)
[quote="filipmar.mf"]Hi,
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py[/quote]
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 ...
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py[/quote]
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 ...
-
- Posts: 3
- Joined: Fri Jun 24, 2016 10:27 pm
Re: Module for Ultrasonic sensor (HC-SR04)
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
Re: Module for Ultrasonic sensor (HC-SR04)
Correct one. Connect distance sensor HC-SR04 to NodeMcu ESP8266 V3.filipmar.mf wrote:Hi,
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py
>>> 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
Re: Module for Ultrasonic sensor (HC-SR04)
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) 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) 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)
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) 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) 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)
Re: Module for Ultrasonic sensor (HC-SR04)
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)
Lets look on ECHO pin.
On ECHO pin we can see near 7ms signal on oscilloscope when distance to object =120cm.
So, 'us' time in micropython is "fantom" Micropython is too high-level language to measure 'us'.
>>> while True:
... trig.high(); time.sleep_us(200); trig.low(); time.sleep_ms(60)
Lets look on ECHO pin.
On ECHO pin we can see near 7ms signal on oscilloscope when distance to object =120cm.
So, 'us' time in micropython is "fantom" Micropython is too high-level language to measure 'us'.
-
- Posts: 51
- Joined: Wed May 08, 2019 10:54 am
Re: Module for Ultrasonic sensor (HC-SR04)
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)?raspi3ua wrote: ↑Fri Mar 24, 2017 2:38 pmCorrect one. Connect distance sensor HC-SR04 to NodeMcu ESP8266 V3.filipmar.mf wrote:Hi,
here is my module with calibration feature:
https://github.com/Euter2/MicroPython/b ... rasonic.py
>>> 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