Debian Codegraph @

examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino

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