100% found this document useful (1 vote)
3K views125 pages

The Hands-On ARM Mbed Development Lab Manual - Agus Kurniawan

Uploaded by

oscar120381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views125 pages

The Hands-On ARM Mbed Development Lab Manual - Agus Kurniawan

Uploaded by

oscar120381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 125

Copyright

The Hands-on ARM mbed Development Lab Manual


Agus Kurniawan
1st Edition, 2015
Copyright 2015 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

2.2.2 Writing Program


2.2.3 Testing
3. Serial Communication - UART
3.1 ARM mbed UART
3.1.1 Installing mbed Serial Driver for Windows Platform
3.2 Demo 1: Hello mbed UART
3.2.1 Wiring
3.2.2 Writing Program
3.2.3 Testing
3.3 Demo 2: UART Pingpong
3.3.1 Hardware Configuration
3.3.2 Writing Program for Arduino
3.3.3 Writing Program for mbed
3.3.4 Testing
4. mbed Analog I/O
4.1 Getting Started
4.2 Demo 1: Reading Analog Input - Analog Sensor
4.2.1 Hardware Wiring
4.2.2 Writing Program
4.2.3 Testing
4.3 Demo 2: Writing Analog Output (PWM) - RGB LED
4.3.1 RGB LED
4.3.2 Hardware Configuration
4.3.2 Writing Program
4.3.3 Testing
5. mbed I2C/TWI
5.1 Getting Started
5.2 Demo 1: Sensor Device Based I2C
5.2.1 Sensor Device Base I2C
5.2.2 Writing Program
5.2.3 Testing

5.3 Demo 2: Actuator Device Based I2C


5.3.1 Actuator Device
5.3.2 Writing Program
5.3.3 Testing
6. mbed SPI
6.1 Getting Started
6.2 Demo 1: Hello SPI
6.2.1 Hardware Configuration
6.2.2 Writing Program
6.2.3 Testing
6.3 Demo 2: Digital Potentiometer Based SPI
6.3.1 Digital Potentiometer - MCP41x1 and MCP41x2
6.3.2 Wiring
6.3.3 Writing Program
6.3.4 Testing
7. mbed and Bluetooth Low Energy (BLE)
7.1 Getting Started
7.2 Hello iBeacon
7.3 Bluetooth Chat
7.3.1 Creating Application
7.3.2 Testing
7.3.2.1 Installing BLE Controller on iOS/Android
7.3.2.2 Running Serial App on PC
7.3.2.3 Testing Chatting
8. Controlling Servo Motor
8.1 Servo Motor
8.2 Hardware Implementation
8.3 Writing Program
8.4 Testing
Source Code

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

1. Setting Up Development Environment

1.1 ARM mbed Boards


mbed is a platform and operating system for internet-connected devices based on 32-bit ARM
Cortex-M microcontrollers. Further information about mbed development, you can visit on
https://developer.mbed.org/ .
To develop a program for ARM mbed, you need ARM board based mbed platform. You can find the
list of ARM mbed board on https://developer.mbed.org/platforms/ .
The following is a sample of ARM mbed board.
mbed LPC1768.

Mbed LPC1114FN28 - ARM Cortex-M0

For testing and evaluating, I use two mbed boards:


STM32 Nucleo F401RE
RedBearLab nRF51822
The following is the form of my ARM mbed board.

1.2 Getting Hardware


How to get ARM mbed board device?
Officially you can buy it from the official partner on https://developer.mbed.org/platforms/ . Click
one of your interest board. Then, you can see Buy button. Click it and you will be navigated to buying
website.

1.3 Development Tools

mbed development provides online tool which we can write program via browser so you don't need
install any tool.

1.4 Electronics Devices


We need electronic components to build our testing, for instance, Resistor, LED, sensor devices and
etc. I recommend you can buy electronic component kit. You find them on your local electronics shops

1.5 Setting up mbed Board to Computer


Each mbed board has a different approach on how to set up and connect to the board to your
computer. Basically, your ARM mbed board must be recognized by your local computer so we can
transfer our program from mbed online development tool to the board through computer.
By default, it will be detected because we already installed STM32 Nucleo driver. You can verify it
via Device Manager if you're working with Windows OS. You can also see flashdisk from your
board.

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 Hello mbed and STM32 Nucleo


In this section, we try to get started with mbed development. We build blinking led on STM32 Nucleo
board.

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.

1.6.2 Adding STM32 Nucleo as Target Board


After registered and logon, you can visit online development tool by clicking Compiler menu.

