Lesson 6 – Obstacle Sensing using an Ultrasonic Sensor (HC-SR04)
Objective – As part of this tutorial we will learn howto sense obstacles in front of the robot.
The image below shows the Micro:Maqueen robot with an ultrasonic (HC-SR04) sensor attached to it. Here’s what the ultrasonic sensor looks like.
Please note that the Ultrasonic sensor is not part of the robotic kit and needs to be purchased separately.
The ultrasonic (HC-SR04) sensor can be used to detect obstacles in front it, measure the distance from the object and take action based on the distance. In this tutorial we will attempt to use the ultrasonic sensor (HC-SR04) to measure the distance of obstacles in front of it. The HC-SR04 costs ~3-4 AUD and can be purchased online from most electronics hobby stores.
Setting things up – Let’s head over to the BBC MakeCode website (https://makecode.microbit.org) since we will be using the MakeCode block editor to code the Micro:Maqueen.
Before we can write any code and get our robot to do stuff for us we need to include some specific libraries (Code) that will allow the BBC Micro:Bit board to understand the different components, capabilities of the robot. We call this special code libraries.
Here’s the steps to import the library –
- Head over to the MakeCode website at : makecode.microbit.org using a new browser window. The first time it loads things might be a bit slow.
- Let’s create a new project to get started.
- Now go ahead and let’s import the library.
- Click the gear icon shown on the right hand side of your screen, click on “Extensions” and then enter the library address: https://github.com/jhlucky/maqueen
- Import the library by clicking on it.
Let’s Get Coding – To be able to execute this code you will need to insert the ultrasonic sensor (HC-SR04) into the pin slots provided at the front (top) of the Micro:Maqueen. This assembly is really simple and all it requires is for the sensor with it’s 4 pins to be pushed into the 4 pin slot on the Micro:Maqueen robotic board (top front). Please make sure you just use enough pressure and not too much pressure else you could damage the robot including the sensor.
At the end of this tutorial you should be able to power up the robot and measure any obstacles that come in front of the robot.
Enter the code provided below into your MakeCode Editor where you have loaded the Micro:Maqueen libraries.
Upload the code to the micro:bit and you should be able to start seeing distances of objects (in cm’s) displayed on the micro:bit. You can try measuring distances with different types of objects and see the accuracy of the ultrasonic sensor (HC-SR04).
Have a look at the sample program at – https://makecode.microbit.org/_4gi4Dj7yTWgK
Challenge – With the Ultrasonic sensor now mounted on the Micro:Maqueen robot you can now detect obstacles, measure the distance of the obstacle from the robot and make decisions on what you want to i.e. step back, stop, turn right or turn left. You challenge is to bring together all the learning you’ve gathered so far and add the obstacle detection and measurement capability to your project.
Your challenge is as follows – Make the robot dance with fast moves, program the lights under it to light up, light up the LED’s on the front of the robot and use the Ultrasonic sensor to identify objects in front of the robot. You can consider adding the following movement sequences for the dance –
- Turn right for 1 sec
- Reverse backwards for 1 sec
- Turn left for 1 sec
- Reverse backwards for 1 sec
- Move forwards for 1 sec
- Move backwards for 1 sec
- Stop for 2 seconds
- Repeat the above sequence 2 more times
In this challenge we bring to together our learning from all the previous tutorials i.e. robot movement, lighting up the neopixels underneath the robot,y lighting up the LED’s in front of the robot and obstacle detection using the ultrasonic sensor (HC-SR04).
- This development track makes extensive use of the BBC micro:bit.
- If you haven’t purchased a BBC micro:bit yet you might want to head over to OzToyLib and pick one up now.
- We would recommend that you pick up the BBC micro:bit kit which comes with a battery pack, usb cable and the real micro:bit.
- We will be using the official online BBC MakeCode Block editor to write code for all our tutorials. No investment required here.
- You will also need a Micro:Maqueen STEM robot to be able to work on this development track. You can pick up the Micro:Maqueen robot from OzToyLib.
- To learn more about the BBC micro:bit please visit – <Link>
- To learn more about global re-sellers of the BBC micro:bit please please visit – <Link>
- To learn more about Kitronik please visit – <Link>