Control A 360 Degree Servo With A BBC micro:bit featured image
We've recently added a continuous rotation servos to our range of stocked products. We thought it would be useful to do a quick tutorial on how to control one using a BBC microbit.

2568-mini-360-degree-servo-microbit-gif

For this tutorial we used a Mini 360 Degree Servo, an Edge Connector Breakout Board for the BBC micro:bit and a microbit. It's super simple to do and requires very few parts. We previously did a very similar tutorial for a 180 degree mini servo which you can read here.  

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. Powering the servo via the micro:bit may also illicit some code not functioning properly. 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 etc 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.
 

Controlling A 360 Degree Servo With A BBC micro:bit 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.
 

Step 1 Connecting The BBC micro:bit To The Servo:

mini_360_degree_servo_microbit

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, see picture above. The BBC micro:bit 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 360 Degree 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.  

The Difference Between A 180 Deg' Servo & A Continuous Rotation Servo:

360 degree servo with microbit

Cosmetically the mini 180 degree servo and the 360 degree servo look the same, both are the same in size as the Tower Pro SG90 and are often used as a lower cost alternative. Its operation is very different to that of a standard servo. Instead of going to a specified angle, this servo will be static at a 1.5 ms pulse (a trimmer is included to tweak this), a longer pulse (duty cycle) gives forward rotation and a shorter pulse (duty cycle) gives backwards rotation. The pulse frequency remains at 50Hz regardless of the length of the duty cycle. If you are using the block editor for the BBC micro:bit to write code to control the servo then this is taken care of by the code behind the blocks but if you choose to write your code in something like 'C' then you would need to specify a pulse frequency of 50Hz in your code.  

Step 2 Write The Code:

Now we know that the 180 degree servos and 360 degree servos are very different in operation, we'll produce some code using the same blocks that we would use to write code for a 180 degree servo to further highlight the difference. We used the Microsoft MakeCode (Beta) editor to produce our code as it also includes a servo simulator that appears automatically when you insert servo blocks into your code.
  As you can see from the embedded editor above, the code is very simple. The code basically delivers three things:
  • The servo rotates in one direction at full speed for three seconds.
  • It will then rotate in the opposite direction at full speed for three seconds.
  • Then the servo stops rotating for three seconds.
The above code on a 180-degree servo would move the servo arm to the specified position and hold it there for three seconds before moving the arm to the next specified position. When using the 360-degree servo, the value instead denotes direction and speed and in the code above, we've shown the absolutes in both directions and the value at which the servo is stationary. Experiment with different servo write values to see what level of control you can exert. The show number block is just to give an indication on the BBC micro:bit of a change in step in the code. This cycle will continue until you either press the reset button on the BBC microbit or disconnect it from a power source.  

You can download the code directly from the link below:

The linked Hex File has been zipped and will need to be unzipped before use. Download the code here. You can import the HEX file into the MakeCode editor if you wish to modify our code without having to recreate it by following the screenshot of the code.  

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

Once you have downloaded the code from one of the links above unzip the file, connect your BBC micro:bit to your computer via USB. Navigate to the file you unzipped 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 and continue to loop through the code until you press the reset button on the BBC micro:bit. The keen-eyed amongst you will have noticed from the code screenshot that we had a micro:bit uploader program running. This program automatically puts the HEX file onto your micro:bit when you hit the download button in the blocks editor. It will only work if this program is running at the time you download the HEX file. It is a great time saver. You can download it from here.

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.

More Information:

For more information, visit the product page. Or alternatively you can add one directly to your shopping cart below:

Kitronik's suggested additional learning

Using PWM with the Kitronik Motor Driver Board

Way back when we first released our Motor Driver Board the microbit Block Editor only allowed digital write on the pins we used for controlling motors. This meant that motors had to be full on or off. Thanks to an update in the PXT editor you can now use PWM to control the motors and therefore the speed of your buggy.

14 comments

Mark Donnison

Mark Donnison

Hi Mason, Our customer services team may be able to help: suport@kitronik.co.uk

Mason

Mason

It did not work please help

Mark Donnison

Mark Donnison

Hi Annette, thanks for getting in touch and sorry that you are experiencing difficulties. Please email support with your issue and your contact details and they will be able to talk you through some tests that will help to highlight the problem and a solution. support@kitronik.co.uk