You will get the mbed IDE.

To start, you can click New -> New Program.

mbed tool will detect if you don't add a board platform.

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.

After added, you will get a confirmation, shown in Figure below.

Now you are ready to develop.

1.6.3 Creating Project


Create a new project. You will get a dialog, shown in Figure below.

Select your platform, Nucleo. Then, select Blinky LED test for the ST Nucleo boards.
If done, you will get a form as below.

You can navigate it to see the code.

1.6.4 Compiling
You can compile your program by clicking Compile menu.

If success, you will ge a save dialog.

Save the compiled code (*.bin) into your local computer.

1.6.5 Deploying Program to STM32 Nucleo Board


To deploy program into the board, it's easy. You can drag your compiled file (*.bin) into your Nucleo
flash drive.

You also can do by copying and pasting.

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 Hello mbed and RedBearLab nRF51822


In this section, we try to get started with mbed development using RedBearLab nRF51822.

1.7.1 Registration

To use mbed development, we must register. You can register on https://developer.mbed.org/ by


clicking signup menu on website.

1.7.2 Adding RedBearLab nRF51822 Board to mbed IDE


After registered and logon, you can visit online development tool by clicking Platforms menu.

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.

You will get a success confirmation.

Now you have already added your mbed board into IDE. You're ready to write program.

1.7.3 Creating Project


In this section, we create a new project with targeting on RedBearLab nRF51822.
Open mbed online IDE. Click menu New -> New Program.

You should see a dialog. Select your mbed Platform and Empty Program on Template. Fill a project
name. If done, click OK button.

After that, you see the empty project on your Workspace.

1.7.4 Adding mbed Library


Now you add mbed library into your project. Right-click on your project so you should see a context
menu.

Select Import Library -> From Import Wizard. After selected, you should see a list of library from
mbed.org.
Select mbed on Libraries tab.

You will get a dialog. Click Import button if finished.

Then, you should see mbed library that installed into your project.

1.7.5 Writing Program


The next step is to add our program. Right click on your project. Select menu New File.

Fill main.cpp on the dialog. If done, click OK button.

After that, open main.cpp file and write this code.


#include "mbed.h"
DigitalOut myled(P0_15);

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.

1.7.7 Deploying Program to RedBearLab nRF51822 Board


To deploy a program into RedBearLab nRF51822 board, you just copy your compiled file, *.hex, into
MBED removable disk.

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.

2. mbed Digital I/O

This chapter explains how to write mbed program to access mbed digital I/O.

2.1 Getting Started


mbed board provide digital I/O pins. You can see it on their datasheet. For instance, I have STM32
Nucleo F401RE and RedBearLab nRF51822 board. The following is the board pins.

2.2 Demo 1: Turning on LED by Pressing Pushbutton


In this chapter, we will focus on mbed board digital I/O programming. In our case, we develop a
simple app to illustrate how to read digital input and write to digital output from mbed board. We can
use DigitalOut object to write data to mbed board digital output and DigitalIn for reading mbed board
digital input.
The scenario is to connect a pushbutton and LED to the board. If we press pushbutton, we will turn on
LED. It's easy. Let's start.

2.2.1 Wiring Hardware


The following is hardware needed:

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.

2.2.2 Writing Program


Now you can write a program via mbed online, https://developer.mbed.org/compiler/ , called
LED_pusbutton with Empty template. Then, import mbed.org library. Read section 1.7.
On your main program, main.cpp, write this code.
#include "mbed.h"
DigitalOut myled(P0_13);
DigitalIn mybutton(P0_16);
int main() {
mybutton.mode(PullDown);
while(1) {
if(mybutton){
myled = 1; // LED is ON
}else{
myled = 0; // LED is OFF
}
wait(0.2); // 200 ms

}
}

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.

3. Serial Communication - UART

This chapter explains how to work with serial communication (UART) in mbed board.

3.1 ARM mbed UART


If you are working with Mac or Linux, mbed serial USB already detected but it needs serial driver for
Windows platform. You can download serial driver for mbed board on this website,
https://developer.mbed.org/handbook/Windows-serial-configuration . You can follow installation
steps on section 3.1.1.

3.1.1 Installing mbed Serial Driver for Windows Platform


After downloaded serial driver for Windows on https://developer.mbed.org/handbook/Windowsserial-configuration, you run the setup file. Don't forget to disconnect your mbed board from
computer.
After run the setup file, you will get a dialog, shown in Figure below.

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.

