I have created two libraries to use ultrasonic sensors with the micro:bit and micropython. Besides, there is a third library to help with the calibration of the sound sensor KY-038.

Since I have not been able to find a micropython example for the micro:bit to be use an ultrasonic sensor, I used my own approach. I only have an US-100 so I am not sure it will also work with the HC-SR04, although I guess it should with the proper voltage divider. Hopefully someone can confirm if it works with the HC-SR04,

In the pulse widh mode, we need to measure the width in microseconds, and the microbit has a hardware device that can be used for this: the SPI device. It allows to detect high/low pulses at frequencies higher than 1 megahertz. We do not really need that granularity since there other sources of error. I found that 125.000Hz was enough to get a decent measure.

In serlal mode, it is just a question of issuing the proper command to the sonar and it will return either the distance or the temperature.

For the sound sensor library, I used a KY-038, but again, I guess that a KY-037 should also work (again, hopefully someone can confirm this point). It helps with the calibration by showing an arrow in the direction that the potentiometer has to be turned. It also has a function to count the number of "claps" detected.

fizban
I tried it with an HC-SR04 and it works but I'd say it was undermeasuring by around 10%. Using the same module with a Picaxe microprocessor, the results (between 1cm and 1.5m) were accurate to around 1% I'd say.

PS There are typos in your example scripts on GitHub. In the library you have:

Thanks for testing. I just uploaded the version with mm. Apparently, I still had it on my computer and had not uploaded it.

Good to know that my library is more accurate than the pxt one, since until now the pxt option was the recommended option for using this sensor.

The US-100, the one I have, iis not that accurate in echo mode, so I was not able to tell much of a difference. To improve accuracy, maybe you can double the spi baudrate and double the result as well...

I tried doubling the SPI frequency but it didn't seem to make any noticeable difference.

I also tried the same code with the SRF005 ultrasonic sensor and it was slightly more accurate, especially at distances up to 10cm where it was only around 3% short. At 30cm, accuracy went down to about 90%.

I've got two power supplies with a common ground. The 5v supply just connects to the 5v and GND pins on the sensor. The micro:bit is powered from the 3.3V supply and the trig and echo pins on the sensor are connected to the micro:bit i/o pins.

I believe the micro:bit digital pins expects 3.3V for a logic 1. I do not think you will damage the micro:bit with a 5V logic in the short term, but it might be causing undesirable results. If we assume there is an internal pull down of 12K, maybe you can try putting a resistor between 5K and 12K between the echo pin and the micro:bit to lower the logic 5V to something between 2.5 and 3.3... Another reason to use the US-100, which works fine with 3.3V...