How to use servo blocks to control a servo on a micro:bit

Issue #27 resolved
Dominic Kelly created an issue

Hi there,

I am planning a micro:bit project with my school code club involving servos. I would love to use Microblocks, but I can't work out how to use the relevant code blocks to get my servo to work. If possible, could you give me an example of how I could control a 180 degree servo via pin 0?

(I have done this fine with the same servo in MakeCode so I know the servo / connections are working fine).

Many thanks,

Dom

Comments (2)

  1. John Maloney repo owner

    Hi, Dom.

    Are you using a servo interface board such as the Kitronics Servo:Lite board? There are some power issues when using servo's with the BBC micro:bit directly. I've found that some servo's will work when the micro:bit has a battery plugged in but don't work when powered by the USB-serial cable. Other servos (such as the KeyStudio microservo) seem to need more than 3.3 volts. They only work with an external battery pack (e.g. 4.5v, three-batteries).

    However, since you say you've gotten your servo's to work with Makecode, you must have found a solution for powering them.

    To use servos with MicroBlocks, import the Servo library. Add a "when started" script to run the servo loop:

    The "run servo loop" block loops forever; it never returns, so any commands following it in a script will never get executed! This is a misfeature of the library that has caused some confusion in our workshops; I'm working on a fix for it right now.

    In a separate script, you can write commands to change the servo settings for servos attached to pins 0-2 such as:

    I hope this helps.

    -- John
    

    P.S. I'm curious how you are powering and connecting your servos, and what solution you'd recommend to other teachers. My personal favorite is the Servo:Lite board because of the built-in AAA battery pack. I've also used the Hummingbird:bit, the SparkFun MotoBit board, and a breakout board from KeyStudio. You can also connect your servos directly to a 4.5v battery pack using alligator clips, but that solution can get messy when you've working with more than one servo.

    P.P.S. For some reason servos's work okay with the AdaFruit Circuit Playground Express board even without external power. I realize that's not an option for you, but when we've done workshops with servos we've used the CPX rather than the micro:bit. As a result, I have less real-world experience using servos with the micro:bit.

  2. John Maloney repo owner

    New servo library and low level support in micro:bit makes servos much easier to use. Note that a board that supplies additional battery power such as the Ring:bit v2 or the servo:lite board is needed for servos to work reliably with the micro:bit.

  3. Log in to comment