其他分享
首页 > 其他分享> > 2.3 Arduino各类模块-4(Arduino uno伺服器拓展模块、DHT11温湿度传感器)

2.3 Arduino各类模块-4(Arduino uno伺服器拓展模块、DHT11温湿度传感器)

作者:互联网

7、Arduino uno伺服器拓展模块

 

8、DHT11温湿度传感器

该模块使用DHT11库实现

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2  //设置data的IO口
void setup(){
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
}
void loop(){
  Serial.println("\n");
  int chk = DHT11.read(DHT11PIN);
  Serial.print("Read sensor: ");
/*  switch (chk)
  {
    case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
  }*/
  Serial.print("湿度 (%): ");
  Serial.println((float)DHT11.humidity, 2);
  Serial.print("温度 (oC): ");//摄氏度
  Serial.println((float)DHT11.temperature, 2);
  Serial.print("温度 (oF): ");//华氏度
  Serial.println(Fahrenheit(DHT11.temperature), 2);
  Serial.print("温度 (K): ");//开尔文
  Serial.println(Kelvin(DHT11.temperature), 2);
  Serial.print("露点 (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
  Serial.print("露点快速计算 (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  delay(10000);
}
double Fahrenheit(double celsius){//华氏度
        return 1.8 * celsius + 32;
}  
double Kelvin(double celsius){//开尔文
        return celsius + 273.15;
}   
double dewPoint(double celsius, double humidity){//露点计算
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}
double dewPointFast(double celsius, double humidity){//露点快速计算
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

 9、MQ2烟雾传感器

int MQ2=A1;//指定模拟端口A1
int val=0;//声明临时变量

void setup()
{
  pinMode(MQ2,INPUT);
  Serial.begin(9600);//设置串口波特率为9600
}
void loop()
{
  val=analogRead(MQ2);//读取A0口的电压值并赋值到val
  Serial.println(val);//串口发送val值
  delay(1000);
}

10、土壤湿度传感器

#define PIN_AO 2
#define PIN_DO 4
  
void setup() {  
  pinMode(PIN_AO, INPUT);
  pinMode(PIN_DO, INPUT);  
  Serial.begin(9600);  
}  
  
void loop() {
  Serial.print("AO=");  
  Serial.print(analogRead(PIN_AO));
  Serial.print(", DO=");  
  Serial.println(digitalRead(PIN_DO));
  delay(500);  
  int newdata = map(-analogRead(PIN_AO),-1023,-245,0,100);
} 

标签:Arduino,double,print,celsius,模块,温湿度,println,Serial,DHT11
来源: https://blog.csdn.net/m0_59889595/article/details/120525846