Digital Input

Example 1

Connect as follows

Pin in mbedTo
VOUTJumper Wire
VOUT47 Ω resistor
  1. Observe that LED1 starts blinking when the free end of the jumper wire in contact with the free end of the resistor.
  2. When the connection breaks, the blinking stops.

/media/uploads/yoonghm/digitalinput1.jpg

#include "mbed.h"

DigitalIn  enable(p30);
DigitalOut myled(LED1);

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

Warning

Most of the pin in mbed board can only take up 3.3 V and 40 mA. Never connect an input with voltage larger than 3.3 V or larger than 40 mA.

Example 2

Connect as follows

/media/uploads/yoonghm/digitalinput2a.jpg /media/uploads/yoonghm/digitalinput2b.jpg

Pin in mbedTo
VOUTOutermost of the external track
GNDInnermost of the lowest track
Pin 30Side A of button -> 47 Ω resistor -> VOUT
Pin 30Side A of button -> GND
  1. If the button is pressed, the digital input enable becomes false (0V). LED1 starts blinking.
  2. When the button is released, the digital input enable becomes true (+3.3V). LED1 stop blinking.

#include "mbed.h"

DigitalIn  enable(p30);
DigitalOut myled(LED1);

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

Example 3

Connect as follows

/media/uploads/yoonghm/digitalinput3a.jpg /media/uploads/yoonghm/digitalinput3b.jpg

Pin in mbedTo
VOUTOutermost of the external track
GNDInnermost of the lowest track
Pin 30Side A of button -> 47 Ω resistor -> GND
Pin 30Side B of button -> VOUT
  1. If the button is pressed, the digital input enable becomes true (+3.3V). LED1 starts blinking.
  2. When the button is released, the digital input enable becomes false (0V). LED1 stop blinking.

#include "mbed.h"

DigitalIn  enable(p30);
DigitalOut myled(LED1);

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

Example 4

Using internal pull-up resistor, we can save external resistor and connection.

Connect as follows

/media/uploads/yoonghm/digitalinput4a.jpg /media/uploads/yoonghm/digitalinput4b.jpg

Pin in mbedTo
VOUTOutermost of the external track
GNDInnermost of the lowest track
Pin 30Side A of button -> GND
  1. If the button is pressed, the digital input enable becomes false (0V). LED1 starts blinking.
  2. When the button is released, the digital input enable becomes true (+3.3V). LED1 stop blinking.

#include "mbed.h"

DigitalIn  enable(p30);
DigitalOut myled(LED1);

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

Example 5

Connect as follows

/media/uploads/yoonghm/digitalinput5a.jpg /media/uploads/yoonghm/digitalinput5b.jpg

Pin in mbedTo
VOUTOutermost of the external track
GNDInnermost of the lowest track
Pin 30Side A of button -> VOUT
  1. If the button is pressed, the digital input enable becomes true (+3.3V). LED1 starts blinking.
  2. When the button is released, the digital input enable becomes false (0V). LED1 stop blinking.

#include "mbed.h"

DigitalIn  enable(p30);
DigitalOut myled(LED1);

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


1 comment on Digital Input:

03 Oct 2018

To use internal PullUp or PullDown resistors, you might use inside main function before while loop:

enable.mode(PullUp);

Please log in to post comments.