## **模拟信号与串口**
> 本例向你展示如何从模拟信号输入引脚读模拟信号值、将读出的值映射到0-255的范围,并根据结果进行**PWM**来让LED渐暗/渐亮以及打印结果到**Arduino IDE**上的**串口监视器**。
### **所需硬件**
* Arduino板Genuino板
* 电位器
* 红色LED
* 220Ω电阻
* 杜邦线
### **电路**
![图片来自官网](http://img.blog.csdn.net/20160505171637519)
电位器一个引脚连接到5V,中间引脚连接到A0口,最后一个引脚连接到GND。
接下来,在9号引脚连一个220Ω限流电阻并串接一个LED。(LED长脚(阳极)应连接到电阻,短脚(阴极)连接到GND)。
### **原理图**
![图片来自官网](http://img.blog.csdn.net/20160505171652660)
### **代码**
在下列代码中。**setup**函数只做两件事情:第一是定义了两个引脚(A0给电位器,9号引脚给LED),以及两个变量(**sensorValue**和**outputValue**)。第二是打开串口通信。
接下来,在主循环中,**sensorValue**被用来存储来自电位器的原始模拟信号值。Arduino有一个返回值为0到1023的**analogRead**函数,以及一个参数为0-255的**analogWrite**函数。为了让来自电位器的模拟信号值能正确操作LED,我们需要将值的范围映射成0-255。
为了完成转换,用map()函数即可:
~~~
outputValue = map(sensorValue, 0, 1023, 0, 255);
~~~
**outputValue**时刻与电位器映射后的值保持相等。**map()**函数有五个参数,依次是:要映射的原始值,原始值最小值,原始值最大值,映射后的最小值,映射后的最大值。这样,传感器返回的数据就能够从其原来的0-1023被映射为0-255了。
接下来,新映射的传感器数据就能会被输出到**analogOutPin**来使LED在电位器转动时渐亮/渐暗。最终,原始值和被映射到0-255的传感器值将被输出到Arduino IDE中。
~~~
/*
模拟信号与串口
读取模拟输入引脚的值,并且将它映射到0-255。然后使用映射后的值来为输出引脚设定脉宽(通过PWM)。
并将结果输出到串口监视器。
电路搭建:
* 电位器中间脚连接到A0。两边的引脚分别连接到+5V和GND.
* LED连接数字引脚9到GND
示例代码公开
*/
// 常量的声明定义,用作引脚定义
const int analogInPin = A0; // 电位器连接到的模拟输入引脚
const int analogOutPin = 9; // LED连接的输出引脚
//变量的声明定义
int sensorValue = 0; // 从电位器读到的值
int outputValue = 0; // PWM值
void setup() {
// 初始化串口(波特率9600):
Serial.begin(9600);
}
void loop() {
// 读取模拟输入值:
sensorValue = analogRead(analogInPin);
// 映射模拟输入值到0-255:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// 改变模拟输出值:
analogWrite(analogOutPin, outputValue);
// 结果输出到串口监视器:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// 等两毫秒以让数模转换器完成工作:
delay(2);
}
~~~
### **相关资料**:
[map()](http://www.arduino.cc/en/Reference/Map)
[analogRead()](http://www.arduino.cc/en/Reference/AnalogRead)
[analogWrite()](http://www.arduino.cc/en/Reference/AnalogWrite)
[serial()](http://www.arduino.cc/en/Reference/Serial)
- 说明
- 系统示例文件目录结构及说明
- 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