This page describes how to write a simple obstacle avoidance program for mBot2 with the mBlock 5 app on your smart device.

Before you start, make sure that you’ve got:

image.png or image.png
A tablet or smart phone
image.png
mBot2
If you haven’t completed the building of mBot2, refer to “Build mBot2“ to build it first.

:::info Note:
The smart device described on this page is a tablet, and you can use the mBlock 5 app on smart phones in the same way. :::

1. Download the mBlock 5 app on the tablet

(Skip this step if you have already downloaded the mBlock 5 app.)
Use the tablet to scan the following QR code:
[Detailed Example] Programming mBot2 with the Mobile mBlock 5 App - 图4
Alternatively, search for mBlock in app stores to download it.

2. Connect mBot2 to the mBlock 5 app

(1) Enable the Bluetooth function on the tablet.

image.png

(2) Power on mBot2.

image.png

(3) Tap to open the mBlock 5 app.

mBlock-5-mobile.png

(4) Tap “Coding”, and tap “+” on the page that appears.

image.png

(5) In the sprite library, choose “Devices” > “CyberPi” and tap “√”.

image.png

(6) Tap the CyberPi setting icon and then tap “Connect” on the device setting page that appears.

image.png

(7) Place the tablet close to mBot2.

mBlock 5 automatically searches for and connects to mBot2.
image.png
After the connection is complete, a voice message is played, indicating that the connection is successful.

3. Start to write the program

(1) Tap “Back to coding”.

image.png

(2) Add the extensions required in the obstacle avoidance program.

To avoid obstacles, mBot2 uses its ultrasonic sensor 2 to detect obstacles in its way and then respond to them by using the motors and wheels connected to mBot2 Shield. Therefore, you need to add the mBot2 and Ultrasonic Sensor 2 extensions.

Add them as follows:
a. Tap + Extension at the bottom of the blocks area, and then tap + Add under mBot2 on the Extension Center page that appears.
image.png
b. Tap + Extension at the bottom of the blocks area, and then tap + Add under Ultrasonic Sensor 2 on the Extension Center page that appears.
image.png
After the extensions are added, you can find the blocks in the blocks area.
image.png

(3) Set the programming mode to “Upload”.

Live: In this mode, you can view the program execution effect in real time, which facilitates the debugging of the program. In this mode, you must keep mBot2 connected to mBlock 5. If they are disconnected, the program cannot be executed.
Upload: In this mode, you need to upload the compiled program to mBot2. After being successfully uploaded, the program can still run properly on mBot2 when it is disconnected from mBlock 5.
image.png

(4) Drag the blocks required to the scripts area.

a. First, define the event for triggering the execution of the obstacle avoidance program. For example, trigger the program by pressing button B.

Tap the Events category, drag the block when button () pressed to the scripts area, and tap the drop-down list box to select button B.
image.png

b. Define how many times the program is to be executed. For example, keep executing the program after it is triggered.

Tap the Control category and drag the block forever to the scripts area.
image.png

c. Define the specific conditions for avoidance and how mBot2 responds to an obstacle.

Define the condition
For example, mBot2 starts to avoid an obstacle when it is 9cm or less away from mBot2; and mBot2 keeps moving forward when no obstacle is found or an obstacle is more than 9cm away from it.

Tap the Control category and drag the block if () then () else () to the scripts area.
The condition involves the operation of distance.
Tap the Operators category and drag the block () > () to the scripts area and put it into the condition block.
image.png
Tap the value 50 to change it to 9.

Tap the Ultrasonic Sensor 2 category and drag the block ultrasonic 2 () distance to an object (cm) to the scripts area and put it into the operation block.
image.png

Define the responses
When an obstacle is more than 9cm away from mBot2, mBot2 keeps moving forward.
Tap the mBot2 Chassis category and drag the block () at () RPM to the scripts area and put it into the condition block.
image.png

When an obstacle is 9cm or less away from mBot2, mBot2 turns left.
Drag the block () at () RPM for () secs to the scripts area, put it into the condition block, and tap the drop-down list box to select turns left.
image.png

Now, a simple obstacle avoidance program is completed.

4. Upload the program to mBot2

:::info Note: The program you upload will replace the last program you execute on mBot2. :::

Tap the Upload icon, wait till the uploading is complete, and tap OK.
image.png

5. Execute the program

Press button B on CyberPi.
image.png
mBot2 starts to move forward and detects obstacles. When an obstacle is found and it’s 9cm or less away from mBot2, mBot2 turns left for one second and then moves forward.

:::tips Tips:

  • You can change the parameter values on the blocks (such as changing the distance threshold or responses) or add more conditions to compile your own obstacle avoidance programs.
  • You can also compile other programs (such as a line following program) in the similar way. :::