Hardware:The other components we use are:
- Edge Connector Break Out Board for BBC microbit.
- BBC microbit, USB Cable and Battery Pack.
- Prototyping Breadboard and Mounting Plate (available in our Inventor's Kit).
- Jumper Wires (Female to Male).
- Red LED.
- Yellow LED.
- Green LED.
- 3xAAA Battery Pack with JST Connector.
- HC-SR04 3V.
- HC-SR04 5V.
Distance Sensing With A microbit And HC-SR04 Module:Laying out the HC-SR04 ultrasonic sensor is quite simple, especially with the 3V sensor. It only has four pins. Left to right these are VCC, Trig, Echo and GND. VCC and GND are for power and Trig, Echo and GND are for your signal. GND is included in with the signal set since it's needed for a baseline. One of my common early mistakes with sensors and things like smart LEDs was not connecting all components to a common ground. I've included a picture and a schematic of my set up. The easiest pins to use on the microbit are 0, 1, and 2. So I used 0 for Trig and 1 for Echo. Adding the battery pack adds only a little complexity, it's worth noting that we have removed the power from the microbit and connected the black wires (GND or Ground) together. This gives the circuit a common baseline 0V, which it is worth noting again, is important when measuring electronic signals. Once the hardware is assembled you can set up your software.
Software:As you might be aware, the microbit is programmed in the browser, you can head to https://makecode.microbit.org/ to start programming your sonic sensor. First, you'll need to add a new package to the standard set. I've used an extension called Sonar, extensions were previously called packages. Finding the Sonar extension is done by a simple search and then just click add it to your blocks and set up your code as is shown here. Use the bargraph command to display the sensor data (the variable called item) because it's a simple way to show that the sensor is working. More info on bargraph can be found here: https://makecode.microbit.org/reference/led/plot-... Although amazingly I had to look at the code on GitHub to find out that leaving the bottom value as 0 allows it to auto adjust. Change this to set your maximum point that you want to measure. It will give data in whole numbers (integers) and in the units you select. I believe the sonar block is doing the conversion calculations for us. By switching to print the sensor data to screen, you can begin to get a feel for the distances you wanted to measure and code for. If you're following along you'll have noticed that there are a lot of zero values, which cause a flickering of bargraph or other output. I thought it was easiest to filter these out with an if statement (found in logic). This was also the easiest way I found to work using LEDs like ZIP LEDs.
More Hardware:Finally, and my favourite part of this project is adding some outputs, here we use some LEDs like a traffic light. Setting up the LEDs as shown: And then make the code as shown here: You can quickly make a distance sensor and feedback system! With all being correct you can now also experiment with the code and even add some more inputs. Additionally, a servo motor would be great for pushing away items!
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.