Christmas Information: Kitronik will be closing 24th Dec December 2024, reopening on 2nd of January 2025. See our delivery schedule for more information.

Using The BBC micro:bit To Control A Servo featured image
In this tutorial we are going to look at how to control a servo using the microbit via an Edge Connector Breakout Board for the BBC micro:bit. It’s super simple to set up and requires very few parts.

Using The BBC micro:bit To Control A Servo -main-870

For this tutorial we used the 180 Mini Servo, this servo is great for this application. We have used this particular servo for several different projects as it is small and has a decent amount of torque.  

Note:

Problem: Although we have used the BBC micro:bit as a power source for the servo, in this and other projects, this isn't the ideal method. This has to do with the maximum amount of current that the BBC micro:bit can handle, using the BBC micro:bit to supply power to a servo has you very close to this limit. Solution: Using a battery pack to provide power directly to the servo and only using the BBC micro:bit to supply the control signal to the servo is the best way to protect your BBC micro:bit from harm and to ensure that you are getting the full amount of torque from the servo.  

Level Of Difficulty:

  • Easy.

Parts List:

You Will Also Need:

  • An Internet browser.
  • A USB cable to connect the BBC micro:bit to the computer.

Possible Uses:

We recently posted an article submitted by Thomas Stratford in which he shared his process for making a pedestrian crossing with the BBC micro:bit. If you wanted to add a little extra complexity to your project you could use a servo as a level crossing barrier.  

Using The BBC micro:bit To Control A Servo Process:

  • 1 - Connect the Servo to the Breakout board.
  • 2 - Write/download the code.
  • 3 - Put the code onto the BBC micro:bit and test.
  • 4 - Troubleshoot any issues.

Using The BBC micro:bit To Control A Servo -870

Step 1 Connecting The BBC microbit To The Servo:

The servo has a three wire connector. The brown wire is ground, the red wire is positive and the orange wire is the signal wire. The BBC microbit will send instructions to the servo from the P0 pin via the servo's signal wire. Push the BBC micro:bit into the Breakout Board. Use the 3 x M/F Jumper wires to connect the Servo to the Breakout Board, the table below shows how the connections are made.
BBC micro:bit Edge Connector Breakout Board 180 Mini Servo
3V Pin. Red Wire.
GND. Brown Wire.
P0 Pin. Orange Wire.
  Note: This experiment can also be done without the Edge Connector Breakout board. In order to do this, you will need 3 x Crocodile Leads and you will need to cut off the Servo's connector and strip the three wires. The connections in the above table remain the same if using this method, though you will be connecting the Crocodile Leads directly to the BBC micro:bit.  

Step 2 Write The Code:

As you can see in the picture below, we produced two code examples to control a servo. This was just to illustrate that there is more than one way to get the job done.

Example 1:

  In Code Example 1 we made use of both of the BBC micro:bit's buttons. Button A makes the servo spin around one way and Button B returns it to its starting position.  

Example 2:

  In Code Example 2 we used button A on the BBC micro:bit to start the process, the servo then continues to move one way and then the other until the BBC micro:bit is either reset or its power source is disconnected.  

Step 3 Download The Hex File To The BBC micro:bit And Test:

To Download: Choose your preferred option from the embedded editors above. Press the download link at the bottom of the embedded editor. The code will now download to the default download folder on your computer. Once you have downloaded the code from one of the links above, connect your BBC micro:bit to your computer via USB. Navigate to the file you downloaded in your downloads folder and drag it straight onto the BBC micro:bit in File Explorer (Windows). Once the .hex file is on the BBC micro:bit, press button A and the servo should move. If you used Code Example 1 pressing A will make the Servo move in one direction only. If you used code example 2, pressing button A will make the servo rotate one way and then the other until you press the reset button on the BBC micro;bit or disconnect it from the power source.  

Step 4 Troubleshooting:

  • Check that you have connected the servo to the breakout board correctly, refer to the table in Step 1 for guidance.
  • Ensure that the M/F jumper wires are pushed into the servo connector securely.
  • Note: If writing your own code, using a forever loop 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.8v power supply to power the servo directly whilst still using the BBC micro:bit to provide the control signal.

Kitronik's suggested additional learning

BBC micro:bit Controlled Crane Step By Step

This is a step by step guide to customising a toy crane to be controlled with a BBC micro:bit, using the Motor Driver Board for the BBC micro:bit and the built-in accelerometer to detect tilt and turn motions.

6 comments

TP

TP

For those of us who are complete novices, it would be helpful if the tutorial included images of (1) The proper connections to the Edge Connector Breakout Board, and (2) A wiring diagram illustrating the “Solution: Using a battery pack to provide power directly to the servo and only using the BBC micro:bit to supply the control signal to the servo” in the Note (above). Thanks.

Mark Donnison

Mark Donnison

Hi Trevor, the Kitronik 16 servo control board has an edge connector that the micro:bit slots into. Please see the product page for more info, there is a video and also a datasheet. https://www.kitronik.co.uk/5612-kitronik-i2c-servo-driver-board-for-the-bbc-microbit.html

Trevor

Trevor

Hello, how do I connect a BBC MICRO BIT to a KITRONIK 16 to make the MICRO SERVO 9g move?

Mark Donnison

Mark Donnison

Hi Laura, if you contact support directly they will be able to go through setup and testing with you to help you get to the bottom of the issue. support@kitronik.co.uk

Laura Sammon

Laura Sammon

Hi, I was just wondering if the code for the movement of the servo has changed recently, along with the update for the 'micro:bit create code' page? I am trying to build a move:mini car but the code, which is working perfectly on the computer, won't work with my servo. I have bought all new micro:bits and servos so i dont know what else it could be. Do you know whether the code has changed slightly since the update? Kind Regards, Laura

Magnus Carlenäs

Magnus Carlenäs

My experience with servos is that they try to stay in position, if you tell it to go to 180 it makes tiny movements to stay in 180 degree position. If I would like it to turn off is there a way to do that? I tried to use “digital write PX to 0” and put that under “servo write PX to 180” and a small paus. Could I use an other blocks to turn the servo off?

Leave a comment

All comments are moderated before being published