Recent changes
SerialPC
Creating a program
Downloading a program
Setup guide
Exporting to Code Red
Exporting to uVision
Order
tag order
From the mbed microcontroller Handbook.  

DigitalIn

/media/uploads/mbedofficial/digitalin_interfaces.png

The DigitalIn interface is used to read the value of a digital input pin.

Any of the numbered mbed pins can be used as a DigitalIn.

Hello World!

Flash an LED while a DigitalIn is true

#include "mbed.h"

DigitalIn enable(p5);
DigitalOut led(LED1);

int main() {
    while(1) {
        if(enable) {
            led = !led;
        }
        wait(0.25);
    }
}

API

API summary

DigitalInA digital input, used for reading the state of a pin
Functions
DigitalInCreate a DigitalIn connected to the specified pin
readRead the input, represented as 0 or 1 (int)
modeSet the input pin mode
operator int()An operator shorthand for read()
class DigitalIn : public Base
A digital input, used for reading the state of a pin
DigitalIn(PinName pin,  
const char *name =  NULL)
Create a DigitalIn connected to the specified pin
int read()
Read the input, represented as 0 or 1 (int)
void mode(PinMode pull)
Set the input pin mode
operator int()
An operator shorthand for read()

Details

The pin input is logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v. By default, the DigitalIn is setup with an internal pull-down resistor.

Examples

Boolean logic - NOT, AND, OR, XOR

#include "mbed.h"

DigitalIn a(p5);
DigitalIn b(p6);
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;
    }
}

Flash a LED while a pin is true, then exit

#include "mbed.h"

DigitalIn enable(p5);
DigitalOut led(LED1);

int main() {
    while(enable) {
        led = !led;
        wait(0.25);
    }
}

Related

To handle an interrupt, see InterruptIn




calendar Page history
Last modified 30 Oct 2010, by   user Simon Ford   tag No tags | 8 comments      

8 comments on DigitalIn:

30 Oct 2010

Last example should have a variable name before (p5), otherwise it gives an error related to the class constructor.

30 Oct 2010

Thanks, fixed.

13 Nov 2010

I can see from the schematics that the LED pins on the ethernet phy are routed to the µC. How can I name those pins so that I can read from them?

04 Feb 2011

Hi i want to capture the output of serial ADC which generates samples at the rate of 1.25MSPS, can i do it with the mbed ...?

13 Mar 2011

May i Know if i put the push On switch and 1 pin connect to the Mbed pin5 and another pin connect to which Mbed pin?sorry, i am newbie..Thanks

09 Jun 2011

@Alexandre Nunes - the pins corresponding to the two LEDs for the Ethernet PHY are not addressable using DigitallIn().

Calling Ethernet.link() would be the correct way to see if your PHY has link or not, if that's what you're ultimately trying to determine.


@Prashant Achari

It is probable that you can, but using DigitalIn() may not be the best (or a workable) way to do it. It may also depend on your ADC (the serial protocol, # of bits/sample, # of channels). This would be a question best raised in the forum with more complete information, if you have not done so already.

-

@Charles Lee

Where to connect the other switch pin to depends on how you want the switch to work (high or low when pressed, and whether or not you are using internal or external pullups). This would also be a good question to ask in the forum.

09 Jun 2011

What is the default (pullup) mode for a newly constructed DigitalIn()? It would seem like this is something that should be documented explicitly.

25 Jul 2011

the z_and with z_or should be z_and = a & b; z_or = a | b;

Please login to post comments.