Page 1 of 2

Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Tue Mar 21, 2017 11:46 am
by fizban
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,

The US-100 has two modes of working: pulse width and serial. The first one is equivalent to the HC-SR04. There is one library for the serial mode and another one for the pulse width mode.

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.

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sat Apr 01, 2017 5:51 pm
by BruinBear
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:

Code: Select all

def distance_cm(self):
But in the examples (2.1 and \Sample) you have:

Code: Select all

distance = round(sonar.distance_mm()/10)

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sat Apr 01, 2017 7:40 pm
by BruinBear
Actually, I just tried the same setup with pxt.microbit.org and the readings obtained using the Sonar package are underestimated by about 50%!

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sat Apr 01, 2017 7:49 pm
by deshipu
Make sure you are powering the sensor with 5V -- that's what it requires for functioning properly.

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sat Apr 01, 2017 9:16 pm
by BruinBear
Yes, dual power supply 5v->sensor 3.3v->micro:bit

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sat Apr 01, 2017 10:27 pm
by fizban
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...

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sun Apr 02, 2017 4:02 pm
by BruinBear
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%.

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sun Apr 02, 2017 4:36 pm
by fizban
Interesting. Are you connecting the echo pin directly to the microbit or with a divider? I wonder if an echo pulse of 5V might be causing problems in the sampling...

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sun Apr 02, 2017 5:50 pm
by BruinBear
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.

Re: Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries

Posted: Sun Apr 02, 2017 6:37 pm
by fizban
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...