Now you are ready to write program.

3.2 Demo 1: Hello mbed UART


The first demo is to access mbed UART which display it using Serial monitor application. To write
and read data on UART, you can use Serial object. You can read it on
https://developer.mbed.org/handbook/Serial .
Our scenario is to write data into UART. Then, we will see it through Serial application on PC.

3.2.1 Wiring

You don't do anything on your board. You just connect your mbed board to your PC.

3.2.2 Writing Program


Now you can write a program via mbed online, https://developer.mbed.org/compiler/ , called
Serial_toPC with Empty template. Then, import mbed.org library. Read section 1.7.
On your main program, main.cpp, write this code.
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
int main() {
int counter = 1;
while(1) {
pc.printf("counter=%d \n",counter);
counter++;
if(counter>50)
counter = 1;
wait(1.0);
}
}

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.

3.3 Demo 2: UART Pingpong


The second demo is to build app which collaborate between mbed board and Arduino. You can use
any device or sensor device based on UART.
Let's start.

3.3.1 Hardware Configuration


As we know, we can see UART pins on mbed board and Arduino. To communicate each other, we
connect mbed board TX to Arduino RX and mbed board RX to Arduino TX.
mbed board TX --> Arduino RX (Digital/PWM 0)
mbed board RX --> Arduino TX (Digital/PWM 1)
mbed board GND --> Arduino GND

The following is hardware wiring.

3.3.2 Writing Program for Arduino


In this step, we develop app for Arduino. You can use Windows, Linux or MAC platform to write
program for Arduino. For illustration, I use Windows platform to develop app.
Firstly, you connect your Arduino to computer. After that, you can verify it on Device Manager.

Using Arduino software, http://arduino.cc/en/Main/Software, we can write a program. Write this


following code.
int data = 97;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.write((char)data);
data++;
if(data>110)
data = 97;
delay(1000);
}

Before you upload code to Arduino, you must disconnect Arduino UART from mbed board UART.
Now you can upload the program into Arduino.

3.3.3 Writing Program for mbed


Now you can write a program for mbed board. Open mbed online,
https://developer.mbed.org/compiler , create a project, called mbed_uart_arduino with Empty
template. Import mbed library, read section 1.7.
Add main.cpp file for main program. Then, write this code.
#include "mbed.h"
Serial pc(USBTX, USBRX); // to pc
Serial uart(P0_9, P0_11); // tx, rx
int main() {
while(1) {
if(uart.readable()) {
pc.printf("recv=%c \n",uart.getc());
}
wait(1.0);
}
}

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.

A sample output can be seen in Figure below.

4. mbed Analog I/O

This chapter explains how to write mbed program to access mbed Analog I/O.

4.1 Getting Started


To illustrate how to work with analog I/O on mbed board, we build two demo about analog input and
analog output. You can follow the demo instructions on next section.

4.2 Demo 1: Reading Analog Input - Analog Sensor


In this section, we learn how to read analog input on mbed board. For illustration, I use Potentiometer
as analog input source. Let's start!.

4.2.1 Hardware Wiring


To understand Potentiometer, you see its scheme in Figure below.

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.

4.2.2 Writing Program

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);
}
}

We call read() function to read analog input from AnalogIn object.


Save this code. Compile and upload it into mbed board.

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.

4.3 Demo 2: Writing Analog Output (PWM) - RGB LED


The last section, we learn how to work with analog output on mbed board. We use RGB LED which
sets colors by giving R, G and B values on its pins. We use mbed board Analog output (PWM). In this
demo I used RedBearLab nRF51822 board.
RedBearLab nRF51822 board has three PWM pins as follows:
D9 (P0_18)
D6 (P0_16)
D5 (P0_23)
Please check your mbed board PWM on the datasheet.

4.3.1 RGB LED


In this scenario we build an application to control RGB LED color using mbed board Analog output
(PWM). RGB LED has 4 pins that you can see it in Figure below.

To understand these pins, you can see the following Figure.

Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue

Now we can start to build an application and hardware implementation.

4.3.2 Hardware Configuration


To connect RGB LED pins to mbed board (I used RedBearLab nRF51822 board), we can configure
the following schema.
RGB LED pin 1 (red) is connected to RedBearLab nRF51822 board PWM pin D9 (P0_18)
RGB LED pin 2 is connected to RedBearLab nRF51822 board VCC 5V
RGB LED pin 3 (green) is connected to RedBearLab nRF51822 board PWM pin D6 (P0_16)
RGB LED pin 4 (blue) is connected to RedBearLab nRF51822 board PWM pin D5 (P0_23)
Note: you can use resistor to handle voltage issues.
Hardware implementation can be seen in Figure below.

