My Cart:

0 item(s) - £0.00
You have no items in your shopping cart.

0

Control A 360 Degree Servo With A BBC micro:bit

As we've recently added a continuous rotation servo 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 BBC micro:bit. 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:

  • Step 1 - Connect the Servo to the Breakout board.
  • Step 2 - Write / download the code.
  • Step 3 - Put the code onto the BBC micro:bit and test.
  • Step 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.

controlling-a-continuous-rotation-servo-with-microbit-870

As you can see from the image above the code is very simple. The code basically delivers three things:

  • The servo rotates in one direction at full speed for three seconds.
  • The servo then rotates in the opposite direction at full speed for three seconds.
  • 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. Normally we would publish the code and link to the editors but due to a temporary issue with logging into the Kitronik account on the BBC microbit website we are unable to publish code in the usual way.

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 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:

E-Textiles
← Previous Post Next Post →

6 thoughts on “Control A 360 Degree Servo With A BBC micro:bit”

  • Jack March 21, 2017 at 3:14 pm

    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.

    Reply
    • Mark Donnison April 3, 2017 at 12:32 pm

      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.

      Reply
  • Andrew Butler September 8, 2017 at 1:42 pm

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

    Reply
    • Mark Donnison September 15, 2017 at 1:53 pm

      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.

      Reply
  • Jack Jarvis November 11, 2017 at 10:22 am

    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?

    Reply
    • Mark Donnison November 13, 2017 at 1:02 pm

      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.

      Reply
Leave a Reply