Authorized Online Retailers

SKU:2020005200(with mainboard)

Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

2020005300 (without mainboard)

Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

In the previous lessons, we introduced the ultrasonic obstacle avoidance robot and infrared obstacle avoidance ro. In order to avoid obstacles more accurately, in this lesson we combined an ultrasonic module and an infrared obstacle detector sensor to build an obstacle avoidance robot.

Ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. Please click here to learn more
Ultrasonic sensor interface: use ultrasonic sensor to detect the distance, connect Trig to p14, connect Echo to P15.

Step 0: Go to urlhttps://makecode.microbit.org, click New Project

Step 1. Click on the Basic category again. Then  drag the show icon block  and drop it inside the on start block ,and choose the icon from the drop-down menu as following figure.

Step 2. Click on the Logic category and then click the if then else block, and then Drag inside forever block as following:

Step 3. Click on the Logic category again. Under the Comparison section, drag “0 < 0” blocks over, and place it on the placeholder of the if-then-else block, and change “<” to “>” as following:

Step 4. In the Editor controls, click on the Advanced button and then Extensions  as following figure.
Step 5. In the Extensions page , enter project URL “https://github.com/osoyoo/OSOYOO-MICRO-BIT-CAR ” in the Search textbox and click on the Search button. The page will show you all the matching extensions based on your search string. Now, click on osoyoo to add it into your project .

Step 6. Click on the OSOYOO_Sensor category and drag and drop the Ultrasonic block and place it on the first placeholder of the Comparison block and change Trig P0 to P14, Echo P0 to P15. Also, change the second placeholder of the Comparison block to 15 as following picture:

Step 7. Click on the Logic category and then click the if then else block, and then place it inside the “then” section of the if-then-else block as following:

Step 8. Click on the Logic category again. Under the Boolean section, drag “ and ” blocks over, and place it on the placeholder of the second if-then-else block, as following:

Step 9. Click on the OSOYOO_Sensor category and drag and drop the IR_Sensor Pin block and place it on the first placeholder of the Boolean block and change P0 to P12, and keep detected as following picture:

Step 9. Duplicate IR_Sensor Pin block and change P12 to P13, and detected to undetected, and then place it on the second placeholder of the Boolean block as following picture:

Step 10. Drag and drop a CarCtrlSpeed  block over and place it inside the “then” section of the if-then-else block, and change the direction to rotate left and speed to 180.

Step 11. Click on the Basic category, and then drag and drop the pause (ms) block under CarCtrlSpeed block and change to 400ms as following:

Step 12. Click “+” to add an else section as following:

Step 13. Duplicate Boolean block and change detected to undetected, and undetected to detected, and then place it on the second if section as following picture:

Step 14. Duplicate CarCtrlSpeed block and pause (ms) block and change direction to rotate right, and then place it on the second then section as following picture:

Step 15. Click “+” to add an else section as following:

Step 16. Duplicate Boolean block and change detected to undetected, and then place it on the third if section as following picture:

Step 17. Duplicate CarCtrlSpeed block and change direction to forward, and then place it on the third then section as following picture:

Step 18. Drag and drop a CarCtrl block over and place it inside the “else” section of the if-then-else block, and change the direction to stop.

Step 19. Click on the Basic category, and then drag and drop the pause (ms) block again under CarCtrl block and change to 100ms as following:

Step 20. Duplicate CarCtrl block and pause (ms) block and change direction to back, and 100 to 200, and then place it under pause (ms) block as following picture:

Step 21.Duplicate the first CarCtrlSpeed and pause (ms) blocks and change 400 to 200, and then place it under second pause (ms) block as following picture:

Step 22.Duplicate the second if-then-else block, change the third CarCtrlSpeed block forward to left, delete the first CarCtrlSpeed and pause (ms) blocks, and change the fourth pause (ms) block 200 to 100, and change the fifth pause (ms) block 200 to 500 and then place it inside else section of the first if-then-else block as following picture:

Step 23. Rename this project and download the hex file as following

The whole code for the project is as below. You can click “Edit” to download the whole project or edit the project according to your own ideas.

Step A)Connect the micro:bit with the computer through USB cable, and the computer will pop up a U disk and send the download file(.hex)to your micro:bit.

Step B)Insert the micro:bit main board and ultrasonic sensor into the robot (please make sure the “eyes” of ultrasonic sensor face to front), and install a 9v battery into the battery box.

Step C)Turn the POWER switch ON. Putting the car on the floor, the robot car can avoid obstacles as if it can “see” the obstacles

 RUNNING RESULT

When the front obstacle distance measured by ultrasonic sensor is greater than 15cm.

When the front obstacle distance measured by ultrasonic sensor is less than or equal to 15cm.

Note:

This experiment must be carried out indoors to reduce interference from sunlight to infrared receiver.

If the robot tracking is not normal, you can adjust its speed and running time through makecode, and you can also change the sensitivity of the infrared obstacle avoidance sensor by adjusting the potentiometer