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

InterruptIn

InterruptIn class hierarchy

Use the InterruptIn interface to trigger an event when a digital input pin changes. You can trigger interrupts on the rising edge (change from 0 to 1) or falling edge (change from 1 to 0) of signals.

InterruptIn class reference

Public Member Functions
 InterruptIn (PinName pin)
 Create an InterruptIn connected to the specified pin. More...
 InterruptIn (PinName pin, PinMode mode)
 Create an InterruptIn connected to the specified pin, and the pin configured to the specified mode. More...
int read ()
 Read the input, represented as 0 or 1 (int) More...
 operator int ()
 An operator shorthand for read() More...
void rise (Callback< void()> func)
 Attach a function to call when a rising edge occurs on the input. More...
void fall (Callback< void()> func)
 Attach a function to call when a falling edge occurs on the input. More...
void mode (PinMode pull)
 Set the input pin mode. More...
void enable_irq ()
 Enable IRQ. More...
void disable_irq ()
 Disable IRQ. More...

Warnings:

  • 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 non re-entrant, and their behavior could be corrupted when called from an ISR.

  • For printfs from interrupt context, use Event instead.

To read an input, see DigitalIn.

InterruptIn hello, world

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

InterruptIn button(SW2);
DigitalOut led(LED1);
DigitalOut flash(LED_RED);

void flip()
{
    led = !led;
}

int main()
{
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while (1) {          // wait around, interrupts will interrupt this!
        flash = !flash;
        ThisThread::sleep_for(250);
    }
}

InterruptIn example

Try the following example to count rising edges on a pin.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

class Counter {
public:
    Counter(PinName pin) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
    {
        _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
    }

    void increment()
    {
        _count++;
    }

    int read()
    {
        return _count;
    }

private:
    InterruptIn _interrupt;
    volatile int _count;
};

Counter counter(SW2);

int main()
{
    while (1) {
        printf("Count so far: %d\n", counter.read());
        ThisThread::sleep_for(2000);
    }
}

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.