Learn Mbed-cpp - 10 Code Examples & CST Typing Practice Test
Mbed OS (mbed-cpp) is an open-source embedded operating system and C++ framework designed for ARM Cortex-M microcontrollers. It provides a full-featured platform for building IoT devices, supporting real-time operating system capabilities, peripheral access, and connectivity.
Learn MBED-CPP with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
Mbed C++ Button Press Counter
#include "mbed.h"
DigitalOut led(LED1);
InterruptIn button(USER_BUTTON);
int buttonCount = 0;
void updateUI() {
printf("Button Count: %d\n", buttonCount);
led = buttonCount % 2;
}
void buttonPressed() {
buttonCount++;
updateUI();
}
int main() {
button.rise(&buttonPressed);
updateUI();
while(1) {}
}
Counts button presses and toggles an LED on each press.
Mbed C++ LED Blink
#include "mbed.h"
DigitalOut led(LED1);
int main() {
while(1) {
led = !led;
printf("LED is %s\n", led ? "ON" : "OFF");
ThisThread::sleep_for(500ms);
}
}
Blinks an LED every 500ms using a timer.
Mbed C++ Temperature Monitor
#include "mbed.h"
int temperature = 25;
bool alert = false;
void updateUI() {
printf("Temperature: %d\n", temperature);
if (alert) printf("Alert: High Temperature!\n");
}
void readSensor() {
temperature += 2;
alert = temperature > 30;
updateUI();
}
int main() {
updateUI();
readSensor();
readSensor();
}
Simulates reading temperature and printing alerts when high.
Mbed C++ PWM LED Brightness
#include "mbed.h"
PwmOut led(LED1);
int brightness = 0;
void updateLED() {
led.write(brightness / 100.0);
printf("LED Brightness: %d\n", brightness);
}
void increaseBrightness() {
brightness += 20;
if(brightness > 100) brightness = 0;
updateLED();
}
int main() {
updateLED();
increaseBrightness();
increaseBrightness();
}
Adjusts LED brightness using PWM output.
Mbed C++ Relay Toggle
#include "mbed.h"
DigitalOut relay(D2);
int state = 0;
void updateUI() {
printf("Relay is %s\n", state ? "ON" : "OFF");
relay = state;
}
void toggleRelay() {
state = !state;
updateUI();
}
int main() {
updateUI();
toggleRelay();
toggleRelay();
}
Simulates a relay switch toggled by a button press.
Mbed C++ Buzzer Toggle
#include "mbed.h"
DigitalOut buzzer(D3);
bool isOn = false;
void updateUI() {
buzzer = isOn;
printf("Buzzer is %s\n", isOn ? "ON" : "OFF");
}
void toggleBuzzer() {
isOn = !isOn;
updateUI();
}
int main() {
updateUI();
toggleBuzzer();
toggleBuzzer();
}
Turns a buzzer on and off using a digital output.
Mbed C++ Fan Speed Control
#include "mbed.h"
PwmOut fan(D4);
int speed = 0;
void updateUI() {
fan.write(speed / 5.0);
printf("Fan Speed: %d\n", speed);
}
void increaseSpeed() {
speed++;
if(speed > 5) speed = 0;
updateUI();
}
int main() {
updateUI();
increaseSpeed();
increaseSpeed();
}
Controls fan speed using PWM steps.
Mbed C++ Dual LED Toggle
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
void updateLEDs(bool state1, bool state2) {
led1 = state1;
led2 = state2;
printf("LED1: %s, LED2: %s\n", state1 ? "ON" : "OFF", state2 ? "ON" : "OFF");
}
int main() {
updateLEDs(true, false);
updateLEDs(false, true);
}
Alternates two LEDs on and off.
Mbed C++ Humidity Alert
#include "mbed.h"
int humidity = 60;
bool alert = false;
void updateUI() {
printf("Humidity: %d\n", humidity);
if(alert) printf("Alert: High Humidity!\n");
}
void readSensor() {
humidity += 5;
alert = humidity > 70;
updateUI();
}
int main() {
updateUI();
readSensor();
readSensor();
}
Simulates a humidity sensor with alert threshold.
Mbed C++ LED Blink Pattern
#include "mbed.h"
DigitalOut led(LED1);
void blinkPattern() {
led = !led;
printf("LED is %s\n", led ? "ON" : "OFF");
}
int main() {
blinkPattern();
blinkPattern();
blinkPattern();
}
Blink an LED in a simple on/off pattern.
Frequently Asked Questions about Mbed-cpp
What is Mbed-cpp?
Mbed OS (mbed-cpp) is an open-source embedded operating system and C++ framework designed for ARM Cortex-M microcontrollers. It provides a full-featured platform for building IoT devices, supporting real-time operating system capabilities, peripheral access, and connectivity.
What are the primary use cases for Mbed-cpp?
Professional IoT and embedded device development. Real-time applications with RTOS support. Networking and connected devices (Wi-Fi, BLE, LoRa, Ethernet). Sensor and actuator control for robotics and automation. Prototyping and production-level embedded systems
What are the strengths of Mbed-cpp?
High-performance C++ environment. Robust RTOS and multi-threading support. Wide hardware and connectivity support. Production-grade embedded system features. Strong ecosystem with professional libraries and tools
What are the limitations of Mbed-cpp?
Requires C++ knowledge and embedded experience. Larger memory footprint than lightweight scripting languages. Steeper learning curve for beginners. Complexity increases for small-scale hobby projects. Less interactive than Python-based embedded scripting
How can I practice Mbed-cpp typing speed?
CodeSpeedTest offers 10+ real Mbed-cpp code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.