Program mBot with the mBlock App

This page describes how to program mBot with the mBlock app on your smart device.

  1. Download and install the mBlock app

  2. Connect mBot to the mBlock app

  3. Write a program

  4. Run the program

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

1280X1280_-_2023-03-13T143813.664.PNG

A tablet or smartphone

1280X1280_-_2023-03-13T143819.286.PNG

mBot (If you haven't completed the building of mBot, build it by referring to Build mBot first.)

 

1. Download and install the mBlock app

Scan the QR code below with your smart device.

Alternatively, you can search for mBlock in an app store.

 

2. Connect mBot to the mBlock app

(1) Enable Bluetooth on your smart device.

1280X1280_-_2023-03-13T144050.677.PNG

(2) Power on mBot.

(3) Tap to open the mBlock app.

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

1a6d3778-096a-46ff-b28e-54fcbb7c37ca.png

(5) In the Sprite Library, choose Devices > mBot and tap.

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

168a709f-b686-4a38-ba90-ddab70d4ddf8.png
1bcc4278-e9a0-4d7f-8c67-3d2fe132a5bd.png

(7) Place your smart device close to mBot for mBlock to automatically search for and connect to mBot.

After the connection is complete, a voice message is played, indicating that the connection is successful.

 

3. Write a program

(1) Tap Back to coding.

8d074f42-9859-4681-923e-09361bf45ee5.png

 

(2) Set the programming mode to Live.

Live: In this mode, you can view the program running effect in real-time, which facilitates the debugging of the program. In this mode, you must keep mBot connected to the mBlock app. If they are disconnected, the program can't be run.

Upload: In this mode, you need to upload the compiled program to mBot. After being successfully uploaded, the program can still run normally on mBot when it is disconnected from the mBlock app.

Note: Programs can not only be uploaded to mBot via the mBlock app, but mBlock PC client and web version.

(3) Drag blocks to the scripts area.

a. Define the time when the program is triggered by selecting an appropriate block from the Events category. For example, select the block when green flag clicked.

Tap Events, and drag e0a23df9-3426-4286-a4b0-06507c0d67c8.png to the scripts area.

ea6dec64-1f65-4743-810f-27c31ad6b744.png

 

b. Define the number of times the program is to be run. For example, keep the program running after it is triggered.

Tap Control and drag the block forever () to the scripts area.

c. Define the specific conditions for obstacle avoidance and the way mBot responds to an obstacle.

Define the condition

For example, mBot starts to avoid an obstacle when the obstacle is within 9 cm away from it, and mBot keeps moving forward when no obstacle is found or an obstacle is more than 9 cm away from it.

Tap Control and drag the block if () then () else () to the scripts area.

df66b605-8e26-4c4a-ac1b-cfedd04e2fda.png

Set the condition with a distance.
Tap Operators and drag the block () > () to the scripts area and put it in the condition block.
Tap the value 50 to change it to 9.

Tap Sensing and then drag and place the block ultrasonic sensor () distance(cm) as below.

Define the response

When mBot is more than 9 cm away from the obstacle in front, mBot keeps moving forward.
Tap Action, drag and place the block () at power ()% as below, tap and choose move forward.

2260396a-a9d1-4f5f-b013-3fe6099a0ff2.pngc46e6727-754f-4951-bdb4-898b7c293af8.png

When mBot is within 9 cm away from the obstacle in front, mBot turns left.
Tap Action, drag and place the block () at power ()% as below, tap move forward, and choose turn left.

ed4f97a5-3de3-4b16-b66c-31a2f845a894.pnga76614ef-7792-48d2-9355-4a50db317576.png

Now, a simple obstacle avoidance program is completed.

 

4. Run the program

Tap the green flag in the lower right corner to run the program.

See if it is as expected that mBot starts to move forward and when an obstacle is detected within 9 cm away from it, it turns left and then keeps moving forward.
Note: Keep mBot within the Bluetooth connection range of your smart device.

 

Tips:

  • You can change the parameter values on the blocks (e.g., change the distance threshold and responses) or add more conditions to compile your own obstacle avoidance program.

  • You can also compile other programs (such as a line following program) in a similar way.

Was this article helpful?
11 out of 14 found this helpful

Comments

1 comment
  • Translator
     
     
     
     

     

    Hi, I need save the project build in the app to share with other persons or devices, how do it?

     

    Thanks.

    0

Please sign in to leave a comment.