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

DigitalIn

Use the DigitalIn interface to read the value of a digital input pin. The logic level is either 1 or 0.

You can use any of the numbered Arm Mbed pins as a DigitalIn.

DigitalIn class reference

Public Member Functions
 DigitalIn (PinName pin)
 Create a DigitalIn connected to the specified pin. More...
 DigitalIn (PinName pin, PinMode mode)
 Create a DigitalIn connected to the specified pin. More...
int read ()
 Read the input, represented as 0 or 1 (int) More...
void mode (PinMode pull)
 Set the input pin mode. More...
int is_connected ()
 Return the output setting, represented as 0 or 1 (int) More...
 operator int ()
 An operator shorthand for read() More...

DigitalIn hello, world

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

#include "mbed.h"

DigitalIn  mypin(SW2); // change this to the button on your board
DigitalOut myled(LED1);

int main()
{
    // check mypin object is initialized and connected to a pin
    if (mypin.is_connected()) {
        printf("mypin is connected and initialized! \n\r");
    }

    // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
    mypin.mode(PullNone);

    // press the button and see the console / led change
    while (1) {
        printf("mypin has value : %d \n\r", mypin.read());
        myled = mypin; // toggle led based on value of button
        ThisThread::sleep_for(250);
    }
}

DigitalIn example

To handle an interrupt, see InterruptIn.

Examples of logical functions - boolean logic NOT, AND, OR, XOR:

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

#include "mbed.h"

DigitalIn a(D0);
DigitalIn b(D1);
DigitalOut z_not(LED1);
DigitalOut z_and(LED2);
DigitalOut z_or(LED3);
DigitalOut z_xor(LED4);

int main()
{
    while (1) {
        z_not = !a;
        z_and = a && b;
        z_or = a || b;
        z_xor = a ^ b;
    }
}

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.