DigitalIn

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!

» Import this program

#include "mbed.h"
 
DigitalIn enable(p5);
DigitalOut led(LED1);
 
int main() {
    while(1) {
        if(enable) {
            led = !led;
        }
        wait(0.25);
    }
}

» Import this program

#include "mbed.h"
 
DigitalIn enable(PTB0);
DigitalOut led(LED_BLUE);

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

API

API summary

» Import latest build into a program

Public Member Functions

  DigitalIn (PinName pin)
  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()

Interface

The DigitalIn Interface can be used on any pin with a blue label.

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.

/media/uploads/chris/pinout-thumbnails.jpg
See the Pinout page for more details

To handle an interrupt, see InterruptIn

Examples of logical functions

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;
    }
}



1 related question:

13 comments:

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;

28 Nov 2012

How to use switch button ANDed with internal(embedded) LEDs to command external LEDs? For example:

if(led1==1&sw1==0) led1 is internal or embedded. LEDred7 = 1; external LED.

Thank you in advance.

28 Nov 2012

Feliciano:

code tags.

if(led1==1&sw1==0) //led1 is internal or embedded. 
LEDred7 = 1; //external 

That being said, do you have a whole program?
I would like to see what else you defined in there.

Lerche

05 Dec 2012

How can I just revise and check the pin mode related to pull up, pull down and open drain?

08 Dec 2012

Hi Mbed team,

I think it would be a good idea to show some code examples here about the possible pinmodes. (PullUp, PullDown) The only thing mentioned here is that there are default pulldown resistors present, but NOT how to change this. Is'nt this the right place for some explanation about this with small examples? I had to search all over the site for more information about this subject. Regards, LvdK

10 Dec 2012

Using enable for the pin is slightly confusing as it can be thought of as enabling the pin as a digital input

Posting comments for this page has been disabled