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


Timeout class hierarchy

Use the Timeout interface to set up an interrupt to call a function after a specified delay.

You can create any number of Timeout objects, allowing multiple outstanding interrupts at the same time.

Warnings and notes

  • Timers are based on 32-bit int microsecond counters, so they can only time up to a maximum of 2^31-1 microseconds (30 minutes). They are designed for times between microseconds and seconds. For longer times, you should consider the time() real time clock.

  • No blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general.

  • No printf, malloc or new in ISR: Avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.

Timeout class reference

Protected Member Functions
virtual void handler ()
 Protected Member Functions inherited from mbed::Ticker
void setup (us_timestamp_t t)
 Protected Member Functions inherited from mbed::TimerEvent
void insert (timestamp_t timestamp)
void insert_absolute (us_timestamp_t timestamp)
void remove ()
Additional Inherited Members
 Public Member Functions inherited from mbed::Ticker
 Ticker (const ticker_data_t *data)
void attach (Callback< void()> func, float t)
template<typename T , typename M >
void attach (T *obj, M method, float t)
void attach_us (Callback< void()> func, us_timestamp_t t)
template<typename T , typename M >
void attach_us (T *obj, M method, us_timestamp_t t)
void detach ()
 Public Member Functions inherited from mbed::TimerEvent
 TimerEvent (const ticker_data_t *data)
virtual ~TimerEvent ()
 Static Public Member Functions inherited from mbed::TimerEvent
static void irq (uint32_t id)
 Protected Attributes inherited from mbed::Ticker
us_timestamp_t _delay
Callback< void()> _function
bool _lock_deepsleep
 Protected Attributes inherited from mbed::TimerEvent
ticker_event_t event
const ticker_data_t_ticker_data

Timeout hello, world

Set up a Timeout to invert an LED after a given timeout:

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
#include "mbed.h"
Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
void flip() {
    led2 = !led2;
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;

Timeout example

Try this example to attach a member function:

#include "mbed.h"

// A class for flip()-ing a DigitalOut 
class Flipper {
    Flipper(PinName pin) : _pin(pin) {
        _pin = 0;
    void flip() {
        _pin = !_pin;
    DigitalOut _pin;

DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;

int main() {
    // the address of the object, member function, and interval
    t.attach(callback(&f, &Flipper::flip), 2.0); 

    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
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.