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 program

00001 #include "mbed.h"
00002  
00003 DigitalIn enable(p5);
00004 DigitalOut led(LED1);
00005  
00006 int main() {
00007     while(1) {
00008         if(enable) {
00009             led = !led;
00010         }
00011         wait(0.25);
00012     }
00013 }

Import program

00001 #include "mbed.h"
00002  
00003 DigitalIn enable(PTB0);
00004 DigitalOut led(LED_BLUE);
00005 
00006 int main() {
00007     while(1) {
00008         if(enable) {
00009             led = !led;
00010         }
00011         wait(0.25);
00012     }
00013 }

API

API summary

Import library

Public Member Functions

  DigitalIn (PinName pin)
  Create a DigitalIn connected to the specified pin.
  DigitalIn (PinName pin, PinMode mode)
  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.
int  is_connected ()
  Return the output setting, represented as 0 or 1 (int)
  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;
    }
}