11. Servo control

With micro:bits, motors and servos connected and programmed the Robot Wars can begin.


Kitronik has some very clear and simple instructions for using the servo with the micro:bit. I have used content from their website (https://www.kitronik.co.uk/blog/using-bbc-microbit-control-servo) in my notes below, so many thanks to Kitronik.


Connecting the servo to the micro:bit:

The servo cable shown above has a three terminal connector. 

The micro:bit will be programmed to send instructions to the servo from pin 0 via the servo's signal wire.

micro:bit edge connector                Tower Pro SG90 Mini Servo

          3V Pin                                                          Red Wire

          GND                                                             Brown Wire

          Pin 0                                                         Orange Signal Wire

To make the physical connections to the micro:bit's edge connector strip you will need to use jumper wires or can cut off the Servo's terminal connector, strip the three wires and attach crocodile clips or use micro:pegs to make the connections to the edge strip.

Whilst these micro-servos are known to work, it's important to note that the specified operating voltage for most servo motors is around +5V and that the micro:bit can only supply a small amount of power to connected circuits (3V and 90mA max).   As the battery voltage drops the servo will become less reliable.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

The optimal method for connecting a servo is to use a 4.5 volt or 6 volt battery pack to power the servo AND the micro:bit which controls it. This way you do not need an extra battery pack to power the micro:bit.


Remote control servo

In this 'remote control' example, we used a transmitter micro:bit to send a radio signal  (script on the left)to the receiver micro:bit which was in control of the servo (script on the right).

For a demonstration of this setup,  watch the video below.  


Additional battery packs often come as either 4.5V (3 batteries) or 6V (4 batteries). The micro:bit will supply 0V or 3V on the PWM pin0, and this has to be above the digital input pin threshold of the servo. If the voltage supplied to the servo is too high, half of that voltage becomes the pin threshold and the 3V from the micro:bit is rarely enough to direct the servo.


Makecode editor script:

The script above uses button A on the BBC micro:bit to start the process, the servo then continues to move 180 degrees in one direction and then returns to its starting position repeatedly until the BBC micro:bit is either reset or its power source is disconnected.


Development:

1. Edit the script so that pressing button A will rotate the servo to the right and button B will rotate the servo to the left.

2. Changer the number in servo write block from 180 to 90 (don't go larger than 180 as these small servos only allow 180 degrees of rotation).

3. Automated parking barrier project - Using an LDR as a light sensor, create a system that will raise a barrier to allow a car into a parking space when a light beam is broken.


N.B. The current drain on this servo is between 0.1-0.5A, and the voltage range is between 3.5-6V’s. Using a forever loop in your code in favour of one of the above methods may result in the servo not receiving quite enough power to turn. You can get around this by using a dedicated 4.5v power supply to power the servo directly whilst still using the BBC micro:bit to provide the control signal.

MicroPython script:

With the orange/yellow signal wire connected to pin 2 of the micro:bit (red wire to 3 v and brown wire to GND) the following script will set the servo to 90 degrees.

When button A is pressed, if the servo angle is more than 10 it will take 10 degrees off the servo angle.

When button B is pressed, if the servo angle is less than 170 it will add 10 degrees onto the servo angle.



from microbit import *

def set_servo_angle(pin, angle):

   duty = 26 + (angle * 102) / 180

   pin.write_analog(duty)


angle = 90

set_servo_angle(pin2, angle)


while True:

   if button_a.was_pressed() and angle >= 10:

       angle -= 10

       set_servo_angle(pin2, angle)

   if button_b.was_pressed() and angle <= 170:

       angle += 10

       set_servo_angle(pin2, angle)


Vehicle control + flipper

This is code some pupils created to add a flipper to their remote control vehicles so that they can compete in a 'robot wars' competition. 

The power of the serve is too weak to flip another vehicle but their wheels can be lifted off the ground.

from microbit import *

import radio

radio.on()


while True:

      if pin1.read_digital () == 1:

           radio.send('flip')

           sleep(50)

      if button_a.is_pressed():

           radio.send('lt')

           sleep(50)

      if button_b.is_pressed():

           radio.send('rt')

           sleep(50)



from microbit import *

import radio

radio.on()


def set_servo_angle(pin, angle):

      duty = 26 + (angle * 102) / 180

      pin.write_analog(duty)


angle = 90

set_servo_angle(pin1, angle)


while True:

      msgin = radio.receive()

      if msgin == 'flip':

          angle = 135

          set_servo_angle(pin1, angle)

          sleep(50)

      else:

          angle = 45

          set_servo_angle(pin1, angle)

          sleep(50)

      if msgin == 'lt':

          pin8.write_digital(0)

          pin12.write_digital(0)

          pin0.write_digital(1)

          pin16.write_digital(0)

          sleep(50) 

      if msgin == 'rt':

          pin8.write_digital(1)

          pin12.write_digital(0)

          pin0.write_digital(0)

          pin16.write_digital(0)

          sleep(50)

      else:

          pin8.write_digital(1)

          pin12.write_digital(0)

          pin0.write_digital(1)

          pin16.write_digital(0)

          sleep(50)