2. Other Projects‎ > ‎Micro:Bit‎ > ‎

Controlling a Servo with micro:bit

posted 18 Dec 2016, 15:25 by Andy Lakin-Hall   [ updated 19 Dec 2016, 00:23 ]

My next project is to control a Servo with the micro:bit.

Micro Servo
I already have a good micro Servo from my Arduino and PICAXE kit, and I've got a wiring diagram of that so I know how to connect it.

The grand idea is to use the micro:bit in a rocket as a parachute release mechanism, particularly as it has accelerometers which can detect launch and apogee. But first to get the micro:bit to test drive the Servo.

Helpfully, Kitronik have a tutorial for this, so I won't be working in the dark.

Here's my circuit. 

I'm using a separate 4.5v supply to power the Servo. The current required for the Servo motor is too much for the micro:bit to do anything else useful, so it's better to use the extra battery.

Here's the code. I've made it so that pressing A moves the Servo up, while B moves it down.

I originally tried moving the Servo to positions 0 and 180, but I found that the Servo jiggled around at the end positions, and I have read about that occasionally being a problem, so I reduced the positions to 10 and 160, and that works better.

Another really interesting tutorial can be found at https://pxt.microbit.org/reference/pins/servo-write-pin. This demonstrates how one range of numbers can be mapped onto another, without a lot of calculations to work it out.

In this example, the number recorded from the accelerometer, which clearly is in the range from -1023 to 1023 is mapped onto values between 0 and 180, which the Servo can cope with.

I haven't actually tried this yet, but I've popped it in here to remind me to try.