From a1dad5fb5a30e954943831c357a468fb26d08987 Mon Sep 17 00:00:00 2001 From: Alan Wang <44191076+alankrantas@users.noreply.github.com> Date: Fri, 3 Apr 2020 23:35:22 +0800 Subject: [PATCH] Update README.md --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b138f2..53b7f0b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [BBC micro:bit MicroPython documentation](https://microbit-micropython.readthedocs.io/en/latest/index.html#) -This is my notes, tricks and experiments about BBC micro:bit and MicroPython. +This is the collection of my notes, tricks and experiments about BBC micro:bit and MicroPython. ## Easer Eggs @@ -217,7 +217,7 @@ while True: Do not use servos and buzzers at the same time. They require different PWM frequencies and most microcontrollers can only set one frequency accross all pins at a time. -micro:bit's power output may just enough to power a SG90 mini servo. External power supply is recommended. +Also: micro:bit's power output may just (barely) enough to power a single SG90 mini servo. External power supply recommended. ## Value Mapping @@ -302,3 +302,46 @@ while True: ledRotate() sleep(50) ``` + +## HC-SR04 Ultrasonic Sensor + +Get detected distance from HC-SR04/HC-SR04P sonar sensors. Set the parameter unit to 'cm' or 'inch'. External power supply recommended. + +```python +from machine import time_pulse_us +from utime import sleep_us +from microbit import pin1, pin2, display, Image, sleep + +def sonar(trig_pin, echo_pin, unit='cm'): + + trig_pin.write_digital(0) + sleep_us(2) + trig_pin.write_digital(1) + sleep_us(10) + trig_pin.write_digital(0) + + while echo_pin.read_digital() == 0: + pass + + duration = time_pulse_us(echo_pin, 1, 30000) + + if unit == 'cm': + return duration / 2.0 * 0.03313 + elif unit == 'inch': + return duration / 2.0 * 0.01304 + else: + return duration + + +while True: + + distance = sonar(trig_pin=pin1, echo_pin=pin2, unit='cm') + print(distance) + + if 2 <= distance <= 20: + display.show(Image.YES) + else: + display.clear() + + sleep(100) +```