micro:bot Kit Experiment Guide
Experiment 3: Following a Line
OK, you have your robot staying inside of box drawn on the floor, but that still seems a little odd and random. You want your robot to go somewhere, do something and then keep going! In this experiment you will elaborate what you learned from Experiment 2 to get your robot to follow a line.
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.
Introduction to Using Multiple Line Sensors
In the previous experiment you used a single line sensor (the middle sensor) to detect the line on the floor. That is great for staying inside of a line, but now, you need to follow a line. That is where the other two line sensors come in.
Essentially, you want the center sensor to detect the line, but not the other two, meaning that the robot is centered on the line. If one of the side sensors detect a line it means that you are veering to one side or another and your robot should correct itself. We will use the information from multiple sensors combined with an if/else statement block to build a decision tree for your robot to follow. For simplicity, we will start with using just two of the line sensors (the left and right sensors).
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.
LCal / RCal
Like in the previous experiment, you need to set a baseline value for the surface that your robot is driving on. This is actually called a calibration value. Like the previous experiment, we need to do this, but for two sensors; right and left. We go through the same routine we did for the single sensor previously, but for the right and left sensors.
On Button Press
As in the first experiment, we use the
On Button Press block to start the program. This is so you can can get a good base reading and calibrate your sensors without having to wrestle with a robot that is trying to move around.
While block is a logic block that is similar to the
loop block, but a bit smarter. The
While block accepts a TRUE/FALSE statement, if that statement is true the block will loop whatever code is placed inside of it. If that value is false the
While block is simply skipped over. We hardcode a
true value into the
While block so that it constantly loops when the A button is pressed. That way we have the benefits of both the event block and the
loop block without needing complicated programming.
What You Should See
Once you have loaded your script place your robot on a dark line on a light / white background. Make sure you have it centered with the line just underneath the center line sensor. Make sure the motor switch is changed from "STOP MOTORS" to "RUN MOTORS" and press the A button to start.
Your robot should drive forward until one of the side line sensors detects the line and then it will turn in that direction to correct itself. Depending on your line shape and thickness your robot may "waddle" more or less.
Robot Drives In a Circle - Double check your wiring and your code, your robot is not seeing the line. Also, double check that there isn't something like dust blocking your sensor.
Robot Isn't Moving - Pesky motor switch! Make sure that is set to "run" 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 Waddles a Lot! - Change the width of your line.
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. Also, the code calibrates when the micro:bot starts up. Make sure to have the sensors over a black line.