攀藤 5003粉尘激光传感器arduino使用

    xiaoxiao2025-02-06  40

    同事给了个攀藤 的 粉尘激光传感器,看着还不错,所以就写了个数据的处理驱动,用arduino nano,定时器处理。

    先在网上找到了数据手册 发现最大工作电流为120mA,而arduino管脚电流极限是40mA,所以不能用nano来驱动,用一根usb先外部供电,共地就可以。 接下来说接线 就插三根线就可以接收数据了。我使用了nano的软串口。 这是数据帧的图,一帧32字节数据,两字节头,一字节长度,最后一字节校验,中间的都是数据位。 然后手册里说 set应该是内部上拉了,不用管,如果要低功耗,可以自己设置, 我用定时器两秒进行一次采样,在采样时,采够32字节数据后,就将定时器关闭,处理完后再打开。 处理中会有对数据的校验,不符合校验的帧直接丢了,符合的打印。 这个是正确的结果打印图。 需要用到 #include <MsTimer2.h> #include <SoftwareSerial.h> 这两个库可以自己在ide中下载 话不多说 代码如下:

    #include <MsTimer2.h> #include <SoftwareSerial.h> SoftwareSerial serial(2, 3); int count = 0; int dat_len = 30, dat_cnt = 2; byte ch; uint8_t temp[33]; uint16_t jiaoyan = 0x0000; struct data { uint16_t bpm1p0; uint16_t bpm2p5; uint16_t bpm10; uint16_t pm1p0; uint16_t pm2p5; uint16_t pm10; uint16_t pm0p3u; uint16_t pm0p5u; uint16_t pm1p0u; uint16_t pm2p5u; uint16_t pm5p0u; uint16_t pm10u; }; data pm; void onTimer() { while (serial.available() > 0) { ch = byte(serial.read()); if (count == 0 && ch == 0x42) { count = 1; temp[0] = ch; } else if (count == 1 && ch == 0x4D) { count = 2; temp[1] = ch; } else if (count == 2 && dat_len > 0) { dat_len--; temp[dat_cnt++] = ch; if (dat_len == 0) count = 3; } else if (count == 3) { MsTimer2::stop(); count = 0; dat_len = 30; dat_cnt = 2; DataGive(); MsTimer2::start(); } } } void DataGive() { for (int i = 0; i < 30; i ++) { jiaoyan += temp[i] ; } if (jiaoyan != ((temp[30] << 8) | temp[31])) { jiaoyan = 0x0000; Serial.println("数据有误!"); return; } jiaoyan = 0x0000; pm.bpm1p0 = (temp[4] << 8) | temp[5]; pm.bpm2p5 = (temp[6] << 8) | temp[7]; pm.bpm10 = (temp[8] << 8) | temp[9]; pm.pm1p0 = (temp[10] << 8) | temp[11]; pm.pm2p5 = (temp[12] << 8) | temp[13]; pm.pm10 = (temp[14] << 8) | temp[15]; pm.pm0p3u = (temp[16] << 8) | temp[17]; pm.pm0p5u = (temp[18] << 8) | temp[19]; pm.pm1p0u = (temp[20] << 8) | temp[21]; pm.pm2p5u = (temp[22] << 8) | temp[23]; pm.pm5p0u = (temp[24] << 8) | temp[25]; pm.pm10u = (temp[26] << 8) | temp[27]; Serial.print("CF=1,标准颗粒物下 "); Serial.print("PM1.0为:"); Serial.print(pm.bpm1p0); Serial.print(" PM2.5为:"); Serial.print(pm.bpm2p5); Serial.print(" PM10为:"); Serial.println(pm.bpm10); Serial.print("标准大气压下 "); Serial.print("PM1.0为:"); Serial.print(pm.pm1p0); Serial.print(" PM2.5为:"); Serial.print(pm.pm2p5); Serial.print(" PM10为:"); Serial.println(pm.pm10); Serial.print("0.1 升空气中直径在 0.3um 以上颗 粒物个数为:"); Serial.println(pm.pm0p3u); Serial.print("0.1 升空气中直径在 0.5um 以上颗 粒物个数为:"); Serial.println(pm.pm0p5u); Serial.print("0.1 升空气中直径在 1.0um 以上颗 粒物个数为:"); Serial.println(pm.pm1p0u); Serial.print("0.1 升空气中直径在 2.5um 以上颗 粒物个数为:"); Serial.println(pm.pm2p5u); Serial.print("0.1 升空气中直径在 5.0um 以上颗 粒物个数为:"); Serial.println(pm.pm5p0u); Serial.print("0.1 升空气中直径在 10 um 以上颗 粒物个数为:"); Serial.println(pm.pm10u); } void setup() { Serial.begin(9600); serial.begin(9600); MsTimer2::set(2000, onTimer); MsTimer2::start(); } void loop() { }

    代码有什么问题欢迎随时留言讨论 `

    最新回复(0)