Skip to content

Commit 324d44e

Browse files
committed
add sonic measure
1 parent ab8fd97 commit 324d44e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,27 @@ Examples for micropython at NodeMCU
1313

1414
led_on.py : light LED
1515
GPIO setting, OUT
16+
Pin:D0
1617

1718
flash_key : pushing the flash button will light LED
1819
GPIO setting, IN, Timer w/ polling
20+
Pin:D3
1921

2022
led_breath.py : make LED light and dim
2123
software PWM, Timer, w/ delay
2224
may cause reset w/o correct setting
25+
Pin:D0
2326

2427
led_breath_timer.py : make LED light and dim
2528
software PWM, Timer
2629
set each period w/ timer
30+
Pin:D0
2731

2832
stepper.py : use 28BYJ-48 5V DC stepper
33+
Pin:D1, D2, D3, D4
34+
35+
sonic.py : use HC-SR04 to measure distance
36+
Pin:D5, D6
37+
2938

3039
```

sonic.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from pyb import Pin
2+
from time import sleep_ms, sleep_us, ticks_us
3+
from machine import Timer
4+
5+
D5 = Pin(14, Pin.OUT)
6+
D6 = Pin(12, Pin.IN)
7+
Trig = D5
8+
Echo = D6
9+
10+
def measure():
11+
Trig.low()
12+
sleep_us(15)
13+
Trig.high()
14+
sleep_us(15)
15+
Trig.low()
16+
while(Echo.value() == 0):
17+
start = ticks_us()
18+
while(Echo.value() == 1):
19+
end = ticks_us()
20+
duration = end - start
21+
dist = 0.03435 * 0.5 * duration
22+
print("duration: " + str(duration) + ", " + \
23+
"distance: " + str(dist))
24+
return dist
25+
26+
tim = Timer(0)
27+
tim.deinit()
28+
tim.init(period=200, mode=Timer.PERIODIC, callback=lambda t:measure())
29+
#

0 commit comments

Comments
 (0)