micro:bot Kit Experiment Guide

Pages
Contributors: D___Run___, TheDarkSaint
Favorited Favorite 1

Experiment 2: Staying in a Box

Introduction

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.

Parts Needed

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.

micro:bit Board

micro:bit Board

DEV-14208
$14.95
5
Battery Holder - 4xAA to Barrel Jack Connector

Battery Holder - 4xAA to Barrel Jack Connector

PRT-09835
$2.49
Hobby Gearmotor - 140 RPM (Pair)

Hobby Gearmotor - 140 RPM (Pair)

ROB-13302
$4.95
SparkFun moto:bit

SparkFun moto:bit

DEV-14213
$14.95
3
USB micro-B Cable - 6 Foot

USB micro-B Cable - 6 Foot

CAB-10215
$4.95
10
Wheel - 65mm (Rubber Tire, Pair)

Wheel - 65mm (Rubber Tire, Pair)

ROB-13259
$2.95
2
SparkFun RedBot Sensor - Line Follower

SparkFun RedBot Sensor - Line Follower

SEN-11769
$2.95
7
Shadow Chassis

Shadow Chassis

ROB-13301
$12.95
7
1500 mAh Alkaline Battery - AA

1500 mAh Alkaline Battery - AA

PRT-09100
$0.50
Jumper Wire - 0.1", 3-pin, 6"

Jumper Wire - 0.1", 3-pin, 6"

PRT-10368
$1.50

Suggested Reading

Before continuing on with this experiment, we recommend you be familiar with the concepts in the following tutorial:

Getting Started with the micro:bit

The micro:bit is a compact, powerful programming tool that requires no software installation. Read on to learn how to use it YOUR way!

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.

Line sensor

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.

Hardware Hookup

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!

Line Sensor Wire Hookup

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 => P0
  • CENTER => P1
  • RIGHT => P2

Double check to make sure they are hooked up correctly and in the proper orientation

Attached to moto:bit

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.

Calibration is very important with the line sensors to work accurately. Your environment will greatly affect the P1 analog readings and thresholds for the surface value so you might have to customize these numbers to suit your application.

Code to Note

Let’s take a look at the code and what to expect.

Experiment 2 Code Screenshot

Set Motors

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.

variable drawer

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.

naming variable

Set To

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 (0 - 1023) from the line sensor.0 is totally black and 1023 is totally saturated white. Your surface will be something probably in between.

If

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.”

Stop 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!

Go Further: So, you have built a robot corral! Use what you now know to build a maze for your robot to solve on its own!

Troubleshooting

  • 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 start block 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 surface.