0% found this document useful (0 votes)
17 views

Tutorials - Digital Input: Circuitry

The document discusses using a push button as a digital input for an Arduino circuit. It describes connecting a push button with a pull-down resistor to an input pin and an LED to an output pin. The code sample shows setting the pins as input and output in setup(), then reading the button state with digitalRead() and toggling the LED accordingly in loop(). It also explains how to use the Arduino's internal pull-up resistors instead of an external one.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Tutorials - Digital Input: Circuitry

The document discusses using a push button as a digital input for an Arduino circuit. It describes connecting a push button with a pull-down resistor to an input pin and an LED to an output pin. The code sample shows setting the pins as input and output in setup(), then reading the button state with digitalRead() and toggling the LED accordingly in loop(). It also explains how to use the Arduino's internal pull-up resistors instead of an external one.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorials | Digital Input

Circuitry

We will now understand the basic working of taking input through pins with the help of
‘push buttons’. Get hold of one (these are pretty cheap) and connect it like the following
circuit.

Note the use of a pull-down resistor (150 ohms) in conjunction with the push button. To
understand how pull-down and pull-up resistors work, refer to
: http://francisshanahan.com/index.php/2009/what-are-pull-up-and-pull-down-
resistors/

Code

Now enter the code:

// Project 3.1 - Push Button Input


int led = 10; //pin for LED
int button = 2; //pin for Button

void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop() {
boolean state = digitalRead(button);
if (state == HIGH)
digitalWrite(led,HIGH);
else
digitalWrite(led,LOW);
}

Plug in the Arduino, click Verify, then Upload if no errors are found. The breadboard LED
should be turned off by default and should glow as long as the button is depressed.

Understanding The Code

 Most of the code should be easy to figure out once you have understood the
previous examples.
 Note the pins from which output has to be gathered are set to the INPUT
pinMode in the setup block.
 digitalRead(pin) This function returns a boolean value - HIGH if the pin
is high and LOW if the pin is low.

Internal Pull-Up Resistors

Conveniently, the Arduino contains pull-up resistors that are connected to the pins (the
analog pins have pull-up resistors also). These have a value of 20K ohms and need to be
activated within software to use them. To activate an internal pull-up resistor on a pin,
you first need to change the pinMode of the pin to an INPUT and then write
a HIGH to that pin using a digitalWrite command. Make sure that your button is
connected across the pin and the ground in this case:

pinMode(pin, INPUT);
digitalWrite(pin, HIGH);

Note that now pressing the button will cause the state variable to read LOW and
otherwise HIGH by default. The wire connecting the push button to +5V is no longer
needed. The circuit should be like this:

You might also like