## **模擬信號輸入**
~~~
本例中,我們根據電位器(或用光敏電阻替代)的阻值變化來改變LED閃爍頻率。電阻的模擬信號值將根據電壓變化。
~~~
### **所需硬件**
* Arduino板或Genuino板
* 電位器 或 10kΩ光敏電阻+10kΩ電阻
* 紅色220Ω LED燈(或者板載LED)
### **電路連接**
#### **電位器接法**

連接3根線到Arduino/Genuino板。電位器一個外側的引腳連接到GND,另一個外側的引腳連接到+5V。A0口連接到電位器中間引腳。
對于本例來說,用13號板載LED也可。如要用外加LED,將LED長腳(**陽極**)用220Ω電阻連接到13號引腳。將LED短腳(**陰極**)連接到GND引腳。
> 譯者注:原文是“連接到13號引腳旁邊的引腳”。
#### **光敏電阻接法**

用光敏電阻的電路使用**分壓器**來保證模擬信號在轉換電壓時有高阻抗。因為模擬輸入引腳幾乎不會消耗任何電流,因此根據歐姆定律,不管電阻阻值為多少,連接到5V的那一端總是5V。為了根據光敏電阻阻值變化改變電壓,電阻分壓器必不可少。電路中用一個可變電阻和一個電位器來組成分壓器,分壓器”劃片”在兩電阻的中間。?
測量的電壓應根據以下公式可算得:
Vout=Vin*(R2/(R1+R2))
**Vin**為5V,**R2**為10kΩ,**R1**為光敏電阻。光敏電阻在黑暗時為1mΩ,白天(10?**流明**?)時10kΩ,日光下/明亮燈(超過100?**流明**?)管下小于1kΩ)。
### **原理圖**
#### **電位器接法**

#### **光敏電阻接法**

### **例程代碼**
在代碼開頭,**sensorPin**變量被設置為A0,A0口連接著電位器。LED引腳被設置為13。你也可創建另一個**sensorValue**變量來存儲從傳感器讀出來的值。
**analogRead()**函數將0-5v的電壓值映射成為0-1023。這個操作通過板子上的**數模轉換電路**(ADC)完成。
通過轉動電位器的軸,你能改變電位器劃片兩側的電阻(整個電位器是由其中間引腳分開的滑動變阻器)。這樣一來就能改變中間引腳上的電壓,從而讓你在analogRead()讀出不同的值。當電位器轉軸被朝一個方向轉到底時,中間引腳和連接到GND引腳之間的電阻為0。**analogRead()?**此時應返回0。當電位器轉軸被朝另一個方向轉到底時,中間引腳和連接到5v引腳之間的電阻為0。中間引腳連接到+5v,**analogRead()?**返回1023。在中間時,**analogRead()?**根據A0口上的電壓按照比例返回0-1023的值。
返回值被存入**sensorValue**,**sensorValue**用來設置delay()的毫秒數,即為閃爍的間隔時間。**sensorValue**值越小,閃爍的間隔時間越小。**sensorValue**的值是在閃爍的開頭讀取的,因此LED打開和關閉之間的時間也總是相等的。
~~~
/*
Analog Input
本例向你展示在A0口讀取輸出模擬信號的傳感器的值,并且根據這個值讓13號引腳的LED閃爍。LED的閃爍周期根據analogRead()返回值確定。
電路搭建:
* 電位器中間的引腳連接到A0,兩側分別連接+5V和GND
* LED陽極(長腳) 連接到13號引腳
* LED負極(短腳)連接到GND
* 小貼士:因為大多數板有板載LED連接到13號引腳,因此外加的LED是可選的。
代碼是公開的。
*/
int sensorPin = A0; // 設置電位器的引腳
int ledPin = 13; // 設置LED引腳
int sensorValue = 0; // 傳感器值的存儲變量
void setup() {
// 聲明ledPin為輸出模式:
pinMode(ledPin, OUTPUT);
}
void loop() {
// 從傳感器讀值:
sensorValue = analogRead(sensorPin);
// 打開LED
digitalWrite(ledPin, HIGH);
// 暫停程序sensorValue毫秒:
delay(sensorValue);
// 將LED關閉:
digitalWrite(ledPin, LOW);
// 暫停程序sensorValue毫秒:
delay(sensorValue);
}
~~~
### **相關資料**
[pinMode()](https://www.arduino.cc/en/Reference/PinMode)?
[analogRead()](https://www.arduino.cc/en/Reference/AnalogRead)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[delay()](https://www.arduino.cc/en/Reference/Delay)?
- 說明
- 系統示例文件目錄結構及說明
- 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
