First Mbed Application
Building an Mbed Application¶
This tutorial will guide you through creating an Mbed application. We first will cover Mbed OS to read/write to sensors and peripherals on the Mbed Connect Cloud board.
Prerequisites¶
- An Mbed account - sign up here
- Install the Mbed serial driver (for Windows PCs)
Required Hardware¶
Setup¶
- Connect the board to your computer via the micro USB port.
- The board mounts as a mass-storage device (like a USB drive). Verify that you can see it (the drive name will be DAPLINK).
- Add the u-blox EVK-ODIN-W2 board to your online Mbed Compiler here
Making an Mbed application¶
Import the example program.
Import programMbed-Connect-Cloud-Demo
Demo for the Mbed Connect Cloud board
Printing to the LCD screen¶
We will add some code to print a message to the LCD screen on the board.
Open main.cpp
Declare an object to instantiate the driver for the LCD screen. Under 'GLOBAL VARIABLES HERE' add the following code:
C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
Add a function that prints a message to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void lcd_print(const char* message) { lcd.cls(); lcd.locate(0, 3); lcd.printf(message); }
Invoke the previous function to print "Hello World!" to the LCD screen. Under 'MAIN CODE HERE' add the following code:
lcd_print("Hello World!");
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- "Hello World!" should show on the LCD screen
Toggling the LED¶
We will add some code to toggle the LED when we push the button (SW1) down.
Open main.cpp
Instantiate a DigitalOut
object to write to the LED and an InterruptIn
object to read the button input. Under 'GLOBAL VARIABLES HERE' add the following code:
DigitalOut led(PB_8, 1); InterruptIn button(PF_2);
Add a toggle_led
function to switch the state of the LED. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void toggle_led() { led = !led; }
Whenever the button is pressed, the toggle_led
function will be called (interrupting the main program then returning). Under 'MAIN CODE HERE' add the following code:
button.rise(&toggle_led);
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The LED will now toggle it's state (on/off) if you push the button (SW1) down
Reading the temperature/humidity sensor¶
We will add some code to read the values from the temperature/humidity sensor and print the values to the LCD screen.
Open main.cpp
Instantiate a Sht31
object to read from the temperature/humidity sensor. Under 'GLOBAL VARIABLES HERE' add the following code:
Sht31 temp_sensor(PF_0, PF_1);
Add a function that reads the temperature/humidity sensor and prints the values to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_temp() { float t = temp_sensor.readTemperature(); float h = temp_sensor.readHumidity(); char val[32]; sprintf(val, "TEMP: %3.2fC, HUM: %3.2f%%", t, h); lcd_print(val); }
Call read_temp
every 2 seconds from a while loop in the main function. Under 'MAIN CODE HERE' add the following code:
while(1) { // WHILE LOOP CODE HERE read_temp(); wait_ms(2000); }
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The humidity and temperature values should now be displayed and updating on the LCD screen
Reading the air quality sensor¶
We will add some code to read the values from the air quality sensor and print the values to the LCD screen.
Open main.cpp
Instantiate a CCS811
object to read from the air quality sensor. Under 'GLOBAL VARIABLES HERE' add the following code:
CCS811 air_sensor(PF_0, PF_1);
Add a function that reads the air quality sensor and prints the values to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_air() { air_sensor.init(); uint16_t eco2, tvoc; air_sensor.readData(&eco2, &tvoc); char val[32]; sprintf(val, "eCO2: %dppm, TVOC: %dppb", eco2, tvoc); lcd_print(val); }
Call read_air
every 2 seconds from the while loop we created previously for the temperature/humidity sensor. Under 'WHILE LOOP CODE HERE' add the following code:
read_air(); wait_ms(2000);
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The air quality and temperature/humidity values should now be displayed and updating on the LCD screen (every 2 seconds)
Reading the light sensor¶
We will add some code to read the values from the light/luminosity sensor and print the values to the LCD screen.
Open main.cpp
Instantiate a TSL2561
object to read from the light sensor. Under 'GLOBAL VARIABLES HERE' add the following code:
TSL2561 light_sensor(PF_0, PF_1, TSL2561_ADDR_HIGH);
Add code to the main function that starts the light sensor and sets the initial timing and gain of the sensor. Under 'MAIN CODE HERE' add the following code:
light_sensor.begin(); light_sensor.setGain(TSL2561_GAIN_0X); light_sensor.setTiming(TSL2561_INTEGRATIONTIME_402MS);
Add a function that reads the light sensor and prints the values to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_light() { int vis = light_sensor.getLuminosity(TSL2561_VISIBLE); int infr = light_sensor.getLuminosity(TSL2561_INFRARED); char val[32]; sprintf(val, "VIS: %d, INFR: %d ", vis, infr); lcd_print(val); }
Call read_light
every 2 seconds from the while loop. Under 'WHILE LOOP CODE HERE' add the following code:
read_light(); wait_ms(2000);
Your main function should now look like this:
int main() { // MAIN CODE HERE lcd_print("Hello World!"); button.rise(&toggle_led); light_sensor.begin(); light_sensor.setGain(TSL2561_GAIN_0X); light_sensor.setTiming(TSL2561_INTEGRATIONTIME_402MS); while(1) { // WHILE LOOP CODE HERE read_temp(); wait_ms(2000); read_air(); wait_ms(2000); read_light(); wait_ms(2000); } }
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The light sensor should now be displayed and updating on the LCD screen (every 2 seconds after the other sensor values)
Reading the accelerometer¶
We will add some code to read the values from the accelerometer and print the values to the LCD screen.
Open main.cpp
Instantiate a MMA7660
object to read from the light sensor. Under 'GLOBAL VARIABLES HERE' add the following code:
MMA7660 accel(PF_0, PF_1);
Add a function that reads the accelerometer and prints the values to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_accel() { float x = accel.x(); float y = accel.y(); float z = accel.z(); char val[32]; sprintf(val, "x=%.2f y=%.2f z=%.2f", x, y, z); lcd_print(val); }
Call read_accel
every 2 seconds from the while loop. Under 'WHILE LOOP CODE HERE' add the following code:
read_accel(); wait_ms(2000);
Compile and run¶
- Select the u-blox EVK-ODIN-W2 target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The accelerometer values should now be displayed and updating on the LCD screen (every 2 seconds after the other sensor values)
Reading the laser distance sensor¶
A detailed implementation using the laser distance sensor can be viewed here.
Publishing changes¶
- Right click the project folder
- Select "Publish"
- Write a commit message in the "Revision Commit" menu
- Press OK
- Select "Fork..." in the "Publish Repository" menu
- Write a Description
- Press OK