4.3.2 Writing Program


To write analog output via PWM, we can use PwmOut object. You can read it on
https://developer.mbed.org/handbook/PwmOut .
Now you can create a new project, called analog_output, via mbed online. Import mbed library.
Read section 1.7.
On main program, main.cpp, write this code.
#include "mbed.h"
Serial
PwmOut
PwmOut
PwmOut

pc(USBTX, USBRX); // tx, rx


red(P0_18);
green(P0_16);
blue(P0_23);

void set_color(float redVal, float greenVal, float blueVal){


red = redVal;
green = greenVal;
blue = blueVal;
}
int main() {
while(1) {
set_color(0, 1, 1); //
pc.printf("red\n");
wait(1.5);
set_color(1, 0, 1); //
pc.printf("green\n");
wait(1.5);
set_color(1, 1, 0); //
pc.printf("blue\n");
wait(1.5);
set_color(0, 0, 1); //
pc.printf("yellow\n");
wait(1.5);
set_color(0.7, 1, 0.7);
pc.printf("purple\n");
wait(1.5);
set_color(1, 0, 0); //
pc.printf("aqua\n");
wait(1.5);
}
}

red

green

blue

yellow

// purple

aqua

Save this code.


Compile and upload this program into your mbed board.

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.

RGB output for blue color.

RGB output for yellow color.

5. mbed I2C/TWI

This chapter explains how to work with I2C on mbed board and write program to access I2C.

5.1 Getting Started


The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s to allow easy
communication between components which reside on the same circuit board. TWI stands for Two
Wire Interface and for most marts this bus is identical to IC. The name TWI was introduced by Atmel
and other companies to avoid conflicts with trademark issues related to IC.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). Check your
mbed board datasheet if it provides I2C interface or not. The following is I2C bus on STM32 Nucleo
F401RE and RedBearLab nRF51822 boards.

To connect device via I2C, you can do the following connection.

You should define which to be master and slave.


We also can define 1 master and two slave devices.

To access I2C bus, We can use I2C library from mbed.


There are many devices which use I2C to communicate to other. The following is a list of sample
device with I2C enabled:
EEPROM IC with I2C support
ADC/DAC with I2C support, for instance, http://www.adafruit.com/product/935
Real Time Clock (RTC), http://www.adafruit.com/products/264
LCD+Keypad module with I2C support, for instance, http://www.adafruit.com/product/1109

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

5.2 Demo 1: Sensor Device Based I2C

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!.

5.2.1 Sensor Device Base I2C


Now you can connect your PCF8591 AD/DA to mbed board . For testing, I use the third I2C
on RedBearLab nRF51822 board and mbed online as development tool.
The following is demo wiring :
Module SCL --> mbed board SCL
Module SDA --> mbed board SDA
Module VCC ---> mbed board +3.3V
Module GND ---> mbed board GND
Based on mbed I2C library document, we must set pull up resistor to SDA and SCL to VCC +3V. I
used two resistors 10 k. Further information what pull-up resistor is, you can read it
on https://learn.sparkfun.com/tutorials/pull-up-resistors .
The following is a sample of hardware implementation.

5.2.2 Writing Program


To access I2C on mbed, we can use mbed library. We also use PCF8591 library, written by Wim
Huiskamp. Create a new project, called I2C_sensor_demo, on mbed online. Import mbed and
PCF8591 libraries.

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);
}
}

The following is a list of steps to access I2C:


Instantiate I2C object by passing I2C pins
Then I2C object is passed into PCF8591 object and set I2C address, PCF8591_SA0
Send a command to access a sensor device which you want to get sensor data using read()
function
The following is PCF8591 module address for sensor:
LDR on channel 0
Photo-voltaic cell on channel 1
Potmeter on channel 3

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

Serial Monitor to read sensor data.


The following is a sample output using CoolTerm program.

5.3 Demo 2: Actuator Device Based I2C


The objective of this section is to illustrate how to read data on I2C bus. We use PCF8591 AD/DA
converter module with actuator device as I2C external source. This module has an actuator, LED. We
pass a digital value between 0 and 255 to the module.
Let's start!.

5.3.1 Actuator Device


You can see an actuator device, LED, on PCF8591 AD/DA converter module in Figure below. See
red arrow.

To connect this module to mbed board, you can read configuration on section 5.2.1

