How to use the ultrasonic sensor with Micro: bit V2 board
Hello and welcome back. In this tutorial, we will learn how to use the ultrasonic sensor with a Micro: bit V2 board. Also, I have used the LCD screen for displaying the ultrasonic sensor readings, but you can skip that part if you want. With the ultrasonic sensor, we can measure the distance in front of an object. If you want more details about this sensor, check out this link. Also, you can use this knowledge for making obstacle avoidance robots, digital rulers, radar systems and so many other projects. It all depends on your skills. Don’t worry; you can find any type of project on our website.
- What is the Micro: bit and how to use it – Click on me
OK, let’s do this project step by step. The required components are given below.
- Micro: bit V2 or V1 board x 1 — Our store / Amazon
- Micro: bit sensor shield x 1 — Our store / Amazon
- Ultrasonic sensor x 1 — Our store / Amazon
- LCD screen x 1 — Our store / Amazon
- I2C module x 1 — Our store / Amazon
- Jumper wires — Our store / Amazon
- Keyestudio BBC Micro: bit starter kit — Click on me
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Secondly, connect the Micro: bit board to the shield.
Step 3
Thirdly, connect the Ultrasonic sensor and LCD screen to the sensor shiels. For that, use the circuit diagram below.
Step 4
Now let’s make the program for this project. For that, follow the instructions below.
- First, go to the MakeCode website and create a new project.
- MakeCode — Click on me
- Next, connect the Micro: bit board to the MakeCode IDE. For that, use the images below.
- Then, install the Sonar and LCD extensions.
- Now, copy and paste the following Python script on the Python tab. Then, you will see the auto-generated code blocks. You can also make it without my script.
- Script and circuit diagram — Download
distance = 0
makerbit.connect_lcd(39)
def on_forever():
global distance
distance = sonar.ping(DigitalPin.P0, DigitalPin.P1, PingUnit.CENTIMETERS)
basic.pause(100)
makerbit.show_string_on_lcd1602("Distance:", makerbit.position1602(LcdPosition1602.POS1), 16)
makerbit.show_string_on_lcd1602("" + str(distance) + "cm",
makerbit.position1602(LcdPosition1602.POS10),
16)
makerbit.show_string_on_lcd1602("Distance:",
makerbit.position1602(LcdPosition1602.POS17),
16)
makerbit.show_string_on_lcd1602("" + str(int(distance * 0.393701)) + "inch",
makerbit.position1602(LcdPosition1602.POS26),
16)
basic.forever(on_forever)
- Finally, click the download button.
Step 5
Now, remove the USB cable and connect the external power supply. Also, connect the jumper to the 5V-V2 pin.
Step 6
Finally, adjust the contrast value using the potentiometer. Now you can see the distance on the LCD. The full video guide is below. So, we hope to see you in the next project.
How to use the ultrasonic sensor with Micro: bit V2 board