Ultrasonic (US-100/HC-SR04) and sound sensor (KY038) libraries
Posted: 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.
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.