All Projects → fizban99 → microbit_hcsr04

fizban99 / microbit_hcsr04

Licence: MIT License
micro:bit library for the HC-SR04 ultrasonic sensor

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to microbit hcsr04

smart-garden-ornaments
The smart garden ornaments project! 🦩🌱🤖
Stars: ✭ 20 (+33.33%)
Mutual labels:  microbit
Electronic-Cheat-Sheet-and-Schematics-MegaCollection
A lot of Files of various Electronic Shit that I have collected over the years. Cheatsheet, Schematics, Pinouts, Pdf, and More... Enjoy it ;)
Stars: ✭ 43 (+186.67%)
Mutual labels:  microbit
pxt-bluetooth-gamepad
BLE HID Gamepad module for micro:bit
Stars: ✭ 20 (+33.33%)
Mutual labels:  microbit
pxt-neopixel
A Neo-Pixel package for pxt-microbit
Stars: ✭ 47 (+213.33%)
Mutual labels:  microbit
microrust
Learning embedded development with Rust on the micro:bit
Stars: ✭ 52 (+246.67%)
Mutual labels:  microbit
Python-For-Kids
A comprehensive and FREE Online Python Development tutorial FOR KIDS utilizing an official BBC micro:bit Development Board going step-by-step into the world of Python for microcontrollers.
Stars: ✭ 621 (+4040%)
Mutual labels:  microbit
pxt-minode
mi:node kit(micro:bit IoT Starter Kit by element14) driver package for PXT/microbit
Stars: ✭ 25 (+66.67%)
Mutual labels:  microbit
microbit-ble
Read data from micro:bit using Bluetooth from Linux
Stars: ✭ 40 (+166.67%)
Mutual labels:  microbit
pxt-calliope
A Microsoft MakeCode editor for the Calliope Mini board
Stars: ✭ 33 (+120%)
Mutual labels:  microbit
openblock-desktop
Graphic programming software for hardware like: arduino, microbit, esp32, esp8266...
Stars: ✭ 161 (+973.33%)
Mutual labels:  microbit
hcsr04
hcsr04 full function driver
Stars: ✭ 25 (+66.67%)
Mutual labels:  hcsr04
zig-bare-metal-microbit
Bare metal microbit program written in zig
Stars: ✭ 27 (+80%)
Mutual labels:  microbit
tinyfont
Text library for TinyGo displays
Stars: ✭ 37 (+146.67%)
Mutual labels:  microbit
pxt-linebeacon
LINE Beacon for micro:bit
Stars: ✭ 28 (+86.67%)
Mutual labels:  microbit
microbit ssd1306
Simple micropython library for the micro:bit to control the SSD1306 display
Stars: ✭ 47 (+213.33%)
Mutual labels:  microbit
blockly
Otto Blockly; a fully integrated graphical programming for any type of Arduino projects, including robots, ready to install in your computer, it works offline and also online
Stars: ✭ 85 (+466.67%)
Mutual labels:  microbit
pxt-powerfunctions
MakeCode extension for controlling your LEGO Power Functions devices with an IR-emitting LED
Stars: ✭ 44 (+193.33%)
Mutual labels:  microbit
Pxt
Microsoft MakeCode (PXT - Programming eXperience Toolkit)
Stars: ✭ 1,649 (+10893.33%)
Mutual labels:  microbit
Tinygo
Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
Stars: ✭ 9,068 (+60353.33%)
Mutual labels:  microbit
pyscrlink
Scratch-link for Linux written in python
Stars: ✭ 88 (+486.67%)
Mutual labels:  microbit

Basic micropython library for the micro:bit to read the distance from an ultrasonic sensor

This library allows the micro:bit to read the distance from an ultrasonic sensor HCSR04 or similar.

It uses the SPI hardware internal device to measure the length of the returning echo, so by default you should connect the sonar echo pin to micro:bit pin 14 and the sonar trigger pin to micro:bit pin 15. The HC-SR04 works with 5V, so you should protect the micro:bit input with a couple of resistors to create a voltage divider (see, e.g. http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/). Note that you can use a US-100 with this library as well. The US-100 has the advantage that works directly with 3V, so it does not require a voltage divider and you can connect it directly to the micro:bit. Note that the US-100 has a serial mode that provides better readings, so it is better to use the other library with it.

ultrasonic.png

These sensors look like robot eyes. In fact, one eye is an emitter and the other is a receiver. The sensor is triggered with a pulse of around 10 microseconds. When it gets the pulse from the micro:bit, it sends an ultrasonic tone through one of the "eyes". The other eye detects the reflection of the sound. The sensor generates a pulse as wide as the time it took for the frequency to be detected. So the width of the "echo" pulse is equivalent to the time it takes the sound to reach the object and come back, which is twice as much as the distance to the object.

spi1.png

The library uses the internal hardware spi device to measure the echo. SPI works by using one pin to set the clock speed, with pulses at the required frequency. Another pin is used to transmit bits at each clock cycle and the last pin is used to receive the bits from the other device. The library sends a pulse through MOSI and waits to receive something through MISO. Then, it measures the length of the returning pulse by counting the equivalent "bits".

spi2.png

Since the information is actually grouped in bytes, we need to identify the first time we receive something different than 0x00 and count its bits (our "preamble"), then wait until we receive something different than 0xFF (hexadecimal for the binary 11111111), which will be our "postamble" and then count the total nomber of bits as preamble + 8 x bytes in the middle + postamble.

1   Main features

  • Get the distance in cm from the sonar to an object.
  • Sample program.

2   Library usage

2.1   distance_cm()

Get the distance in mm.

from hcsr04 import HCSR04
from microbit import sleep


sonar=HCSR04()
while True:
    print('%.1f' % (sonar.distance_mm()/10))
    sleep(1000)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].