MicroBit and a Servo – Make a Lego Robot Dance

This tutorial uses a pair of MicroBits to make a little Lego Robot swing its arms back and forth, like its listening to ‘Hip Hop Hooray’.

 

Materials

I really love the MicroBit.  It is a surprisingly robust micro-processor that is very easy to code and work with.  This tutorial uses the radio feature, where one Microbit can communicate with another through a bluetooth connection.

The MicroBit can be programmed directly through the Microbit.org website using javascript blocks (like Scratch).  You can also use Python, which is what I’ve done here.  I just had an easier time making it work that way.

You’ll need two MicroBits, one will be the controller, and the other will be the waver.

Controller

This is the Python code for the controller.


from microbit import *
import radio

radio.on() #turn on the radio
radio.config(channel=5)
radio.config(power=7)

right = "R"
left = "L"

while True:
  reading = accelerometer.get_x()
  if reading > 20:
    display.show("R")
    radio.send(right)
    sleep(2)
  elif reading < -20:
    display.show("L")
    radio.send(left)
    sleep(2)

This code is pretty straightforward. In it, we use the accelerometer and get which way the Microbit is being turned. If it gets a reading of +20 or more we assign that a value of ‘Right’, and if it is -20 or less we assign it a value of ‘Left’.  The important part is the sleep command.  If that isn’t in there, it overwhelms the other Microbit, so we ask this one to sleep for 2 milliseconds, which is barely noticeable.

The Waver

Here is the code I used for the Microbit that controlls the servo.


from microbit import *
import radio

class Servo:

"""
A simple class for controlling hobby servos.

Args:
pin (pin0 .. pin3): The pin where servo is connected.
freq (int): The frequency of the signal, in hertz.
min_us (int): The minimum signal length supported by the servo.
max_us (int): The maximum signal length supported by the servo.
angle (int): The angle between minimum and maximum positions.

Usage:
SG90 @ 3.3v servo connected to pin0
= Servo(pin0).write_angle(90)
"""

def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
  self.min_us = min_us
  self.max_us = max_us
  self.us = 0
  self.freq = freq
  self.angle = angle
  self.analog_period = 0
  self.pin = pin
  analog_period = round((1/self.freq) * 1000) # hertz to miliseconds
  self.pin.set_analog_period(analog_period)

def write_us(self, us):
  us = min(self.max_us, max(self.min_us, us))
  duty = round(us * 1024 * self.freq // 1000000)
  self.pin.write_analog(duty)
  self.pin.write_digital(0) # turn the pin off

def write_angle(self, degrees=None):
  degrees = degrees % 360
  total_range = self.max_us - self.min_us
  us = self.min_us + total_range * degrees // self.angle
  self.write_us(us)

radio.on() #turn on the radio
radio.config(channel=5)
radio.config(power=7)

right = "R"
left = "L"
sv1 = Servo(pin0)

while True:
  incoming=radio.receive()
  if incoming=="R":
    display.show("R")
    sv1.write_angle(30)
    sleep(1)
  elif incoming=="L":
    display.show("L")
    sv1.write_angle(120)
    sleep(1)

This seems like a lot, but it is really just two chunks of code put together. The first chunk is for a Class Servo.  Out of the box, the Microbit doesn’t have a Servo Class, so this adds one.  It came from this github.

The second chunk is the part that actually controls the servo.  When it gets a radio signal it assigns the servo to a certain setting based on whether it gets a ‘R’ or ‘L’ command.  I made these 90 degrees apart arbitrarily, you can change that to anything you want in the sv1.write_angle lines.

The Connections

Another beauty of the Microbit is how easy it is to make connections.  You will use an alligator clip to the Pin 0 pad, the 3V pad, and the GND pad.  The servo has three connections and a female connector, so you will most likely also need male to male jumper wires.

MicroBit Servo
Pin 0 Yellow/Orange
3V Red
GND Brown

I built a little robot body to house the servo.  My son has promised to make some improvements to my robot design, but this was good enough.

 

Leave a Reply

Your email address will not be published. Required fields are marked *