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.
Schematic
Code
1/*2
3 Input Pull-up Serial4
5 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital6
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 ground12
13 - built-in LED on pin 1314
15 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal16
17 20K-ohm resistor is pulled to 5V. This configuration causes the input to read18
19 HIGH when the switch is open, and LOW when it is closed.20
21 created 14 Mar 201222
23 by Scott Fitzgerald24
25 This example code is in the public domain.26
27 https://www.arduino.cc/en/Tutorial/InputPullupSerial28
29*/30
31void setup() {32
33 //start serial connection34
35 Serial.begin(9600);36
37 //configure pin 2 as an input and enable the internal pull-up resistor38
39 pinMode(2, INPUT_PULLUP);40
41 pinMode(13, OUTPUT);42
43}44
45void loop() {46
47 //read the pushbutton value into a variable48
49 int sensorVal = digitalRead(2);50
51 //print out the value of the pushbutton52
53 Serial.println(sensorVal);54
55 // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes56
57 // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the58
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
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.