Annette Alberts

Annette Alberts

Hi, I purchased a :MOVE mini buggy kit from SparkFun. When I put it together, both motors worked, but only on one side of the board. I double checked all programming. If I plugged the motor in slowly it would move a tiny bit but then no more. So, again, I verified that both motors function, but it looks like one of the motor ports is bad. Thanks, Annette

Jean-Paul Bihin

Jean-Paul Bihin

Thanks James, Your advice should appear at the beginning of each SG90 servo tutorial … Thank you very much for it. Jean-Paul

Mark Donnison

Mark Donnison

Hi David, the main purpose was to show how easy the code is but you are right, to use servos in a project requires more than just a micro:bit. We do have a couple of options on that front; The Servo:Lite board (https://www.kitronik.co.uk/5623-servolite-board-for-move-mini.html) and the 16 Servo Driver Board (https://www.kitronik.co.uk/5612-kitronik-i2c-servo-driver-board-for-the-bbc-microbit.html). Both options are specifically designed for use with the micro:bit and we have a number of tutorials to help get you up and running with using them. I have added updating this page with a further paragraph to my list of edits. Thanks for the feedback!

David

David

Here in lies the problem. What seems simple ends up being quite complex. A lot of the electronics that get kids excited at the possibilities require a lot of expertise. “You need to level shift the voltage” isn't something new user of Microbits can just do. They'll need to understand the electronics required and have access to the parts. I came to this page trying to get a couple of motors working. 3V runs fine through the MicroBit Inventors Kit but not 6V. I then read that transistors only work with certain voltages, requiring new parts. So I go looking up alternatives and come across this simple servo motor setup – only it's not so simple. I'm going to need to work out how to design the circuit (not necessarily hard but non-trivial) and buy the parts. We have the same thing in programming – I'm a software developer. Tutorials on how to do the simplest stuff are easy, but anything beyond the scope of the tutorials and their shortcomings are revealed. That's okay when it's your job to learn these things, but MicroBit is aimed at newbies with zero experience. Really, you just need a load of bundles. Bundle the servos with appropriate parts to allow them to use a 6V battery pack. You could even have recommended additions to a product to self-bundle as other websites do. I now have to find a 6V transistor, at which point maybe I can use my motors, work out what resistor I need and how to use it, and may still have missed something and not get a working setup.

James Dinsmore

James Dinsmore

Also make sure you tie the grounds together (i.e. have a common ground) with the power supply circuit and the MicroBit.

Mark Donnison

Mark Donnison

Hi Jack, the problem is that now that you are using 6V to power the servo the servo will be expecting a similar voltage level from the signal wire. As the signal is being provided by the microbit the signal voltage will be around 3V, which isn't enough. In order for the this set up to work you will need to level shift the signal voltage. This could be done with a transistor and resistor.

Jack Jarvis

Jack Jarvis

I've tried connecting the servo directly to the micro:bit, and it works fine, but when I connect it to a separate 6v power source, it just jumps for a few seconds and then stops working. The jump leads are all secure and correctly corresponding to the servo. Can anyone tell me what I am doing wrong?

Mark Donnison

Mark Donnison

Hi Andrew, we try to release multiple code examples for things that we produce, eg The Inventors Kit for the BBC microbit and we would like to do the same for any code that we make. It's not always possible due to time constraints. We don't post and forget though, we often return to old content to update and expand so never say never. Even for products such as this where the code is only designed to get you started, we would like them to be as useful as possible.

Andrew Butler

Andrew Butler

Can we have this in MicroPython? Please provide code for both platforms!

Mark Donnison

Mark Donnison

Hi Jack, the continuous rotation servo that we stock has a diagram on the product page for the pin assignments, https://www.kitronik.co.uk/2568-mini-360-degree-servo.html I would suggest that you use a dedicated power source for the servo and to not power it directly from the microbit, sometimes you can get away with taking power from the microbit and sometimes it will lead to erratic performance from the servo as the servo needs more current than the microbit is comfortable with handling.

Jack

Jack

Trying to run a feetech Mini 360 Degree Continuous Rotation Servo on a microbit through a motor driver board. Tried my own code, and your code and I cant get it to work. Can I get a confirmation on which pin each wire should be in and why the servo might not function.

Leave a comment

All comments are moderated before being published