micro:bot Kit Experiment Guide
Experiment 2: Staying in a Box
Robots are smart! They are smart because they use sensors to detect what the world around them and then respond to those conditions. One of the conditions our robot can respond to is the darkness of the surface that it is driving on. It can detect lines, a gradient of darkness or a blacked out area. In this experiment you will program your robot to stay inside of a box that you have drawn on a surface.
You will need the following parts:
- 1x micro:bit board (Not Included with Kit)
- 1x Micro-B USB Cable (Not Included with Kit)
- 1x moto:bit Carrier Board
- 2x Wheels
- 1x Assembled Shadow Chassis
- 2x Hobby Gear Motors
- 1x 4xAA Battery Holder
- 4x AA Batteries (Not Included with Kit)
- 3x Analog Line Following Sensors
- 3x 3-Pin Jumper Wires
Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
Before continuing on with this experiment, we recommend you be familiar with the concepts in the following tutorial:
Introduction to the Line Sensors
The line follower sensor is an add-on for your shadow chassis that gives your robot the ability to detect lines or nearby objects. The sensor works by detecting reflected light coming from its own infrared LED. By measuring the amount of reflected infrared light, it can detect transitions from light to dark (lines) or even objects directly in front of it.
The sensor has a 3-pin header which connects directly to the moto:bit carrier board via female to female jumper wires. A mounting hole lets you easily connect one or more of these to the front or back of your robot chassis.
Like the motors, you should have already hooked up the line sensors during the assembly portion of this guide. You can go there now for the full assembly instructions. Double check to make sure that the wires are hooked up to your line sensors correctly!
The line sensors hookup to your moto:bit via female / female jumper wires that snake through the chassis of your robot up to the moto:bit. The sensors hookup to the moto:bit in the following order:
- LEFT =>
- CENTER =>
- RIGHT =>
Double check to make sure they are hooked up correctly and in the proper orientation
Running Your Script
Be sure to add the moto:bit package as instructed in the Installing the moto:bit Package in MakeCode section of this tutorial.
Now, you can either download the following example script below and drag and drop it onto your micro:bit, or use it as an example and build it from scratch in MakeCode.
Code to Note
Let's take a look at the code and what to expect.
We will need to set the motors depending on how the motors are wired to the moto:bit. In this case, we will be setting both motors to
true to be consistent with how we assembled the robot earlier.
Creating a Variable
In Microsoft MakeCode, you need to be able to create variables to store information for your robot to compare against, or to just remember. You can use the built-in variables under the Variables drawer as shown below.
But, you can also make your own custom variables. To do this click on the Variables drawer and select Make New Variable. You can then name it and use it in your program.
To store a value or piece of information in a variable you use the
set _______ to block. This allows you to select a variable and set it to a value block of your choice. In this case, the line sensor value on pin
P1. We do this once in the
on start block to get a base reading from the sensor. Pressing button A while the program is running will also save the line sensor reading again.
Analog Read Pin
To get that base reading from the line sensor we use the
analog read pin __ block to get an analog value (
1023) from the line sensor.
0 is totally black and
1023 is totally saturated white. Your surface will be something probably in between.
To compare the current reading of the line sensor to its known baseline value that was captured and stored in the
on start block, we use an
if statement block. The
if block accepts a logical statement. If that statement (
sensor value P1 ≤ surface - 30) is true, then the
if block runs then "then" section of code. If that statement is false then the "else" section of the block is run.
You may be asking yourself why subtract
30 from the
surface value. Well, that number is a sensitivity value. The sensor reading fluctuates as the robot moves around. By subtracting
30 from the original surface variable, we give the change in lighting a "wiggle room" or tolerance of
30. If you want your sensor to be more sensitive, you would make the number smaller. If you need it less sensitive, make it bigger.
To debug, LEDs of the micro:bit were turned on using the
show icon _____ block to indicate when the motor moves in reverse and forward.
What You Should See
Once your code is loaded find a light surface, preferably white and add a line of black tape to the surface. Then press the RESET button on your micro:bit. In fact, if you have a large enough space you can create a 3 foot square you will be perfect! Make sure to change the motor switch from "STOP MOTORS" to "RUN MOTORS."
|Switch Flipped to STOP MOTORS||Switch Flipped to RUN MOTORS|
Your robot will drive forward until the black tape is directly under the center line sensors. When that happens your robot will stop, reverse, pivot to the left a bit, and try to drive forward again, hitting a line once more. If it works correctly your robot should stay inside of the box forever!
Moto:Bit blocks not showing up - Make sure you have a network connection and you have added the package to your makeCode environment.
Micro:Bit Not Showing Up On My Machine - Try unplugging the USB cable and plugging it back in. Also, be sure that you have the cable inserted all the way into your micro:bit
Robot Not Moving - Make sure your motors are hooked up correctly in the motor ports and your motor power switch is set to “RUN Motors”, the battery pack is plugged in, and you have fresh batteries.
Moving in Reverse?! - There are two options if you notice the micro:bot moving in the wrong direction. As explained above, you can flip the wiring of your motors or use the
set ____ motor invert to ____block in your
on startblock to change what is forward vs. reverse.
Robot Doesn't Detect Line - If changing the width of your line doesn't help, remember line sensor calibration settings can be very sensitive depending on your environment. Press the A button to calibrate when the robot is on the surface or change the value being subtracted from the