单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:1000|回复:0
打印上一主题下一主题

基于arduino的PM2.5检测代码

[复制链接]
跳转到指定楼层
楼主
ID:854569发表于 2023-3-24 12:16|只看该作者|只看大图回帖奖励


  1. #include "DHT.h"
  2. #include< Adafruit_NeoPixel.h>


  3. #define dustPin A0
  4. #define ledPower D3

  5. #define DHTPIN D7       // 定义温湿度传感器的引脚
  6. #define DHTTYPE DHT11     // DHT 11
  7. DHT dht(DHTPIN, DHTTYPE);
  8. float humi_read = 0, temp_read = 0;


  9. #include< Wire.h>
  10. #include< Adafruit_GFX.h>
  11. #include< Adafruit_SSD1306.h>
  12. #include< Adafruit_Sensor.h>
  13. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  14. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  15. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,& Wire, -1);




  16. float dustVal=0;
  17. int delayTime=280;
  18. int delayTime2=40;
  19. float offTime=9680;
  20. void setup(){
  21.   
  22.   Serial.begin(9600);

  23.   pinMode(ledPower,OUTPUT);
  24.   pinMode(dustPin, INPUT);
  25.   dht.begin();


  26.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))   // 扫描OLED的地址,默认0x3c
  27. {
  28.   Serial.println(F("SSD1306 allocation failed"));
  29.   for(;;);
  30.   }
  31.   
  32.   display.clearDisplay();   //  清屏
  33.   display.setTextColor(WHITE);  //开像素点发光
  34. }

  35. void loop(){
  36. // ledPower is any digital pin on the arduino connected to Pin 3 on the sensor
  37.   digitalWrite(ledPower,LOW);
  38.   delayMicroseconds(delayTime);
  39.   dustVal=analogRead(dustPin);
  40.   delayMicroseconds(delayTime2);
  41.   digitalWrite(ledPower,HIGH);
  42.   delayMicroseconds(offTime);
  43.   delay(1000);
  44.   // if (dustVal>36.455)
  45.    Serial.println(dustVal);
  46.   
  47.    float h = dht.readHumidity();
  48.    float t = dht.readTemperature();
  49.   
  50.    if (isnan(h) || isnan(t))
  51.    {
  52.        // BLINKER_LOG("Failed to read from DHT sensor!");
  53.    }
  54.    else
  55.    {
  56.    Serial.println(t,2);
  57.    display.clearDisplay();   // 清屏
  58.    display.setTextSize(1);  // 字体
  59.    display.setCursor(0,0);  //显示位置在第一行第一个
  60.    display.print("Temp: ");
  61.    display.setTextSize(2);
  62.    display.print(t);
  63.    display.print(" ");
  64.    display.setTextSize(1);
  65.    display.cp437(true);  //使用cp437符号集
  66.    display.write(167);     //对应的温度符号
  67.    display.setTextSize(2);
  68.    display.print("C");   // display humidity
  69.   
  70.    display.setTextSize(1);
  71.    display.setCursor(0, 25);
  72.    display.print("Humi: ");
  73.    display.setTextSize(2);
  74.    display.print(h);
  75.    display.print(" %");
  76.    display.display();

  77.    display.setTextSize(1);
  78.    display.setCursor(0, 50);
  79.    display.print("PM2.5: ");
  80.    display.setTextSize(2);
  81.    display.print(dustVal);
  82.    display.print(" %");
  83.    display.display();

  84.        Serial.println(h);
  85.        Serial.println(t);
  86.        humi_read = h;
  87.        temp_read = t;
  88.    }


  89. }
复制代码


手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网