Mistake on this page?
Report an issue in GitHub or email us

Alarm tutorial

This tutorial is for an alarm application that uses a simple countdown mechanism. It relies on two input buttons to set, activate and silence the alarm. During the countdown, the device is in sleep mode. When the countdown ends and the alarm triggers, an LED and a digital out pin go high. They go back to low when the alarm is reset.

The LEDs provides some feedback to the user: when setting the alarm, the LEDs blink to show the input was recognised. When the alarm is fully set, the LEDs blink the configured delay once, before letting the device go into sleep mode.

Tip: You can complete this tutorial with the Mbed Online Compiler or Mbed CLI.

Import the example application

If using Mbed CLI, use the import command:

mbed import mbed-os-example-alarm
cd mbed-os-example-alarm

If using the Online Compiler, click the Import into Mbed IDE button below:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
#include "mbed.h"

// Time constants in seconds
#define HOUR   60 * 60
#define MINUTE 60

// Globals
DigitalOut alarm_out(D2, 0);
DigitalOut alarm_led(LED_RED, 1);
DigitalOut hour_led(LED_GREEN, 1);
DigitalOut min_led(LED_BLUE, 1);

InterruptIn inc_time(BUTTON1);
InterruptIn sel(BUTTON2);

LowPowerTicker alarm_event;

volatile uint64_t delay        = 0;
volatile uint8_t  hour_count   = 0;
volatile uint8_t  min_count    = 0;
volatile uint8_t  select_state = 0;

// Timer Callbacks
void inc_select(void)
    if (select_state < 2) {
    } else {
        // Use select button to disable alarm
        alarm_out = 0;
        alarm_led = 1;

void set_time_leds(void)
    if (select_state == 0) {
        hour_led = !hour_led;
    } else {
        min_led = !min_led;

void inc_delay(void)
    if (select_state == 0) {
        delay += HOUR;
        hour_led = !hour_led;
    } else {
        delay += MINUTE;
        min_led = !min_led;

void trigger_alarm_out(void)
    alarm_out = 1;
    alarm_led = 0;

/* Use buttons to select the alarm time. Cycle through hours in an incrementing
 * fashion using Button1. Press select, and increment through minutes. Press
 * select one more time to begin the alarm timer.
 * The Time LEDs blink in time with the button inputs to show the
 * currently selected alarm time. Once you press select a second time to begin
 * the timer, the LEDs will blink out the configured delay in hours and
 * minutes before going into a low power sleep mode.
 * Once the alarm fires, pressing the select button will turn the alarm off
 * until the next time it fires.
 * You may also use the RTC (hardware or software through the Time API) to
 * set a real world time and set an alarm for a specific timestamp rather
 * than on a delay. However, this requires manually setting the time on each
 * reset, or an internet connection to automatically collect the
 * time.
// Main thread
int main()
    // Configure interrupt in pins (button controls)

    // Sleep while waiting for user input to set the desired delay
    while (select_state < 2) {

    // Once the delay has been input, blink back the configured hours and
    // minutes selected
    for (uint8_t i = 0; i < hour_count * 2; i++) {
        hour_led = !hour_led;

    for (uint8_t i = 0; i < min_count * 2; i++) {
        min_led = !min_led;

    // Attach the low power ticker with the configured alarm delay
    alarm_event.attach(&trigger_alarm_out, delay);

    // Sleep in the main thread
    while (1) {

Compile and flash to your board

  1. To compile the application:

    • If using Mbed CLI, invoke mbed compile, and specify the name of your target and toolchain (GCC_ARM, ARM, IAR). For example, for the ARM Compiler 5 and FRDM-K64F:
    mbed compile -m K64F -t ARM
    • If using the Online Compiler, click the Compile button.

    Your PC may take a few minutes to compile your code.

  2. Find the compiled binary:

    • If using the Online Compiler, the compiled binary will be downloaded to your default location.
    • If using Mbed CLI, the compiled binary will be next to the source code, in your local copy of the example.
  3. Connect your Mbed device to the computer over USB.

  4. Copy the binary file to the Mbed device.

  5. Press the reset button to start the program.

Use the alarm

The alarm isn't set to a timestamp; it counts down from the moment it's activated. So to set the alarm, specify the countdown duration:

  1. Press Button1 for the number of desired hours to delay.
  2. Press Button2 to cycle to minutes, and repeat the previous step for the number of desired minutes.
  3. Press Button2 again to start the alarm.
  4. Press Button2 again once the alarm triggers to silence it.

Extending the application

You can set the alarm to a specific time by relying on either the board's RTC or the time API. You will need to set the time on each reset, or rely on an internet connection and fetch the time.


If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.