## 數字引腳上拉電阻
> 本例向你展示**pinMode**函數中**INPUT_PULLUP**常量的使用。本例通過Arduino板和電腦間的串口連接來監視按鍵的狀態。當按鍵輸入值為HIGH時,板載LED將被打開。當按鍵輸入值為LOW時,板載LED將被關閉。
### 所需硬件
* Arduino板
* 記憶開關,按鍵或撥動開關
* 面包板
* 面包板跳線
* 杜邦線/面包板連接線
### 電路
將兩根線連到板子上:黑色線連接按鍵和GND。另一根線從2號引腳鏈接到按鍵的另外一腳。
按鍵或者開關將在被按下時連通兩側。當按鍵處于開路時(未按下),按鍵兩側并不會有連接。因為2號引腳上的上拉電阻被激活并連接到了5V,所以當按鍵被松開時我們讀到HIGH。而當按鍵按下時我們讀到LOW,因為此時線路被完全連接到GND。

### 原理圖

### 代碼
在下列程序中,setup函數用以下代碼初始化串口連接(波特率9600):
~~~
Serial.begin(9600);
~~~
接著,初始化2號引腳為輸入模式,并且使用內部上拉電阻:
~~~
pinMode(2,INPUT_PULLUP);
~~~
接下來的代碼初始化13號引腳為輸出模式 :
~~~
pinMode(13, OUTPUT);
~~~
好的,setup函數已經寫完,開始寫loop函數了。當按鍵沒有被按下時,內部上拉電阻連接到5V,這使得Arduino讀出**HIGH**(數值1)。而當按鍵被按下的時候,Arduino的引腳被”拉向”**GND**,因而Arduino讀出**LOW**(數值0)。
在主循環你所要做的第一件事情就是聲明一個變量來存儲按鍵的狀態。由于按鍵返回的值只可能是0或1,因而用**Int**類型正合適。將變量命名為**sensorValue**,并且讓它和按鍵狀態保持一致。以下代碼可以輕松做到:
~~~
int sensorValue = digitalRead(2);
~~~
一旦Arduino讀取到了輸入,就將值(應是一個十進制數)輸出到電腦。你可以在最后一行添加**Serial.println()**函數:
~~~
Serial.println(sensorValue, DEC);
~~~
現在,打開Arduino IDE上的串口監視器。按鍵按下,你可以看到一列穩定的“0”。如果按鍵被松開,則應看到“1”。
與13引腳連接的板載LED將在按鍵返回HIGH時打開,在返回LOW時關閉。
~~~
/*
數字引腳上拉電阻
本例向你展示pinMode函數中INPUT_PULLUP常量的使用。本例將會讀取2號引腳上的數字輸入,然后輸出到串口監視器。
電路連接:
* 記憶開關連接2號到GND
* 13引腳連LED
不像pinMode(INPUT), 這里不需要下拉電阻。20kΩ電阻將電壓上拉到5V。這種配置使得按鍵被松開時返回HIGH,按鍵被按下時返回LOW。
代碼是公開的
*/
void setup() {
//打開串口連接
Serial.begin(9600);
//配置2號引腳為輸入引腳,并且開啟上拉電阻
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop() {
//讀取按鍵的值并存入變量
int sensorVal = digitalRead(2);
//輸出按鍵的值
Serial.println(sensorVal);
// 記住,使用了上拉電阻就意味著按鍵的使用邏輯反過來了。
// 在松開時返回HIGH,按下時返回LOW。
//在按鍵按下點亮13號引腳上的LED,在松開時熄滅:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
}
~~~
### 相關資料
[setup()](http://www.arduino.cc/en/Reference/Setup)?
[loop()](http://www.arduino.cc/en/Reference/Loop)?
[pinMode()](http://www.arduino.cc/en/Reference/PinMode)?
[digitalRead()](http://www.arduino.cc/en/Reference/DigitalRead)?
[delay()](http://www.arduino.cc/en/Reference/Delay)?
[int](http://www.arduino.cc/en/Reference/Int)?
[serial](http://www.arduino.cc/en/Reference/Serial)?
[DigitalPins](http://www.arduino.cc/en/Tutorial/DigitalPins)?
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP