The Hands-On ARM Mbed Development Lab Manual - Agus Kurniawan
The Hands-On ARM Mbed Development Lab Manual - Agus Kurniawan
Table of Contents
Copyright
Preface
1. Setting Up Development Environment
1.1 ARM mbed Boards
1.2 Getting Hardware
1.3 Development Tools
1.4 Electronics Devices
1.5 Setting up mbed Board to Computer
1.6 Hello mbed and STM32 Nucleo
1.6.1 Registration
1.6.2 Adding STM32 Nucleo as Target Board
1.6.3 Creating Project
1.6.4 Compiling
1.6.5 Deploying Program to STM32 Nucleo Board
1.6.6 Testing
1.7 Hello mbed and RedBearLab nRF51822
1.7.1 Registration
1.7.2 Adding RedBearLab nRF51822 Board to mbed IDE
1.7.3 Creating Project
1.7.4 Adding mbed Library
1.7.5 Writing Program
1.7.6 Compiling
1.7.7 Deploying Program to RedBearLab nRF51822 Board
1.7.8 Testing
2. mbed Digital I/O
2.1 Getting Started
2.2 Demo 1: Turning on LED by Pressing Pushbutton
2.2.1 Wiring Hardware
Contact
Preface
This book was written to help anyone wants to get started in ARM mbed development. It describes all the basic elements of the
mbed board and how to program with step-by-step approach..
Agus Kurniawan
Depok, May 2015
mbed development provides online tool which we can write program via browser so you don't need
install any tool.
For RedBearLab nRF51822 board, you can see it as removable storage and can be verified using
Device Manager.
The next step is to understand mbed board pins. For illustration, STM32 Nucleo F401RE board pin
can be described as follows.
You could also find the board pin for RedBearLab nRF51822, shown in Figure below.
1.6.1 Registration
To use mbed development, we must register. You can register on https://developer.mbed.org/ by
clicking signup menu on website.
Click Add Platform button. Then, you will be navigated to mbed board platform.
Select your STM32 Nucleo board. After that, click Add to your mbed Compiler menu.
Select your platform, Nucleo. Then, select Blinky LED test for the ST Nucleo boards.
If done, you will get a form as below.
1.6.4 Compiling
You can compile your program by clicking Compile menu.
STM32 Nucleo board will blinking to flash the program. If success, your compiled file will not show
again.
1.6.6 Testing
After deployed, you can see the board will show blinking LED.
1.7.1 Registration
After that, you should see a list of mbed board. Select your own board, for instance, RedBearLab
nRF51822. Then, click Add to your mbed button.
Now you have already added your mbed board into IDE. You're ready to write program.
You should see a dialog. Select your mbed Platform and Empty Program on Template. Fill a project
name. If done, click OK button.
Select Import Library -> From Import Wizard. After selected, you should see a list of library from
mbed.org.
Select mbed on Libraries tab.
Then, you should see mbed library that installed into your project.
int main() {
while(1) {
myled = 1;
wait(0.2);
myled = 0;
wait(1.0);
}
}
//
//
//
//
LED is ON
200 ms
LED is OFF
1 sec
RedBearLab nRF51822 board has a built-in LED on D13 or P0_15. See RedBearLab nRF51822 pin
map on section 1.5. To turn on LED, you just set value 1. Otherwise, you set value 0 to turn off LED.
1.7.6 Compiling
To compile this program, you can click Compile. If success, you will get a download popup dialog.
Save the result compiled, *. hex, for instance, in this project, you
get RedBearLab_nRF51822_blink_led_RBLAB_NRF51822.hex file.
mbed board will take this program and then install it automatically.
1.7.8 Testing
After deployed a program to mbed board, you should see LED blinking.
This chapter explains how to write mbed program to access mbed digital I/O.
LED
two resistor 220 or 330
Pushbutton
To understand a Pushbutton wiring, you can see the following Pushbutton schema.
LED is connected to mbed board digital pin 12 and Pushbutton is connected to mbed board digital pin
6.
The following is my hardware implementation. I used Arduino shield from Link Sprite,
http://store.linksprite.com/base-shield-of-linker-kit-for-pcduino-arduino/ , so it's easy to attach my
electronics components.
}
}
Because I used RedBearLab nRF51822, Digital 12 and Digital 6 are defined as P0_13 and P0_16.
We pass these pins to DigitalOut and DigitalIn objects.
To write data, we just set the value 1 or 0. wait() function is used to delay our program.
Now you can compile and upload the program to mbed board. Read section 1.7.7.
2.2.3 Testing
After uploaded the program, you can press a pushbutton, you should see LED lighting.
This chapter explains how to work with serial communication (UART) in mbed board.
Click Install button. Follow the installation steps. At the middle of process, you will be asked to give
security confirmation. Click Install button.
After finished, you can connect mbed board into your computer. You should a process dialog for
mbed driver installation. Wait it until finish.
After done, you can verify the installation using Device Manager. You should see connected mbed
board on Port (COM & LPT) category.
3.2.1 Wiring
You don't do anything on your board. You just connect your mbed board to your PC.
This code will write incremental number data to mbed UART. USBTX and USBRX are pins for TX
and RX which connected to PC.
Compile this code and upload to mbed board.
3.2.3 Testing
After uploaded the program into mbed board, you can test it using any Serial Application for your
platform. In this case, I use CoolTerm for Windows platform. You can download it on
http://freeware.the-meiers.org/ .
Navigate your Serial app to mbed Serial Port/COM. In my case, my mbed board is connected to
COM6.
After that, connect to your mbed board. You should see incoming message from mbed.
A sample output can be seen in Figure below.
Before you upload code to Arduino, you must disconnect Arduino UART from mbed board UART.
Now you can upload the program into Arduino.
This program will read incoming message. After that, write it on UART PC via USBTX and USBRX
pins.
Save this code. Compile and upload it to mbed board.
3.3.4 Testing
After uploaded program to mbed board, you can use your Serial app to see incoming message from
mbed board. I used CoolTerm app.
This chapter explains how to write mbed program to access mbed Analog I/O.
You can connect VCC to mbed board VCC 5V. Vout to mbed board Analog input A0. In addition,
GND to mbed board GND. The following is hardware implementation. I use slide potentiometer.
To read analog input from mbed board, we can use AnalogIn object. You can read it on
https://developer.mbed.org/handbook/AnalogIn . Now you can create mbed project, called
analog_input. Read section 1.7.
On main.cpp file, you can write this code.
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
AnalogIn pot(A0);
int main() {
while(1) {
float pot_val = pot.read();
printf("potential: %.3f \n", pot_val);
wait(1.0);
}
}
4.2.3 Testing
After uploaded the program into mbed board, you can open your Serial app. I used CoolTerm. You
should see analog input value from this app.
The following is a sample out for reading analog input.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
red
green
blue
yellow
// purple
aqua
4.3.3 Testing
After uploaded, you can open Serial app and then connect to mbed Serial port. You should see
messages from mbed board.
You also see RGB lighting. The following is sample outputs for RGB LED.
RGB output for red color.
5. mbed I2C/TWI
This chapter explains how to work with I2C on mbed board and write program to access I2C.
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices. You can
find it on the following online store:
Amazon, http://www.amazon.com/PCF8591-Converter-Module-DigitalConversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-analogconverter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-EvaluationDevelopment/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following URLs.
http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
The objective of this section is to illustrate how to read data on I2C bus. We use PCF8591 AD/DA
converter with sensor devices as I2C external source. You can read this module on this link,
http://www.electrodragon.com/w/images/8/89/PCF8591_ADC_DAC_ADDA_Analog_Digital_Conver
. In our scenario, we will read three sensor devices in PCF8591 AD/DA modules via I2C.
Let's start!.
The following is PCF8591 library. Further information about this library, you can read it
on https://developer.mbed.org/users/wim/code/PCF8591/ .
Add main.cpp file for main program and write this code.
#include "mbed.h"
#include "PCF8591.h"
Serial pc(USBTX, USBRX);
I2C i2c_bus(P0_29, P0_28); // sda, scl
PCF8591 adc_dac(&i2c_bus,PCF8591_SA0);
int main() {
uint8_t analog;
while (1) {
// read A/D value for Channel 0 (LDR)
analog = adc_dac.read(PCF8591_ADC0);
pc.printf("thermistor: %d \r\n",analog);
wait(1);
// read A/D value for Channel 1 (photo-voltaic cell)
analog = adc_dac.read(PCF8591_ADC1);
pc.printf("photo-voltaic cell: %d \r\n",analog);
wait(1);
// read A/D value for Channel 1 (potmeter)
analog = adc_dac.read(PCF8591_ADC3);
pc.printf("potentiometer: %d \r\n",analog);
wait(1);
}
}
Save this code. Compile and upload this program to mbed board.
5.2.3 Testing
Now you can compile and upload the program to the board. To verify the program, you can open
To connect this module to mbed board, you can read configuration on section 5.2.1
5.3.3 Testing
Now you can compile and upload program to a board. Then, open Serial Monitor to see Serial output.
On PCF8591 AD/DA converter module, you can a red LED is dimming. A sample output can be seen
in Figure below.
6. mbed SPI
This chapter explains how to work SPI on mbed board and how to access SPI using mbed program.
You can see these pins on STM32 Nucleo F401RE and RedBearLab nRF51822 boards., shown in
Figure below.
mbed board works as SPI Master. If you want to connect to SPI Slave Devices, you can connect with
the following configuration.
data++;
if(data>110)
data = 97;
}
}
At the first this program will initialize SPI object by passing SPI pins, MOSI, MISO and SCK.
Because I use RedBearLab nRF51822 board, these pins are defined as P0_20, P0_22 and P0_25.
We use write() to write data into SPI. Furthermore, we get a return value from SPI. We already
connect MOSI to MISO so we will get the same data from what I have sent.
Save this code. You can compile and upload it into mbed board.
6.2.3 Testing
After uploaded, you can open Serial app. Connect it to mbed board Serial Port (COM). After that,
you should see the output from the program.
A sample output can be seen in Figure below.
6.3.2 Wiring
To implement our second demo, you need the following items:
mbed board. I used RedBearLab nRF51822 board
MCP41x1 and MCP41x2 from Microchip, https://www.sparkfun.com/products/10613 . I used a
chip MCP4132
LED
This code writes data through SPI. Data value is 0 - 255 and 255 - 0. It uses looping approach to set
this value.
Save this code. You can compile and upload it into mbed board.
6.3.4 Testing
After uploaded, you open Serial app. Connect it to mbed board UART. Now you should see
Potentiometer value.
A sample output can be seen in Figure below.
This chapter explains how to work with Bluetooth low energy (BLE) on mbed board.
After imported, we can see your project with BLE_API and nRF51822 libraries.
If you open main.cpp file, you should get the following code.
#include "mbed.h"
#include "iBeaconService.h"
BLEDevice ble;
const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61
};
uint16_t majorNumber = 1122;
uint16_t minorNumber = 3344;
Install this application. After installed, you can run this app. Try to scan. If success, you should see
our beacon signal from mbed board.
A sample output of BLE Scanner can be seen in Figure below.
7.3.2 Testing
Now we test our program.
After installed, you can run this program. Select Simple Chat.
Now you try to scan. You should see our mbed board BLE. You should see Biscuit name. Tap this
item.
Now you can use Serial app from PC to type any message. Then, press Enter to send.
This chapter explains how to work with servo motor connected to mbed board. We explore how to
access PWM (Pulse Width Modulation) on mbed board.
The next step we are going to build a program with mbed board and servo motor.
As we know, mbed board has a power about +3.3V and +5V so I recommend to use an external
power to supply the servo motor. This servo motor needs +5V.
The following is a sample of hardware implementation.
P0_18
//D9
if(direction==1)
degree = degree + 15;
if(direction==2)
degree = degree - 15;
if(degree>180){
degree = 180;
direction = 2;
}
if(degree<0){
degree = 0;
direction = 1;
}
wait(1.0);
}
}
You can see that I use Servo object from RedBearLab. The following is implementation for servo.h
file.
#ifndef _SERVO_H
#define _SERVO_H
#include "mbed.h"
class Servo
{
public:
Servo(PinName pin);
~Servo(void);
void write(unsigned char degree);
private:
void convert(unsigned char degree);
PwmOut _servo;
unsigned int pulse;
};
#endif
#include "Servo.h"
Servo::Servo(PinName pin) : _servo(pin)
{
_servo.period_ms(20);
}
Servo::~Servo(void)
{
}
void Servo::write(unsigned char degree)
{
convert(degree);
_servo.pulsewidth_us(pulse);
}
void Servo::convert(unsigned char degree)
{
// 0~180 degree correspond to 500~2500
pulse = degree * 11 + 500;
}
Save this code. Compile and upload this program into mbed board.
8.4 Testing
Now you can compile and upload to a board. You can open Serial Monitor to verify data on UART.
You can see the output as below.
and you also see servo moves with incrementing angle 15. After 180 degree, it will back to angle 0.
Source Code
Contact
If you have question related to this book, please contact me at [email protected] . My blog:
http://blog.aguskurniawan.net.