Controlling a servo with a BBC Micro:Bit

In a previous post I wrote about controlling DC motors using an inexpensive integrated circuit. In this post I will explain how you can use a Micro:Bit to control a servo motor (like those used for steering in radio controlled cars) with no extra electronics (other than a secondary power supply)

Servos are a great way of adding another physical dimension to physical computing projects.

In this brief shot, one of our students demonstrates his Micro:Bit controlled (not quite) walking robot, which is powered by servos, to David Cameron at 10 Downing Street.

The process for controlling servos is a little bit peculiar. This is because these ‘hobbyist’ devices pre-date digital electronics.

Servos generally have 3 wires. A control / data line, and two for power supply (+ve and ground)

Servo motors are designed in such a way that the position of the “servo horn” is determined by the ratio of on time and off time in an (approximately) 20 microsecond pulse.

Most servos have 180 degrees of rotation. Lets say a 20 ms pulse begins, we make our data line “on” or “high” for 1.5 ms. (A pulse must always begin with the leading edge of a high).

This would tell our servo to move to the centre (90 degrees). 2 ms would move it to its full extent clockwise (180 degrees) and 1 ms would move it to its least most extent (0 degrees). For the remainder of the “pulse” the signal should be low.

Therefore, varying the length of the on time between 1.000 ms and 2.000 ms will allow us to move the servo to any position we want.

We cannot use the microbit “delay” function to achieve this level of accuracy. It’s lowest resolution is 1ms therefore we can only use it at best to move the servo either all the way one way or all the way in the other but nothing in-between.

Fortunately it is possible by taking advantage of the analogue output of the Micro:Bit. This analogue out is not true analogue, it is pulse width modulation.

With PWM we send a series of high speed on off pulses. The ratio of on to off time makes it appear to receiving electronics that it is receiving varying voltage levels.

We can take advantage of this feature (credit goes to Andy Huntington at Microsoft for this method)

Let’s assume we have 2 battery packs, a standard 2XAA battery pack for the MicroBit and a second batt(4xAA)ery pack to power the servo.

To connect it up, do the following.

  • Cut the plug off the end of the servo wire, separate the 3 wires and strip the insulation from the ends.
  • Connect the ground wire from the servo (usually blue / black) to the negative / ground wire from the 4xAA battery pack.
  • Connect the +’ve wire from the servo (usually red / orange) to the +’ve wire from the 4xAA battery pack. Important – you MUST insulate this +’ve connection with insulation tape and make sure there is no chance of a short. If you short out the connections from even 4xAA batteries it can generate enough heat to cause a nasty skin burn.
  • Connect the ground on the Micro:Bit to the negative / ground connection between 4xAA battery pack and relay. Now insulate this joint too.
  • Finally, connect the control wire from the servo (yellow or similar) to one of the Micro:Bit GPIO pins (lets say, pin 0).

We should be able to control the servo now with a few simple lines of code. In Touch Develop:

  • Analog write pin 0 (512)
  • Analog set period pin 0 (20,000)

This sets up the PWM period of the micro:bit to correspond with the expected 20 ms pulse width.

Now we can write “analogue” values to pin 0, between 35 and 135 (ish) will allow the micro:bit to instruct a servo to any position between 0 and 180 degrees.

Here is a screenshot of one of my test programs controlling a servo attached to pin 0.

servo control

Controlling a servo with a BBC Micro:Bit

Computing in school – making a virtual machine in Azure

In a previous post I described some of the issues faced by computing teachers when attempting to teach their subject on a standard school network.



Forty years ago it was very rare for educational establishments to have a computer on site. Nevertheless it was possible to still practice coding. Students would use punch card systems and teletype to write a program and then connect to a distant mainframe computer. This would execute the program and send the results back to the eagerly waiting student.

Nowadays ICT and computers are ubiquitous in schools but, in spite of this, all to frequently they can not be made available for students to practice coding.

computers, computers everywhere yet not a machine to code on

There is something in this old system of connecting to a remote yet powerful computer system which can be one solution to this modern educational problem.

Cloud computing is the modern equivalent. A virtual computer is created. This virtual computer, while in itself can be a very powerful machine such as a high grade internet web server of the order that is used to power enterprise systems,  is in reality nothing more than a simulation of a computer. A simulated computer that is running on another computer of staggeringly powerful proportions.

Commercial cloud computing like Microsoft Azure makes this available to the general public and it can be used by schools to circumvent the ‘no programming on the school network’ problem.

Once we have set up a Virtual Machine we can easily create user accounts and groups for all of our students. With Linux we can have many individual users all working at the same time on the same single virtual computer, each with their own desktop, running programs and individual home directories.

As they are effectively all using the same machine we only need a worry about installing and maintaining software on that one single (virtual) computer. Additionally, we don’t need worry about hardware failures as our system resides on the same mission critical rack that is used for all manner of other important systems (the NHS choices website being one such example)

An impressive program called Guacamole can be added to our virtual computer. This will allow our virtual computer to create multiple virtual desktops which can be accessed through a modern (HTML 5 compliant) web browser.

At the moment I am using the least powerful of three virtual architectures. I intend to test this on a class of thirty and then scale up if necessary.

(Update – 2 problems in school, guacamole does not seem to work well in general on the old Internet Explorer. 2 we are proxied. Guacamole depends on a constant stream of data but http data gets cached which makes it appear that the client is not responding. Solution will be to use https / websocket which is not cached)