5.3.2 Writing Program


To access I2C on mbed, we can use mbed library. We also use PCF8591 library, written by Wim
Huiskamp. Create a new project, called I2C_actuator_demo, on mbed online. Import mbed and
PCF8591 libraries.

The following is a code for mbed on main.cpp file


#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 = 0;
while (1) {
adc_dac.write(analog);
pc.printf("write analog: %d \r\n",analog);
wait(1);
analog = analog + 25;
if(analog>255)
analog = 0;
}
}

Save this code. Compile and upload it to mbed board.

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.

6.1 Getting Started


The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one or more
slave peripheral integrated circuits (ICs) to a single master SPI device; usually a microcontroller or
microprocessor of some sort.
SPI in mbed board can be defined on the following pins:
MOSI
MISO
SCK

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.

(image source: http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus)

6.2 Demo 1: Hello SPI


The objective of the first demo is to get started how to work with SPI on mbed board. In this
scenario, we apply SPI loopback. To access mbed SPI, we can use SPI object. You can read it on
https://developer.mbed.org/handbook/SPI .

6.2.1 Hardware Configuration


Hardware configuration is easy. You just connect MOSI and MISO pins with each other. In this demo,
I used RedBearLab nRF51822 board so I connect MOSI (P0_20) pin to MISO (P0_22) pin through a
jumper cable.
The following is a sample of hardware configuration.

6.2.2 Writing Program


