Aduino温度传感器和LED

时间:2019-04-12 13:34:50

标签: arduino

我没有arduino的丰富经验,需要您的帮助。我需要打开/内置LED,然后温度升高27 C。我写了从头开始,但是不起作用,它读取了温度和湿度,但是却不起作用,请您帮助,哪里出了错?

#include <dht.h>

dht DHT;

#define DHT11_PIN 2

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
float chk = DHT.read11(DHT11_PIN);
if ( chk > 27.00 )
   digitalWrite( LED_BUILTIN, HIGH);
if ( chk < 27.00 )
   digitalWrite( LED_BUILTIN, LOW);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(2000);
}

谢谢

2 个答案:

答案 0 :(得分:0)

如果您有chk,它是返回值而不是温度。温度在DHT.temperature变量中。尝试以下代码:

#include <dht.h>

dht DHT;

#define DHT11_PIN 2

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  if ( DHT.temperature > 27.00 )
    digitalWrite( LED_BUILTIN, HIGH);
  if ( DHT.temperature <= 27.00 )
    digitalWrite( LED_BUILTIN, LOW);

  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);
}

如果要处理=温度,还可以将27.00°C设为1。

答案 1 :(得分:0)

好吧,我找到了解决方案,可能会对某些人有用:

#include <dht.h>

dht DHT;

#define DHT11_PIN 2

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  if ( DHT.temperature > 27.00 )
     digitalWrite( LED_BUILTIN, HIGH);
  if ( DHT.temperature < 27.00 )
     digitalWrite( LED_BUILTIN, LOW);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);

}