1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-15 00:23:56 +01:00
Arduino/build/shared/examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino

52 lines
1.3 KiB
Arduino
Raw Normal View History

/*
Input Pullup Serial
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.
The circuit:
* Momentary switch attached from pin 2 to ground
2012-03-15 04:11:35 +01:00
* Built-in LED on pin 13
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to
read HIGH when the switch is open, and LOW when it is closed.
created 14 March 2012
by Scott Fitzgerald
2012-03-15 02:11:00 +01:00
http://www.arduino.cc/en/Tutorial/InputPullupSerial
This example code is in the public domain
*/
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
2012-03-15 04:11:35 +01:00
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
2012-03-15 04:11:35 +01:00
// Keep in mind the pullup means the pushbutton's
2012-03-16 13:45:29 +01:00
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin 13 when the
2012-03-15 04:11:35 +01:00
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
2012-03-15 04:11:35 +01:00
digitalWrite(13, HIGH);
}
}
2012-03-15 04:11:35 +01:00