From Imperial College Robotics Society
Jump to: navigation, search

mbed is a fairly new microcontroller from ARM based on the ARM Cortex-M3 core. It is a significant step up from the more established hobbyist microcontrollers such as the Arduino and the BASIC Stamp. It runs at 96MHz, plenty fast enough for most small robots, and it is unique in that its tools are available exclusively online at mbed.org.

Hardware capabilities of the mbed microcontroller include I2C, SPI, Ethernet, Analog I/O, USB, CAN, and quite a bit more. You can find more information about this in the mbed Handbook


The mbed is requires between 4.5V and 9V to run. Do not supply more than 9V or you will damage the mbed. Make sure you check the power being supplied to the mbed as motors and servos will also draw power. To power the mbed one simply connects the topmost left pin to ground and the pin under it to their power source. You can also power the mbed directly from a USB port by using the USB-mini cables.

The mbed is capable of outputting a steady 3.3V regulated voltage out of the topmost right pin and 5V out of the pin underneath it whilst powered by USB.


Mbed quick reference.png


The mbed has many different pins and outputs which can be setup to perform various tasks. All of the pins p5 through to p30 can be setup for digital outputs or inputs, note that the commands are case sensitive. When defining a pin you can treat it as a variable and give it any name you desire.

  1. #include "mbed.h"    //includes mbed standard libary
  3. DigitalOut my_out(p21);  //sets up pin 21 as a digital output
  4. DigitalIn my_in(p25);  //sets up pin 25 and a digital input
  6. int main(){
  7.    my_out=1;  //sets output to the on state
  8.    if (my_in==1){   //tests whether the input is in the on state
  9.        my_out=0;  //sets the output to the off state
  10.    }
  11. }


The mbed also supports several analogue inputs, pins 15 through to 20, and a single analogue output through pin 18 (PWM outputs can be used to supplement this low amount). Analogue pins are treated exactly the same as digital pins when programming the mbed except that they can take any value between 0 and 1. Analogue inputs are extremely useful for sensors such as IR, however, one must be aware of noise when dealing with analogue signals and so should use inequalities in conditions. The following demonstrates using analogue inputs and outputs.

  1. #include "mbed.h"    //includes mbed standard libary
  3. AnalogueOut aout(p18);  //sets up pin 18 as an analogue output
  4. AnalogueIn ain(p15);  //sets up pin 15 and an analogue input
  6. int main(){
  7.    aout=0.5;  //sets output to 0.5
  8.    while(1){    //sets up a loop
  9.       if (ain>0.3){   //tests whether the input is above 0.3
  10.           aout=0;  //sets the output to 0
  11.       }
  12.    }
  13. }

Pulse Width Modulation

Three PWM pulses with the average output marked

Pulse Width Modulation uses the fact that the inertia of the wheel (and the robot) will tend to suppress attempts to change the speed. That is, the inertia will try to enforce a constant speed on the wheel. If you apply a torque to the wheel by turning the motor on, it will take a while to spin it up. The same goes for when you turn the motor off while the wheel is spinning. If you switch the motor on and off so quickly that the speed doesn't have time to change, you will reach an equilibrium speed that is somewhere between full power and turned off. By changing the ratio of how long the motor is on compared to off (the Pulse Width), you can change the equilibrium speed.

The following program snippet demonstrates how to use the PWM hardware built-into the mbed. Note: it assumes that pins 21 and 22 are the "go forward" pins on the left and right motor controller respectively. You need to provide the pins that you have used, which might not be the same.

Use this code as a starting point, and play around with it and see what you can make your robot do!

  1. /*
  2. This program demonstrates the PWM functionality of the mbed.
  3. It drives forwards and turns at different speeds and turning radiuses.
  4. */
  6. #include "mbed.h"
  8. PwmOut lmotor(p21);
  9. PwmOut rmotor(p22);
  11. int main() {
  12.     wait(5); //Wait a little before starting
  14.     //Turn to the right for 3 seconds
  15.     lmotor = 0.5; //Left motor: 50%
  16.     rmotor = 0.0; //Right motor: 0%
  17.     wait(3); //Wait 3 seconds (while turning)
  19.     //Stop for 1 second;
  20.     lmotor = 0.0;
  21.     rmotor = 0.0;
  22.     wait(1);
  24.     //Go forward at 25% for 2 sec
  25.     lmotor = 0.25;
  26.     rmotor = 0.25;
  27.     wait(2);
  29.     //Do a turn with a large radius (for 2 sec)
  30.     lmotor = 0.5;
  31.     rmotor = 0.4;
  32.     wait(2);
  34.     //Go forward at 100%! (for 2 sec)
  35.     lmotor = 1.0;
  36.     rmotor = 1.0;
  37.     wait(2);
  39.     //Finally stop
  40.     lmotor = 0.0;
  41.     rmotor = 0.0;
  42. }

External Links