There are other options also available such as ‘load balancing’ which involves two or more identical machines. If one machine is under too much load as many users connect to it, some will be seamlessly diverted to a different machine.

In this walk through I will create a virtual computer that runs Linux. I will then install a graphical interface, some examples of school programming software and finally install and configure Guacamole.

  • In the Azure Dashboard, create a new ‘classic’ virtual machine
  • Look through the available VMs and choose Ubuntu 14.04 Server. (This is the same as the standard Ubuntu you might install on your laptop except it is just a bare-bones system. None of the standard tools are installed and there is no graphical environment. Only command line (similar to old MS DOS). This is not a problem as we can easily install all of the missing elements.
  • You will be prompted for a username and password as part of the set up, this is important as it is the root user, the administrator. Also, you will not be able to use the system you have created without these details.
  • Go ahead and create the new system. It will take a few minutes to complete the process. When it is completed  you will be presented with a summary page of information about your new virtual machine. Look carefully and you will see a ‘Virtual IP address’. You will use this to connect to your computer and access the command line interface.
  • Open the required software ports to allow this virtual computer to communicate. Create new endpoints for HTTP (port 80), Alternative HTTP (port 8080), HTTPS (port 443) and RDP (port 3389).
  • We need to use a system called SSH to remotely access our virtual machine. This is available in the Terminal in Linux and Mac computers. For Windows we need to use putty.
  • Establish an SSH connection to your virtual IP address. In the Linux or Mac Terminal this is done with the command
    • ssh username@virtualIPaddress
  • We should now be logged in. First step is to update the list of repositories. These are known locations from where Ubuntu is able to download and install new software.
    • sudo apt-get update
  • Currently there is no graphical interface, only a text based command line. We can install one with the command
    • sudo apt-get install lxde
  • We will now follow the set of steps described in chapter 2 and chapter 5 of the Guacamole manual.
  • Install the first set of dependencies
    • sudo apt-get install libcairo2-dev libjpeg62-dev libpng12-dev libossp-uuid-dev
  • Now install the second set of dependencies
    • sudo apt-get install libfreerdp-dev libpango1.0-dev libssh2-1-dev libvncserver-dev libpulse-dev libssl-dev inlibvorbis-dev libwebp-dev
  • Download the Guacamole source code, uncompress it and then change into the new directory.
  • Now we will compile Guacamole and set it up so that it starts when the virtual computer starts.
    • sudo ./configure –with-init-dir=/etc/init.d
    • sudo make
    • sudo make install
  • When finished, change out of the guacamole directory, back to home directory, download and uncompress the guacamole client source code.
  • Install some extra tools needed to compile the client
    • sudo apt-get install maven
  • Install Apache Tomcat . This makes your virtual computer into a powerful dynamic java servlet web server and allows it to broadcast desktops visually with HTML5 over the internet into client web browsers.
    • sudo apt-get install tomcat7 tomcat7-admin tomcat7-docs
    • sudo apt-get install openjdk-7-jdk
  • Compile the Guacamole client.
    • mvn package
  • Now we will copy the compiled .war file to the Tomcat directory.
    • sudo cp guacamole/target/guacamole-0.9.9.war /var/lib/tomcat7/webapps/guacamole.war
  • Create a location for Guacamole configuration files.
    • sudo mkdir /etc/guacamole
  • Install a nice text editor to use to create the config files. Feel free to use emacs or vim here, I just like Joe.
    • sudo apt-get install joe.
  • Now create a config file with the text in red below.
    • sudo joe /etc/guacamole/

# Hostname and port of guacamole proxy
guacd-hostname: localhost
guacd-port: 4822

  • Press [ctrl]-[k]-[x] to save and exit. Now create a connection config file.
  • sudo joe /etc/guacamole/user-mapping.xml


<!– Per-user authentication and config information –>
<authorize username=”username” password=”password”>
<param name=”hostname”>localhost</param>
<param name=”port”>3389</param>

  • Create a directory in tomcat7 for the connection file to go into.
    • sudo mkdir /usr/share/tomcat7/.guacamole
  • Link this to the files you just created.
    • sudo ln -s /etc/guacamole/ /usr/share/tomcat7/.guacamole
    • sudo ln -s /etc/guacamole/user-mapping.xml /usr/share/tomcat7/.guacamole
  • Install RDP client to allow remote desktop broadcasting.
    • sudo apt-get install rdesktop  freerdp-x11 xrdp
  • Restart guacamole and tomcat
    • sudo service guacd restart
    • sudo service tomcat7 restart
  • At this point you should now be able to login to your new desktop. Check your Azure VM properties and you will see a URL. Something like:
  • Type into a web browser
  • You should see a login screen, type in the username and password you specified in your connection config file.
  • You should now see a second login screen, type in your root username and password that you specified when you created the virtual machine.

You should now be logged into an Ubuntu desktop as if you were using it on your own computer. You can now use the adduser commands to create logins for your students as well as install new software for example:

  • sudo apt-get install idle idle3 scratch

If it doesnt work, the first places to check are the log files. These are usually very detailed and help you find the problem.

Look at both of these files

  • /var/log/syslog
  • /var/log/tomcat7/catalina.out



Computing in school – making a virtual machine in Azure