Digital Input Pull-Up Resistor

This example demonstrates the use of pinMode(INPUT_PULLUP).

This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital input on pin 2 and prints the results to the serial monitor.

Hardware Required

  • Arduino Board

  • pushbutton

  • hook-up wires

  • breadboard

Circuit

Connect the pushbutton between pin 2 and ground, without any resistor as reference to 5V thanks to the internal pull-up.

The circuit for this tutorial.
The circuit for this tutorial.

Schematic

The schematic for this tutorial.
The schematic for this tutorial.

Code

1/*
2
3 Input Pull-up Serial
4
5 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
6
7 input on pin 2 and prints the results to the Serial Monitor.
8
9 The circuit:
10
11 - momentary switch attached from pin 2 to ground
12
13 - built-in LED on pin 13
14
15 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
16
17 20K-ohm resistor is pulled to 5V. This configuration causes the input to read
18
19 HIGH when the switch is open, and LOW when it is closed.
20
21 created 14 Mar 2012
22
23 by Scott Fitzgerald
24
25 This example code is in the public domain.
26
27 https://www.arduino.cc/en/Tutorial/InputPullupSerial
28
29*/
30
31void setup() {
32
33 //start serial connection
34
35 Serial.begin(9600);
36
37 //configure pin 2 as an input and enable the internal pull-up resistor
38
39 pinMode(2, INPUT_PULLUP);
40
41 pinMode(13, OUTPUT);
42
43}
44
45void loop() {
46
47 //read the pushbutton value into a variable
48
49 int sensorVal = digitalRead(2);
50
51 //print out the value of the pushbutton
52
53 Serial.println(sensorVal);
54
55 // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
56
57 // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
58
59 // button's pressed, and off when it's not:
60
61 if (sensorVal == HIGH) {
62
63 digitalWrite(13, LOW);
64
65 } else {
66
67 digitalWrite(13, HIGH);
68
69 }
70}

Last revision 2015/08/11 by SM

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.