NodeMCU 似乎没有将数字引脚设置为低电平

发布于 2025-01-19 10:12:29 字数 2022 浏览 1 评论 0原文

我正在写一个简单的项目,使用nodemcu作为我的董事会。我有2个模拟设备:水分传感器和亮度传感器。由于Nodemcu只有一个模拟引脚,因此我尝试转动它们。为此,我将它们连接到Nodemcu的数字引脚。数字引脚输出3.3V 20-40mA 状态(使用万用表检查)。那必须足以为这些设备供电。在状态下,根据Arduino IDE文档的说法,电压必须为0V(并且也已检查),并且传感器必须不接收到任何当前电流,因此不得将任何内容输出到A0。但是,最后我会得到相关的结果:如果我在光孔仪上闪光灯,湿度数据也会受到影响。反之亦然。我该如何避免它? 这是代码,我要做的,我描述的是:

const int analogInputPin = A0; // Analog 0 (A0) on the board

// Several devices will use the same analog pin to send data
const int lightSensorResultPin = analogInputPin;
const int moistureSensorResultPin = analogInputPin;

const int lightSensorPowerPin = 16;
int lightSensorResult = 0;

const int moistureSensorPowerPin = 5;
int moistureSensorResult = 0;

void setup() {
  Serial.begin(9600);
  pinMode(lightSensorResultPin, INPUT);
  pinMode(lightSensorPowerPin, OUTPUT);
  digitalWrite(lightSensorPowerPin, LOW);

  pinMode(moistureSensorResultPin, INPUT);
  pinMode(moistureSensorPowerPin, OUTPUT);
  digitalWrite(moistureSensorPowerPin, LOW);
}

void loop() {
  /*==LIGHT SENSOR DATA READ BLOCK==*/
  digitalWrite(lightSensorPowerPin, HIGH);
  delay(1000);
  lightSensorResult = analogRead(lightSensorResultPin);
  digitalWrite(lightSensorPowerPin, LOW);
  delay(1000);
  /*==END OF LIGHT SENSOR DATA READ BLOCK==*/

  /*==MOISTURE SENSOR DATA READ BLOCK==*/
  digitalWrite(moistureSensorPowerPin, HIGH);
  delay(1000);
  moistureSensorResult = analogRead(moistureSensorResultPin);
  digitalWrite(moistureSensorPowerPin, LOW);
  delay(1000);
  /*==END OF MOISTURE SENSOR DATA READ BLOCK==*/

  Serial.print("Value on the light sensor: ");
  Serial.println(lightSensorResult); // Light value. Low for bright
  Serial.print("Value on the moisture sensor: ");
  Serial.println(moistureSensorResult); // Moisture value. Low for wet
}

upd:这是示意图

“示例”

I'm writing a simple project, using NodeMCU as my board. I have 2 analog devices: moisture sensor and brightness sensor. Since NodeMCU has only one analog pin, I try to power them in turns. To do so, I connect them to digital pins of NodeMCU. Digital pins output 3.3V 20-40mA in HIGH state (checked that with multimeter). That must be enough to power those devices. In the LOW state, according to Arduino IDE docs, voltage must be 0V (and that also was checked) and the sensor must not receive any current and, thus, must not output anything to A0. However, at the end I get correlated results: if I flash light at the photoresistor, humidity data is also affected. And vice-versa. How can I avoid it?
Here's the code, I'm using to do, what I described:

const int analogInputPin = A0; // Analog 0 (A0) on the board

// Several devices will use the same analog pin to send data
const int lightSensorResultPin = analogInputPin;
const int moistureSensorResultPin = analogInputPin;

const int lightSensorPowerPin = 16;
int lightSensorResult = 0;

const int moistureSensorPowerPin = 5;
int moistureSensorResult = 0;

void setup() {
  Serial.begin(9600);
  pinMode(lightSensorResultPin, INPUT);
  pinMode(lightSensorPowerPin, OUTPUT);
  digitalWrite(lightSensorPowerPin, LOW);

  pinMode(moistureSensorResultPin, INPUT);
  pinMode(moistureSensorPowerPin, OUTPUT);
  digitalWrite(moistureSensorPowerPin, LOW);
}

void loop() {
  /*==LIGHT SENSOR DATA READ BLOCK==*/
  digitalWrite(lightSensorPowerPin, HIGH);
  delay(1000);
  lightSensorResult = analogRead(lightSensorResultPin);
  digitalWrite(lightSensorPowerPin, LOW);
  delay(1000);
  /*==END OF LIGHT SENSOR DATA READ BLOCK==*/

  /*==MOISTURE SENSOR DATA READ BLOCK==*/
  digitalWrite(moistureSensorPowerPin, HIGH);
  delay(1000);
  moistureSensorResult = analogRead(moistureSensorResultPin);
  digitalWrite(moistureSensorPowerPin, LOW);
  delay(1000);
  /*==END OF MOISTURE SENSOR DATA READ BLOCK==*/

  Serial.print("Value on the light sensor: ");
  Serial.println(lightSensorResult); // Light value. Low for bright
  Serial.print("Value on the moisture sensor: ");
  Serial.println(moistureSensorResult); // Moisture value. Low for wet
}

UPD: Here's the schematic

Schematic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

廻憶裏菂餘溫 2025-01-26 10:12:29

我不会使用数字输出打开和关闭不同的传感器,而是将模拟输出结合起来,而是使用一个模拟开关,例如 4066 选择要测量的传感器。

Instead of using digital outputs to switch on and off the different sensors, but combining their analog outputs, I would use an analog switch like 4066 to select which sensor you want to measure.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文