Mbed OS Reference
Loading...
Searching...
No Matches
DigitalIn Class Reference

A digital input, used for reading the state of a pin. More...

#include <DigitalIn.h>

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...
 
 ~DigitalIn ()
 Class destructor, deinitialize the 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...
 

Detailed Description

A digital input, used for reading the state of a pin.

Note
Synchronization level: Interrupt safe

Example:

// 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;
}
ThisThread::sleep_for(250);
}
}
A digital input, used for reading the state of a pin.
Definition: DigitalIn.h:60
A digital output, used for setting the state of a pin.
Definition: DigitalOut.h:55

Definition at line 56 of file DigitalIn.h.

Constructor & Destructor Documentation

◆ DigitalIn() [1/2]

DigitalIn ( PinName  pin)

Create a DigitalIn connected to the specified pin.

Parameters
pinDigitalIn pin to connect to

Definition at line 67 of file DigitalIn.h.

◆ DigitalIn() [2/2]

DigitalIn ( PinName  pin,
PinMode  mode 
)

Create a DigitalIn connected to the specified pin.

Parameters
pinDigitalIn pin to connect to
modethe initial mode of the pin

Definition at line 78 of file DigitalIn.h.

◆ ~DigitalIn()

~DigitalIn ( )

Class destructor, deinitialize the pin.

Definition at line 86 of file DigitalIn.h.

Member Function Documentation

◆ read()

int read ( )

Read the input, represented as 0 or 1 (int)

Returns
An integer representing the state of the input pin, 0 for logical 0, 1 for logical 1

Definition at line 97 of file DigitalIn.h.

◆ mode()

void mode ( PinMode  pull)

Set the input pin mode.

Parameters
pullPullUp, PullDown, PullNone, OpenDrain

◆ is_connected()

int is_connected ( )

Return the output setting, represented as 0 or 1 (int)

Returns
Non zero value if pin is connected to uc GPIO 0 if gpio object was initialized with NC

Definition at line 114 of file DigitalIn.h.

◆ operator int()

operator int ( )

An operator shorthand for read()

See also
DigitalIn::read()
DigitalIn button(BUTTON1);
DigitalOut led(LED1);
led = button; // Equivalent to led.write(button.read())

Definition at line 128 of file DigitalIn.h.