为什么Raspberry Pi Pico ADC的电压不等于0,而引脚上没有连接任何东西



我试图从传感器中读取一些电压,但在读取之前,我检查了当引脚上没有连接任何东西时,读数会是什么样子。

以下是我基于示例的代码:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
int main() {
stdio_init_all();
const float conversion_factor = 3.3f / (1 << 12);
adc_init();    
gpio_set_dir_all_bits(0);
for (int i = 2; i < 30; ++i) {
gpio_set_function(i, GPIO_FUNC_SIO);
if (i >= 26) {
gpio_disable_pulls(i);
gpio_set_input_enabled(i, false);
}
}    
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
adc_set_temp_sensor_enabled(true);
bool enabled = 0;
while (1) {
float result[3];
for (int i=0; i<3; i++)
{
adc_select_input(i);
sleep_ms(10);
result[i] = adc_read() * conversion_factor;
}
adc_select_input(4);
sleep_ms(10);
float temp_adc = (float)adc_read() * conversion_factor;
float tempC = 27.0f - (temp_adc - 0.706f) / 0.001721f;
printf("Voltage 0-2: %f,%f,%f V, temp: %frn", result[0], result[1], result[2], tempC);
enabled = (enabled + 1) % 2;
gpio_put(PICO_DEFAULT_LED_PIN, enabled);
sleep_ms(2000);
}
}

我得到的输出中,只有机载温度似乎是合理的,并且没有变化:

Voltage 0-2: 1.145654,0.374634,1.025610 V, temp: 23.861492
Voltage 0-2: 0.407666,1.086035,0.441504 V, temp: 23.393347
Voltage 0-2: 1.136792,0.359326,1.046558 V, temp: 23.393347
Voltage 0-2: 0.558325,0.605859,0.579272 V, temp: 23.861492
Voltage 0-2: 0.645337,0.504346,0.696094 V, temp: 23.861492

此外,我有两个Raspberry Pi Pico,两个都得到了类似的结果。

为什么ADC 0到2的值不等于/接近0,并且在Pico上完全没有连接任何东西的情况下变化如此之快?

当引脚没有连接到任何东西时就会发生这种情况,这被称为噪声。这里的优雅解释

引脚永远不会不连接。它们有一个上拉/下拉电阻器,可以打开或关闭,并连接到可以在之间切换的读写电路。

这些不是物理交换机。当你关闭上拉/下拉电阻器时,真正的意思是你把它变成了一个非常高的电阻器。开关上总是会漏一点电流。

您也有电流通过附近的引脚,可能会在浮动引脚中感应电流。

您也可能只是在引脚上留下了连接时或作为输出引脚时的剩余电荷。如果浮动引脚输出为HIGH,并且您将其切换为读取,则您读取的下一个值可能为HIGH。读数会消耗一点剩余电荷,所以如果你再等一段时间,下一个读数可能会低。

测量浮动引脚基本上没有意义,但可能是随机位的来源。对于其他任何东西,这就是上拉/下拉电阻器的作用。[如果Pico没有这些,你必须从外部连接一个]。

最新更新