User Tools

Site Tools


mbot:lesson2

Objective

This lesson will show you how to program the mBot to avoid obstacles. It will then show you how test your program on the mBot.

Parts Required

The parts below are required to complete this lesson. Note that all parts except the styrofoam blocks are included in the mBot kit that can be purchased on Amazon.

  1. Assembled mBot from Lesson 1
  2. USB Cable
  3. Styrofoam Blocks that are at least 5in tall and 2in thick

Software Programming

  1. Open the mBlock app on your computer

    You can download the mBlock app from mBlock Download if it isn't already installed

    Open mBlock app

  2. Click on the add button on the Devices tab

    You can also click the 'X' for the CyberPi device since it won't be used

    Add new device

  3. Select the mBot and click the OK button

    Select mBot device

  4. Click the Upload button under the Mode Switch section

    This allows you to write your program without having the mBot connected to your computer

    Select Upload mode

  5. Click the Events selector and drag the “when mBot(mCore) starts up” block to the coding pane

    Startup block

  6. Click the Control selector and drag the “wait until < >” block to the coding pane and snap it to the bottom of the stack

    Wait until block

  7. Click the Sensing selector and drag the “when on-board button pressed?” block to the coding pane and snap it into the “wait until < >” block

    This allows you to control when the mBot starts running the obstacle avoidance after you power it on

    Button pressed block

  8. Click the Control selector and drag the “forever” block to the coding pane and snap it to the bottom of the stack

    This will allow your program to keep running until you turn the mBot power off

    Forever block

  9. Drag the “if < > then else” block to the coding pane and snap it inside the forever block

    If-Then-Else block

  10. Click the Operators selector and drag the “( ) < 50” block to the coding pane and snap it into the if - then block

    Less than block

  11. Click the Sensing selector and drag the “ultrasonic sensor port 3 distance (cm)” block to the coding pane and snap it into the “( ) < 50” block

    Ultrasonic sensor block

  12. Change the “50” to “10” in the “( ) < 50” block

    Set distance value

  13. Click the Action selector and drag the “turn right at power 50% for 1 secs” block to the coding pane and snap it inside the top section of the if-then-else block

    This will cause the mBot to turn right whenever it senses an obstacle within 10cm

    Turn right block

  14. Change the “1” to “0.5” in the “turn right at power 50% for 1 secs” block

    Set time for turning

  15. Drag the “move forward at power 50%” block to the coding pane and snap it inside the bottom section of the if-then-else block

    This will cause the mBot to move forward whenever it does not sense an obstacle within 10cm

    Move forward block

  16. Connect the USB cable to the mCore mainboard on the mBot and plug the cable into your computer
  17. Slide the power switch on the mCore mainboard on the mBot to the ON position and click the Connect button

    Connect to mBot

  18. Click the Upload button and wait for the upload to complete

    Upload program to mBot

  19. Slide the power switch on the mCore mainboard on the mBot to the OFF position and disconnect the USB cable from the mBot

Robot Testing

Follow the steps below to test out the program you just created

  1. Place some of the styrofoam blocks on the floor
  2. Place the mBot on the floor and slide the power switch on the mCore mainboard on the mBot to the ON position
  3. Press the black button on the front left of the mCore mainboard on the mBot to start the mBot moving
  4. When you're finished watching the mBot avoid obstacles, pick it up and slide the power switch on the mCore mainboard to the OFF position

What should change in your program to make the mBot turn left when it senses an obstacle? Make that change and see if it works

When you're ready to try out changes to your program, repeat steps 16 through 19 above

How could you change the program so that you can tell the mBot to stop moving instead of picking it up when you're done? Make that change and see if it works

How can you make the mBot start the obstacle avoidance using the infrared remote control like it did in Lesson 1? Make that change and see if it works

Return to Main mBot page

"“Worthy are you, our Lord and God, to receive glory and honor and power, for you created all things, and by your will they existed and were created.” - Revelation 4:11"
mbot/lesson2.txt · Last modified: 2023/10/14 19:00 by jstruebel