Now we can develop a new app using mbed online, https://developer.mbed.org/compiler/ . Create a
new project, called SPI_Demo, with Empty Template. Then, import mbed library. Read section 1.7.
After that, you can add main.cpp file for main program. Write this code.
#include "mbed.h"
SPI spi(P0_20, P0_22, P0_25); // mosi, miso, sclk
Serial pc(USBTX, USBRX); // tx, rx
int main() {
int data = 97;
char recv;
spi.format(8,0);
spi.frequency(1000000);
pc.printf("SPI initialization\n");
while(1) {
recv=spi.write((char)data);
pc.printf("send=%c , recv=%c \n",(char)data,recv);
wait(1.5);

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 Demo 2: Digital Potentiometer Based SPI


The second demo is to control Potentiometer value from mbed board SPI. We use a digital
Potentiometer IC which connected to LED. You can see a brightness change on LED.

6.3.1 Digital Potentiometer - MCP41x1 and MCP41x2


There are many digital Potentiometer ICs based SPI you can use to illustrate how to work with mbed
SPI, for instance MCP41x1 and MCP41x2 from Microchip. For testing, I used MCP4132. You can
read its datasheet on http://www.microchip.com/wwwproducts/Devices.aspx?product=MCP4132 .
The following is IC wiring.

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

If you use IC MCP41x1, you can do the following wiring.


MCP41x1 CS is connected to mbed D10 (P0_14)
MCP41x1 SCK is connected to mbed SCK (P0_25)
MCP41x1 SDI/SDO is connected to mbed MOSI (P0_20)
MCP41x1 Vss and P0B are connected to mbed GND
MCP41x1 VDD and P0A are connected to mbed VCC +5V
MCP41x1 P0W is connected to LED. One of LED pins is connected to GND

If you use IC MCP41x2, you can do the following wiring.


MCP41x2 CS is connected to mbed D10 (P0_14)
MCP41x2 SCK is connected to mbed SCK (P0_25)

MCP41x2 SDI is connected to mbed MOSI (P0_20)


MCP41x2 SDO is connected to mbed MISO (P0_22)
MCP41x2 Vss and P0B are connected to mbed GND
MCP41x2 VDD is connected to mbed VCC +5V
MCP41x2 P0W is connected to LED. One of LED pins is connected to GND

The following is my hardware implementation, RedBearLab nRF51822 board and MCP4132.

6.3.3 Writing Program


Now you can create a new project using mbed online, https://developer.mbed.org/compiler/ , called
SPI_pot_digital, with Empty template. Then, import mbed library.
The next step is to add main.cpp file as main program. Write this code.
#include "mbed.h"

SPI spi(P0_20, P0_22, P0_25); // mosi, miso, sclk


DigitalOut cs(P0_14); // cs
Serial pc(USBTX, USBRX); // tx, rx
void set_pot_value(int value){
cs = 0; // cs = LOW
spi.write(0);
spi.write(value);
cs = 1; // cs = HIGH
}
int main() {
int i;
pc.printf("SPI initialization\n");
while(1) {
i=0;
while(1) {
// looping from i= 0 to 255 with increment 10
set_pot_value(i);
pc.printf("set pot=%d \n",i);
wait(0.5);
if(i==255)
break;
i = i + 10;
if(i>255)
i = 255;
}
i = 255;
while(1) {
// looping from i= 255 to 0 with increment 10
set_pot_value(i);
pc.printf("set pot=%d \n",i);
wait(0.5);
if(i==0)
break;
i = i - 10;
if(i<0)
i = 0;
}
}
}

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.

7. mbed and Bluetooth Low Energy (BLE)

This chapter explains how to work with Bluetooth low energy (BLE) on mbed board.

7.1 Getting Started


Bluetooth low energy (BLE) is a wireless technology standard for personal area networks. BLE is
targeted for very low power devices. In this chapter we learn how to work with BLE on mbed
environment.
In this chapter, we try to run two app samples from mbed related to BLE, iBeacon and BLE chatting.
You can use mbed board with BLE module or attach BLE shield to your board. For testing, I use
RedBearLab nRF51822 board.
Let's start.

7.2 Hello iBeacon


The first demo is to learn how to work iBeacon. Firstly, we import BLE_iBeacon from Programs on
mbed online.

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;

uint16_t txPower = 0xC8;


int main(void)
{
ble.init();
iBeaconService ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
ble.setAdvertisingInterval(1000); /* 1000ms. */
ble.startAdvertising();
while(1) {
ble.waitForEvent(); // allows or low power operation
}
}

You can change uuid, majorNumber, minorNumber and txPower.


Now you can compile and upload to mbed board, RedBearLab nRF51822.
After that, you can install BLE scan, for instance, Bluetooth LE Scanner for Android.

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 Bluetooth Chat


The second demo is to build a chat app based BLE. mbed board with BLE will connect to smartphone
iOs/Android with BLE feature. After connected, these app can exchange message. mbed board uses
UART to send and receive message.
In this demo I use RedBearLab nRF51822 board.

7.3.1 Creating Application


We uses a sample program RedBearLab. You can create a project with RedBearLab BLE Control.

After created, you see the following code.

Save this program.


Now you can compile and upload it to mbed board.

7.3.2 Testing
Now we test our program.

7.3.2.1 Installing BLE Controller on iOS/Android


Firstly, we install RedBearLab BLE Controller on iOS or Android.

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 see chat dialog.

7.3.2.2 Running Serial App on PC


On PC side which mbed board is connected you open Serial app. I use CoolTerm.

Don't forget to check Local Echo.

Save this setting. Try to connect.

7.3.2.3 Testing Chatting


Now you can try to chat. Type any message and tap Send button if done.

You should this message on Serial app from PC.

Now you can use Serial app from PC to type any message. Then, press Enter to send.

You should see this message on BLE Chat from iOS/Android.

8. Controlling Servo Motor

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.

8.1 Servo Motor


Servo motor provides a shaft movement 360 degree. We can control this movement based on its
degree. In this scenario, you can use any DC motor (servo) that will be connected to mbed board. I
used a mini servo from Arduino Sidekick Basic kit.
The following is a picture of my mini servo motor.

To understand servo's cables, you can identify as follows:


Red cable is be connected to +5V
Black or brown cable is be connected to GND
The rest (yellow or orange cable) is be connected to mbed board PWM pin

The next step we are going to build a program with mbed board and servo motor.

8.2 Hardware Implementation


To build hardware implementation, you can connect servo motor to mbed board by following
configuration:
Red cable is be connected to external power +5V
Black or brown cable is be connected to GND
The rest (yellow or orange cable) is be connected to mbed board PWM pin. I used pin P0_18
(D9)

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.

8.3 Writing Program


In this section, we create a program to control servo motor using mbed online. Create a new project,
called Servo_motor_demo, with Empty template. Import mbed library.
Write the following code on main.cpp file.
#include "mbed.h"
#include "Servo.h"
#define SERVO_PIN

P0_18

//D9

Serial pc(USBTX, USBRX); // to pc


Servo myservo(SERVO_PIN);
int main() {
int degree = 0;
int direction = 1; // 1 +, 2 while(1) {
pc.printf("degree=%d \n",degree);
myservo.write(degree);

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

The following is implementation for servo.cpp file.

#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

Source code can be downloaded on http://www.aguskurniawan.net/book/mbed170515.zip.

Contact

If you have question related to this book, please contact me at [email protected] . My blog:
http://blog.aguskurniawan.net.

You might also like