3.2.4
Room Roamer
Room Roamer
Echolocation in nature helps an animals survive by finding food and avoiding obstacles.
Ultrasonic sensors mimic echolocation used by bats, transmitting high-frequency sound waves to gauge the distance between objects within close range.
Credit: Automotive Engineering
What to expect
In today’s activity, we will use the MOVE motor car’s ultrasonic distance sensor to make the car drive around the room automatically while avoiding walls and obstacles. We will also use the serial monitoring function to read the input from the distance sensor and adjust the code accordingly.
Connections
The distance sensor will be used in future activities to add even more capabilities to the MOVE motor car. Distance sensors are also used in many real-world applications. For example, cars with adaptive cruise control (or self-driving mode) use distance sensors to maintain safe following distances and avoid collisions. Distance sensors are also used in manufacturing processes and production lines, so that a robot can find and manipulate parts on the production line.
Materials
Kitronik MOVE motor car with micro:bit
USB cable
Computer
Instructions
The ultrasonic distance sensor is the board at the front of the vehicle with two small cylinders. One of the cylinders (labeled with a “T” in the corner of the board) transmits a high-frequency ultrasonic signal. The signal is reflected by any obstacle in front of it, and the other cylinder (labeled with an “R”) receives the signal. Since the speed of the signal is roughly constant (the speed of sound through air is about 343 m/s), the micro:bit uses the time delay between the transmitted and received signal to calculate the distance from the obstacle.
Just like last week, this activity will have two parts. In Part 1, we will use serial monitoring to view the input from the distance sensor. In Part 2, we will use that information to program the micro:bit and make a “room roamer” vehicle that automatically avoids obstacles.
Part 1. View input from the distance sensor with serial monitoring
Like last week, it is helpful to start by viewing the input received from the distance sensor. We can do this with the micro:bit’s serial monitoring feature.
Start by creating a new Micro:bit project (https://makecode.microbit.org/#editor). Name it Room Roamer or something similar.
Figure 1. MOVE Motor extension
If you don’t already have the “MOVE motor” extension in the MakeCode menu as shown below, add it by clicking on the gray Extensions button, then search for “MOVE Motor”, then click on the tile labeled “kitronik-move-motor”. You should now see the MOVE Motor extension in the menu.
Figure 2. Serial monitoring menu
2. Next, click on the Advanced menu, and then Serial
Figure 3. Serial monitoring menu
3. Next, create a program that looks like the code below. The green blocks are from the MOVE motor menu (under the Sensors sub-menu), and the dark blue block is from the Serial menu.
Figure 4. Button to show live serial monitoring data
4. Insert your micro:bit into the MOVE motor car with the micro:bit’s A and B buttons facing forward. Connect the micro:bit to the computer with the USB cable, and download the code. Do not unplug the USB cable.
Turn the MOVE motor car’s power switch to “ON”.
On the left side of the screen, click on the “Show data Device” button (not the Show data Simulator button).
Figure 5. Serial monitoring of sound level received by the micro:bit microphone
5. The screen should show a live readout of distances received from the distance sensor to the micro:bit, similar to Figure 5.
Find a textbook, notebook, or a folder (or something similarly sized) to use as a test “obstacle”. Move this obstacle to various distances in front of the ultrasonic sensor, and observe the input from the sensor. As you move the obstacle closer and then farther from the ultrasonic sensor, you should see the distance measurement change. The values will be shown on the plot at the top of the screen, and displayed in the table below. Your data may look similar to Figure 5. Notice that the curve is not perfectly smooth. There are occasional “blips” in the data when the signal reflects from something other than the intended obstacle (perhaps something in the background).
Part II. Program the Room Roaming Car
Next we will build the code for the vehicle. We will do this by using the distance sensor to measure the distance to obstacles in front of the car, and then turning the car to avoid them.
Create a new MakeCode program, or, simply edit the program you created in Part 1.
Figure 6. Make a new variable called distance.
Click on the Variables menu, then click Make a variable. Name your new variable distance. We will use this variable to store the distance value received from the ultrasonic sensor.
Figure 7. Code for Room Roamer. Link to code: https://makecode.microbit.org/_JwLgHJcU9CYE
3. Now create the following code shown in Figure 7. This code will first check the distance to the nearest obstacle. If the distance to the nearest obstacle is less than 15 cm, it will spin to the right for one-half second at 25% speed (about 90 degrees) and re-check the distance to the nearest obstacle. If it still detects an obstacle, it will continue spinning to the right. If it does not detect an obstacle less than 15 cm away, it will drive forward at 25% speed.
If you can’t find a code block, remember that the color of the block tells you which menu the block is located in. You can also use the Search feature at the top of the block menus.
Figure 8. If your car curves left or right when it should drive straight ahead, edit the code to control the speed of each motor individually using two “turn ___ motor on direction ___ speed ___” code blocks, one for each motor, instead of the “move ___ at speed __” block.
4. Connect the micro:bit to your computer and download the code. Then disconnect the micro:bit and insert it into the MOVE motor car (if not already inserted) with the A and B buttons facing forward.
Make sure the car has batteries installed. Then set the car on the floor, and turn the car’s power switch to ON.
Your car should start moving on its own. If your car doesn’t drive straight ahead, revise your code to use two motor control blocks “turn ___ motor on direction ___ speed ___” to control the speed of each motor individually, instead of the “move ___ at speed __” block.
Optional Extension
The code in this activity only allows the vehicle to turn right. If the only clear path for the vehicle is to the vehicle’s left, the vehicle will find it by turning right 270 degrees instead of turning left 90 degrees.
Can you create a code where the vehicle moves forward until it detects an obstacle, and then moves either left or right, depending on which direction is more free of obstacles? For example, when the vehicle detects an obstacle, it could spin 90 degrees left and measure the distance to the nearest obstacle (hint: save this distance value as a variable). Then it could spin to the right and measure the distance to the nearest obstacle in this direction (hint: save this as another variable). Then the micro:bit could compare the distance to the obstacles and move forward in whichever direction is more clear, with the farthest distance to the nearest obstacle.
Wrap-up
Today we tested the input signal received by the ultrasonic distance sensor using serial monitoring, then programmed the MOVE motor car’s micro:bit to respond by avoiding obstacles in its path. After it was programmed, the car used the distance sensor input signals to move autonomously, with no further input from the user.
What is another practical real-world use of a distance sensor?
How many distance sensors do you think are embedded in a modern vehicle?
Next Activity
Next week we will adapt the MOVE motor vehicle to be autonomous in yet another way. We will use a new sensor integrated in the MOVE motor vehicle called a contrast sensor. This sensor will allow the vehicle to follow a line drawn on the ground. Using this line-following ability, the MOVE motor can drive in loops around a custom race track.