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

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

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

Post by fizban » Tue Mar 21, 2017 11:46 am

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.

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

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

Post by BruinBear » Sat Apr 01, 2017 5:51 pm

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)

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

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

Post by BruinBear » Sat Apr 01, 2017 7:40 pm

Actually, I just tried the same setup with pxt.microbit.org and the readings obtained using the Sonar package are underestimated by about 50%!

User avatar
deshipu
Posts: 1347
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Sat Apr 01, 2017 7:49 pm

Make sure you are powering the sensor with 5V -- that's what it requires for functioning properly.

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

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

Post by BruinBear » Sat Apr 01, 2017 9:16 pm

Yes, dual power supply 5v->sensor 3.3v->micro:bit

fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

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

Post by fizban » Sat Apr 01, 2017 10:27 pm

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...

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

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

Post by BruinBear » Sun Apr 02, 2017 4:02 pm

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%.

fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

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

Post by fizban » Sun Apr 02, 2017 4:36 pm

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...

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

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

Post by BruinBear » Sun Apr 02, 2017 5:50 pm

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.

fizban
Posts: 24
Joined: Mon Oct 24, 2016 2:32 pm

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

Post by fizban » Sun Apr 02, 2017 6:37 pm

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...

Post Reply