## **模拟信号输入**
~~~
本例中,我们根据电位器(或用光敏电阻替代)的阻值变化来改变LED闪烁频率。电阻的模拟信号值将根据电压变化。
~~~
### **所需硬件**
* Arduino板或Genuino板
* 电位器 或 10kΩ光敏电阻+10kΩ电阻
* 红色220Ω LED灯(或者板载LED)
### **电路连接**
#### **电位器接法**
![图片来自官网](http://img.blog.csdn.net/20160509131529027)
连接3根线到Arduino/Genuino板。电位器一个外侧的引脚连接到GND,另一个外侧的引脚连接到+5V。A0口连接到电位器中间引脚。
对于本例来说,用13号板载LED也可。如要用外加LED,将LED长脚(**阳极**)用220Ω电阻连接到13号引脚。将LED短脚(**阴极**)连接到GND引脚。
> 译者注:原文是“连接到13号引脚旁边的引脚”。
#### **光敏电阻接法**
![图片来自官网](http://img.blog.csdn.net/20160509131554745)
用光敏电阻的电路使用**分压器**来保证模拟信号在转换电压时有高阻抗。因为模拟输入引脚几乎不会消耗任何电流,因此根据欧姆定律,不管电阻阻值为多少,连接到5V的那一端总是5V。为了根据光敏电阻阻值变化改变电压,电阻分压器必不可少。电路中用一个可变电阻和一个电位器来组成分压器,分压器”划片”在两电阻的中间。
测量的电压应根据以下公式可算得:
Vout=Vin*(R2/(R1+R2))
**Vin**为5V,**R2**为10kΩ,**R1**为光敏电阻。光敏电阻在黑暗时为1mΩ,白天(10 **流明** )时10kΩ,日光下/明亮灯(超过100 **流明** )管下小于1kΩ)。
### **原理图**
#### **电位器接法**
![图片来自官网](http://img.blog.csdn.net/20160509132019310)
#### **光敏电阻接法**
![图片来自官网](http://img.blog.csdn.net/20160509132037136)
### **例程代码**
在代码开头,**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