找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7266|回复: 1
打印 上一主题 下一主题
收起左侧

arduino驱动SDS011激光PM2.5颗粒物传感器

[复制链接]
跳转到指定楼层
楼主

SDS011是一款使用激光光源的PM2.5颗粒物监测传感器,串口输出,自带风扇,能检测直径最小0.3微米的颗粒物,性能稳定。


由于采用串口输出,使得数据获取更加方便,同时也提供PWM输出方式,也能满足更高的精度要求。程序使用软串口采集数据,同时输出至硬件串口,在实时显示数据的同时也能将数据发送至上位机或通过添加网络模块发送至物联网络。传感器监测结果较为可靠,比较接近环境监测站发布的数据。

制作资料下载:

SDS011激光PM2.5传感器简介.pdf (184.7 KB, 下载次数: 20)


NewSoftSerial12.zip (9.73 KB, 下载次数: 18)


graphicstest.rar (1.79 KB, 下载次数: 13)



部分源码预览:


  1. #define LCD_CS A3   
  2. #define LCD_CD A2   
  3. #define LCD_WR A1   
  4. #define LCD_RD A0   
  5. // you can also just connect RESET to the arduino RESET pin
  6. #define LCD_RESET A4
  7. //Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
  8. // Color definitions
  9. #define        BLACK           0x0000
  10. #define        BLUE            0x001F
  11. #define        RED             0xF800
  12. #define        GREEN           0x07E0
  13. #define CYAN            0x07FF
  14. #define MAGENTA         0xF81F
  15. #define YELLOW          0xFFE0
  16. #define WHITE           0xFFFF

  17. #include "TFTLCD.h"
  18. #include


  19. TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  20. NewSoftSerial nss(11, 12);

  21. uint8_t c;
  22. uint8_t data[10];
  23. uint8_t pIdx = 0;
  24. bool LogData=false;
  25. int pm2_5 = 0;
  26. int pm10 = 0;
  27. int pm2_5_Old = 0;
  28. int pm10_Old = 0;
  29. uint16_t color2_5 = GREEN;
  30. uint16_t color10 = GREEN;

  31. void setup(void) {

  32.   tft.reset();
  33.   tft.initDisplay();
  34.   tft.setRotation(3);
  35.   tft.fillScreen(BLACK);

  36.   tft.setCursor(0, 15);
  37.   tft.setTextColor(BLUE);
  38.   tft.setTextSize(4);
  39.   tft.println("PM2.5:");
  40.   tft.setCursor(0, 130);
  41.   tft.println("PM10:");


  42.   Serial.begin(9600);
  43.   nss.begin(9600);

  44.   //  testtext(RED);
  45.   //  delay(2000);
  46.   //  testlines(CYAN);
  47.   //  delay(500);
  48.   //  testfastlines(RED, BLUE);
  49.   //  delay(500);
  50.   //  testdrawrects(GREEN);
  51.   //  delay(500);
  52.   //  testfillrects(YELLOW, MAGENTA);
  53.   //  delay(500);
  54.   //  tft.fillScreen(BLACK);
  55.   //  testfillcircles(10, MAGENTA);
  56.   //  testdrawcircles(10, WHITE);
  57.   //  delay(500);
  58.   //  testtriangles();
  59.   //  delay(500);
  60.   //  testfilltriangles();
  61.   //  delay(500);
  62.   //  testRoundRect();
  63.   //  delay(500);
  64.   //  testFillRoundRect();
  65. }

  66. void loop(void) {

  67.   while(nss.available())
  68.   {
  69.     c = nss.read();

  70.     if(c==170)
  71.     {
  72.       LogData=true;
  73.     }

  74.     if(LogData)
  75.     {
  76.       data[pIdx]=c;
  77.       pIdx=pIdx+1;
  78.     }

  79.     if(c==171)
  80.     {
  81.       LogData=false;
  82.       pIdx=0;
  83.       uint8_t sumAdd = data[2]+data[3]+data[4]+data[5]+data[6]+data[7];
  84.       if(sumAdd==data[8])
  85.       {
  86.         pm2_5 = (data[3] * 256 + data[2]) / 10;
  87.         pm10 = (data[5] * 256 + data[4]) / 10;

  88.         if(pm2_5>0 && pm2_5<=75)
  89.         {
  90.           color2_5 = GREEN;
  91.         }
  92.         else if(pm2_5>75 && pm2_5<=200)
  93.         {
  94.           color2_5 = YELLOW;
  95.         }
  96.         else if(pm2_5>200)
  97.         {
  98.           color2_5 = RED;
  99.         }

  100.         if(pm10>0 && pm10<=75)
  101.         {
  102.           color10 = GREEN;
  103.         }
  104.         else if(pm10>75 && pm10<=200)
  105.         {
  106.           color10 = YELLOW;
  107.         }
  108.         else if(pm10>200)
  109.         {
  110.          color10 = RED;
  111.         }

  112.         testtext(pm2_5,color2_5,pm10,color10);
  113.         Serial.print(pm2_5,DEC);
  114.         Serial.print(',');
  115.         Serial.println(pm10,DEC);
  116.       }
  117.     }
  118.   }
  119. }

  120. void testtext(uint16_t val2_5,uint16_t color2_5,uint16_t val10,uint16_t color10) {

  121.   tft.setTextSize(9);

  122.   tft.setCursor(0, 55);
  123.   tft.setTextColor(BLACK);
  124.   tft.println(pm2_5_Old);
  125.   tft.setCursor(0, 55);
  126.   tft.setTextColor(color2_5);
  127.   tft.println(val2_5);
  128.   pm2_5_Old=val2_5;

  129.   tft.setCursor(0, 170);
  130.   tft.setTextColor(BLACK);
  131.   tft.println(pm10_Old);
  132.   tft.setCursor(0, 170);
  133.   tft.setTextColor(color10);
  134.   tft.println(val10);
  135.   pm10_Old=val10;

  136. }

  137. //void testFillRoundRect() {
  138. //  tft.fillScreen(BLACK);
  139. //  
  140. //  for (uint16_t x=tft.width(); x > 20 ; x-=6) {
  141. //    tft.fillRoundRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, x/8,  tft.Color565(0, x, 0));
  142. // }
  143. //}

  144. //void testRoundRect() {
  145. //  tft.fillScreen(BLACK);
  146. //  
  147. //  for (uint16_t x=0; x < tft.width(); x+=6) {
  148. //    tft.drawRoundRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, x/8, tft.Color565(x, 0, 0));
  149. // }
  150. //}

  151. //void testtriangles() {
  152. //  tft.fillScreen(BLACK);
  153. //  for (uint16_t i=0; i<tft.width() 2;="" i+="5)" {
  154. //    tft.drawTriangle(tft.width()/2, tft.height()/2-i,
  155. //                     tft.width()/2-i, tft.height()/2+i,
  156. //                     tft.width()/2+i, tft.height()/2+i, tft.Color565(0, 0, i));
  157. //  }
  158. //}

  159. //void testfilltriangles() {
  160. //  tft.fillScreen(BLACK);
  161. //  
  162. //  for (uint16_t i=tft.width()/2; i>10; i-=5) {
  163. //    tft.fillTriangle(tft.width()/2, tft.height()/2-i,
  164. //                     tft.width()/2-i, tft.height()/2+i,
  165. //                     tft.width()/2+i, tft.height()/2+i,
  166. //                     tft.Color565(0, i, i));
  167. //    tft.drawTriangle(tft.width()/2, tft.height()/2-i,
  168. //                     tft.width()/2-i, tft.height()/2+i,
  169. //                     tft.width()/2+i, tft.height()/2+i, tft.Color565(i, i, 0));   
  170. //  }
  171. //}

  172. //void testfillcircles(uint8_t radius, uint16_t color) {
  173. //  for (uint16_t x=radius; x < tft.width(); x+=radius*2) {
  174. //    for (uint16_t y=radius; y < tft.height(); y+=radius*2) {
  175. //      tft.fillCircle(x, y, radius, color);
  176. //    }
  177. //  }  
  178. //}

  179. //void testdrawcircles(uint8_t radius, uint16_t color) {
  180. //  for (uint16_t x=0; x < tft.width()+radius; x+=radius*2) {
  181. //    for (uint16_t y=0; y < tft.height()+radius; y+=radius*2) {
  182. //      tft.drawCircle(x, y, radius, color);
  183. //    }
  184. //  }  
  185. //}


  186. //void testfillrects(uint16_t color1, uint16_t color2) {
  187. // tft.fillScreen(BLACK);
  188. // for (uint16_t x=tft.width()-1; x > 6; x-=6) {
  189. //   //Serial.println(x, DEC);
  190. //   tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  191. //   tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  192. // }
  193. //}

  194. //void testdrawrects(uint16_t color) {
  195. // tft.fillScreen(BLACK);
  196. // for (uint16_t x=0; x < tft.width(); x+=6) {
  197. //   tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  198. // }
  199. //}

  200. //void testfastlines(uint16_t color1, uint16_t color2) {
  201. //   tft.fillScreen(BLACK);
  202. //   for (uint16_t y=0; y < tft.height(); y+=5) {
  203. //     tft.drawHorizontalLine(0, y, tft.width(), color1);
  204. //   }
  205. //   for (uint16_t x=0; x < tft.width(); x+=5) {
  206. //     tft.drawVerticalLine(x, 0, tft.height(), color2);
  207. //   }
  208. //  
  209. //}

  210. //void testlines(uint16_t color) {
  211. //   tft.fillScreen(BLACK);
  212. //   for (uint16_t x=0; x < tft.width(); x+=6) {
  213. //     tft.drawLine(0, 0, x, tft.height()-1, color);
  214. //   }
  215. //   for (uint16_t y=0; y < tft.height(); y+=6) {
  216. //     tft.drawLine(0, 0, tft.width()-1, y, color);
  217. //   }
  218. //   
  219. //   tft.fillScreen(BLACK);
  220. //   for (uint16_t x=0; x < tft.width(); x+=6) {
  221. //     tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  222. //   }
  223. //   for (uint16_t y=0; y < tft.height(); y+=6) {
  224. //     tft.drawLine(tft.width()-1, 0, 0, y, color);
  225. //   }
  226. //   
  227. //   tft.fillScreen(BLACK);
  228. //   for (uint16_t x=0; x < tft.width(); x+=6) {
  229. //     tft.drawLine(0, tft.height()-1, x, 0, color);
  230. //   }
  231. //   for (uint16_t y=0; y < tft.height(); y+=6) {
  232. //     tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  233. //   }
  234. //
  235. //   tft.fillScreen(BLACK);
  236. //   for (uint16_t x=0; x < tft.width(); x+=6) {
  237. //     tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  238. //   }
  239. //   for (uint16_t y=0; y < tft.height(); y+=6) {
  240. //     tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  241. //   }
  242. //}

  243. //void testBars() {
  244. //  uint16_t i,j;
  245. //  for(i=0; i < tft.height(); i++)
  246. //  {
  247. //    for(j=0; j < tft.width(); j++)
  248. //    {
  249. //      if(i>279) tft.writeData(WHITE);
  250. //      else if(i>239) tft.writeData(BLUE);
  251. //      else if(i>199) tft.writeData(GREEN);
  252. //      else if(i>159) tft.writeData(CYAN);
  253. //      else if(i>119) tft.writeData(RED);
  254. //      else if(i>79) tft.writeData(MAGENTA);
  255. //      else if(i>39) tft.writeData(YELLOW);
  256. //      else tft.writeData(BLACK);
  257. //    }
  258. //  }
  259. //}
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:223039 发表于 2017-9-12 16:58 | 只看该作者
牛逼,大神,最近正在弄这个净化器,要用到激光PM2.5传感器,我下载程序到12864上显示,但数据不显示,可能是什么原因?程序是淘宝网家给的,但他是用数码管显示出来的,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

快速回复 返回顶部 返回列表