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.
- 1x micro:bit board (Not Included)
- 1x Micro B USB Cable (Not Included)
- 1x moto:bit carrier board
- 3x Line following Sensors
- 1x micro-B USB Cable
- 1x 4xAA Battery Holder
- 4x AA Batteries (Not Included)
Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment…
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
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 A0. We do this once in the
On Start block to get a base reading from the sensor.
Analog Read Pin
To get that base reading from the line sensor we use the
Analog Read Pin block to get an analog value (0 - 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 < 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” of 30. If you want your sensor to be more sensitive you make the number smaller, if you need it less sensitive make it bigger.
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 and press the RESET button on your micro:bit. In fact, if you have a large enough space you can create a 3' square you will be perfect! Change the motor switch from “STOP MOTORS” 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 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 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.