Mbed compiler

From Imperial College Robotics Society
Jump to: navigation, search

The mbed compiler is completely online. This allows for easy use without the need to download anything. However, you must have internet access to develop the mbed. Once you have compiled your code simply drag and drop it into the mbed device which will appear on the file explorer as an external memory device. Press reset and the mbed will then run the latest program uploaded to it.

mbed Online Toolchain

The mbed toolchain is a collection of tools which are used to create software which runs on the microcontroller. It includes a text-editor, version control software, a host of useful C++ libraries, help documents and a compiler. To make an mbed account and get access to all of the online tools, follow the instructions in the mbed setup guide.

The Compiler

A compiler is a tool that allows you to write your software, find any mistakes from it and subsequently turns your software into a format that the machine (in this case the mbed microcontroller) can understand.

This video shows you quickly the process of writing, compiling, downloading and running some software on the mbed.

When you arrive at the mbed compiler you will be greeted with something which look like this:

Mbed compiler 1.jpg

  • To the top there are the standard buttons such as save, new, compile and so on.
  • To the left you can see a list of all of the projects and the source files within those projects.
  • In the central large screen the code for the currently opened file can be seen, several files can be opened at once using the tabs above this window.
  • To the bottom is a standard compiler output window that will tell you about errors or warning that appear whilst compiling the code.

To create a new program click the "New" button. You will be prompted to enter a "program name". A new folder will be created with a basic example source file and the mbed library which includes all the commands required for programming the mbed.

Once you have finished your program, or wish to test it, click the compile button. The compiler will firstly debug your code and will alert you to any such buts in the compiler output window to the bottom. If there are no bugs then you will be prompted to download the file. Do so and then copy it across to the mbed which will be listed as an external memory device whilst plugged into the USB port. Press reset and the mbed will then run the latest program uploaded to it.

The Code

Programming the mbed is done in C++, a very popular programming language. Don't worry if you haven't come across C++ yet, you will hopefully be able to grasp the basics through this lesson.

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);   //setup pins
  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.     //Go forward at 25% for 2 sec
  20.     lmotor = 0.25;
  21.     rmotor = 0.25;
  22.     wait(2);
  24.     //Do a turn with a large radius (for 2 sec)
  25.     lmotor = 0.5;
  26.     rmotor = 0.4;
  27.     wait(2);
  29.     //Go forward at 100%! (for 2 sec)
  30.     lmotor = 1.0;
  31.     rmotor = 1.0;
  32.     wait(2);
  34.     //Finally stop
  35.     lmotor = 0.0;
  36.     rmotor = 0.0;
  37. }

Further C++

If you are intrested in programming in C++, you can download the visual studio 2010 completely freely at / Microsoft Dreamspark by registering with Imperial College.

There are some great tutorials on learncpp.com. We highly recommend going through at least Chapter 0 and Chapter 1 before this lesson, if you are new to programming and C++.

mbed Documentation

There are two main sources of information and help for the mbed, the Handbook and the Cookbook.


The Handbook is the official documentation, with guide on how to use the mbed along with descriptions of the main libraries and the functions they offer. Here you can find out everything you need about the "core" functionality of the mbed, such as reading and writing digital signals, communicating with another device, and just about anything else you can see on the mbed Quick Reference card:

Mbed quick reference.png

Please always refer to this handbook first on how to use the mbed before asking any other questions!


The mbed Cookbook is an unofficial source of help in the form of a wiki. This is where you can find more specific help which isn't regarded as a common use of the mbed. For example, if you're having trouble using the mbed with a particular model of LCD display, the chances are that someone else has had the same problem and written about their solution in the Cookbook. You are also encouraged to contribute yourself to this resource, whether it's creating your own pages, or correcting some mistakes you see.

mbed Forums

The mbed forums is yet another resource for people using the mbed in their projects. It's an active community of people asking and answering questions about the mbed and its use with other electronics.

If a search through the mbed Handbook and Cookbook sheds no light on your situation, then go ahead and post your question to the mbed community! Oh, and if you are a member of ICRS, then you should add ICRS as a tag on your post. This way we can answer your questions with higher priority than others, and also it'll show ARM how active we are, encouraging them to support us more.

Mbed forums.png