使用以下代码,我得到一个如下所示的日志。那是为什么?
int analogPin = 5;
int val = 0;
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
val = analogRead(analogPin);
Serial.print("Analog reading = ");
Serial.println(val);
}
结果:
Analog reading = 998
Analog reading = 981
Analog reading = 511
Analog reading = 159
Analog reading = 24
Analog reading = 108
Analog reading = 439
Analog reading = 946
Analog reading = 1023
Analog reading = 420
Analog reading = 116
Analog reading = 25
Analog reading = 151
Analog reading = 542
Analog reading = 997
Analog reading = 982
Analog reading = 513
Analog reading = 161
Analog reading = 25
Analog reading = 107
Analog reading = 437
Analog reading = 945
Analog reading = 1023
Analog reading = 421
Analog reading = 117
Analog reading = 25
Analog reading = 150
Analog reading = 541
Analog reading = 997
Analog reading = 983
Analog reading = 515
Analog reading = 162
Analog reading = 25
Analog reading = 107
Analog reading = 437
Analog reading = 945
Analog reading = 1023
Analog reading = 422
Analog reading = 117
Analog reading = 25
Analog reading = 149
Analog reading = 540
Analog reading = 997
Analog reading = 983
Analog reading = 516
Analog reading = 162
Analog reading = 25
Analog reading = 107
Analog reading = 436
Analog reading = 945
Analog reading = 1023
Analog reading = 422
Analog reading = 117
Analog reading = 25
Analog reading = 150
Analog reading = 540
Analog reading = 998
Analog reading = 982
Analog reading = 516
Analog reading = 162
Analog reading = 25
Analog reading = 108
Analog reading = 437
结果只是噪音吗?
一旦我插入电缆,我就会看到以下模式:
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 72
Analog reading = 447
Analog reading = 1023
Analog reading = 1023
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 118
Analog reading = 849
Analog reading = 1023
Analog reading = 835
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 40
Analog reading = 401
Analog reading = 1023
Analog reading = 1023
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 0
Analog reading = 89
Analog reading = 475
Analog reading = 1023
Analog reading = 1023
这是我的心跳吗?不知道什么会对空针产生这样的影响。
答案 0 :(得分:4)
是的,未连接的模拟引脚产生随机噪声是很正常的。来自documentation(强调我的):
如果由
random()
生成的一系列值很重要,在后续执行草图时,使用randomSeed()
使用相当随机的输入初始化随机数生成器,例如未连接引脚上的analogRead()。
关于插入电缆时看到的值,它们是否有意义取决于您输入引脚的信号。如果存在一些差异,可能需要用示波器查看信号。
另请参阅How come analogRead reads oscillating values from 0 to 1023 when no actual input is present?
答案 1 :(得分:1)
它是一个浮动输入。如果一个引脚没有连接到任何东西或直接连接到5V电源。 arduino Button tutorial解释了这一点。 有关详细信息,请查看维基百科Pull-up Resistor page
答案 2 :(得分:0)
我有PPG传感器板和Arduino Mega。我在我的工具包上试过你的代码。当PPG传感器板检测到我的心率时,它开始改变全零。让我解释一下串行监视器输出。首先到处都是0.检测到我的心率。它现在不是0。它充满了数字。这些不仅仅是噪音。这些是我现在的噪音+真实心率数字。我想,Arduino会以检测频率显示图形峰值。