micro:bot Kit Experiment Guide

Contributors: D___Run___, TheDarkSaint
Favorited Favorite 1

Experiment 5: Controlling a Servo - Battle Bot


Your robot should do something more than just drive around! Maybe control a small robotic arm, a gripper or even a weapon (see the bonus experiment if this peaks your interest!) of some kind. In this experiment you will integrate servo motors with your robot to build a Battle Bot with Jousting Lances. This experiment combines all the elements you’ve learned so far in this tutorial.

Parts Needed

  • 1x micro:bit board (Not Included)
  • 1x moto:bit carrier board
  • 2x wheels
  • 1x assembled shadow chassis
  • 2x Hobby Gear Motors
  • 2x Hobby servos
  • 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…

micro:bit Board

Servo - Generic (Sub-Micro Size)

SparkFun moto:bit

Hobby Gearmotor - 140 RPM (Pair)

Wheel - 65mm (Rubber Tire, Pair)

Battery Holder - 4xAA to Barrel Jack Connector

Shadow Chassis

SparkFun RedBot Sensor - Line Follower

1500 mAh Alkaline Battery - AA

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


Craft Supplies

  • 2x Skewers
  • Hot Glue and Gun
  • Optional: Electrical Tape

Suggested Reading

Hobby Servo Tutorial

Servos are motors that allow you to accurately control the rotation of the output shaft, opening up all kinds of possibilities for robotics and other projects.

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 Servo Motor

alt text

Unlike the action of most motors that continuously rotate, a servo motor can rotate to and hold a specific angle until it is told to rotate to a different angle. You can control the angle of the servo by sending it a Pulse Width Modulation (PWM) pulse train (turning a pin on and off really fast at different intervals); the PWM signal is mapped to a specific angle from 0 to 180 degrees in the servo block in MakeCode.

Inside of the servo there is a gearbox connected to a motor that drives the shaft. There is also a potentiometer that gives feedback on the rotational position of the servo, which is then compared to the incoming PWM signal. The servo adjusts accordingly to match the two signals.

Hardware Hookup

In this experiment you will actually be using two different servo motors to create a Battle Bot! One will be used as a “left” jousting lance and the other as the “right”. If you look at the servos, you will notice that they three wires have different colors; red, black and white. Red is the supply voltage, black is ground and white is the signal. Hook the two servos up to the moto:bit at the two labeled servo ports. Make sure you line up ground to ground.

Male Servo Pins

The “right” servo is connected to pin P16 while the “left” is connected to pin P15. Using a bit of hot glue or tape, you can attach the servos to each side of the robot chassis.

Next, you can build the jousting lances by utilizing some grilling skewers! You will only need two that can be cut down to the needed size. You can either glue or tape each skewer to the servo to the supplied attachments. Each servo set comes with a small bag of attachments as seen below. In this experiment, we utilized the double arm micro horn, but any of them will work.

Servo Attachment Package

Don’t have any skewers laying around? Be creative here!

Bot Assembled with Servos

This Battle Bot utilized electrical tape to mount the jousting lances to the servo motors, and it ended up looking something like this…

Battle Bot

Now, set your contraption aside until you have uploaded your code.

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

Don’t be overwhelmed by everything this code has to offer! I know it looks rather menacing, but the truth is that you’ve already done all of this code in previous experiments.

Experiment 5 Screenshot Code

Servo Write Pin to

The Servo Write Pin to block is found under the Pins drawer and it accepts a pin number that you have a servos signal wire attached to as well as an angle that you want it to rotate to. That’s the basics for controlling a servo motor with Microsoft MakeCode!

On Button A, On Button B and On Button A+B

In this example we use the On Button event blocks a lot. They allow us to break out different functions we want without messy if statements.

In this program the On Button A event block resets the calibration value for the table surface for the robot to use as a comparison value and also sets the servo positions to 90 degrees.

Press Button A

The On Button B block is used to set servo position to 45 degrees.

Press Button B

Finally the On Button A+B event block is used to run the program.

If / Then Statements

You may notice that there is a few if / then blocks inside of the if statement in the forever block. This will aid your Battle Bot in making decisions and knowing what it has to do next.

What You Should See

When your code is loaded onto your micro:bit the servo “arms” will move into a starting position and take initial readings for your line sensors to establish a comparison point to the ground environment.

Initially, the Battle Bot will move forward if none of the line sensors detect any kind of obstacle. If the center line sensor detects a direct collision the Battle Bot will immediately start both servos and move backwards. If the left or right line sensors detect an obstacle, the side that detected the obstacle will have the corresponding jousting lance start to move while turning away from it. So your Battle Bot is prepared to defend itself from any oncoming situation!

Go Further: Design and program a path for your robot to follow with a line sensor and figure out a creative way for it to know when to drop something at a specific location. Could you also program it to pick something up?


  • Servo moves in the wrong direction - Flip your servos, or change your code so that it rotates the correct direction.

  • Servo doesn’t move at all! - Double check your connections of the servos to the moto:bit to make sure that they are hooked up correctly.