本文提供了基于Arduino开发板快速完成甲醛传感器数据读取的方式,可以用于开发甲醛检测仪。
准备
传感器:达特WZ-S甲醛传感器
开发板:Arduino Nano
开发环境:Arduino
依赖库: WZ (https://github.com/leonlucc/WZ)
引脚连接
因为Arduino Nano只有一个硬件UART串口,为了方便调试,我们用硬件串口与电脑连接,并且用软串口的方式来读取传感器数据。
Arduino Nano | WZ-S |
---|---|
5V | 5V |
GND | GND |
GPIO9 | RXD |
GPIO8 | TXD |
代码
达特WZ-S支持主动上送和被动问答式两种模式:
主动模式读数
#include "WZ.h"
#include <SoftwareSerial.h>
/////////////////////////// Arduino Nano Board
/*
* There is only one serial port on the Arduino Nano, so software serial is needed to connect to the sensor.
*/
const byte PIN_RX = 8; // define software serial RX pin
const byte PIN_TX = 9; // define software serial TX pin
SoftwareSerial WzSoftSerial(PIN_RX, PIN_TX);
WZ wz(WzSoftSerial);
WZ::DATA hcho_data;
void setup()
{
Serial.begin(9600);
WzSoftSerial.begin(9600);
//wz.activeMode();
}
void loop()
{
if (wz.read(hcho_data))
{
Serial.print("HCHO (ppd): ");
Serial.println(hcho_data.HCHO_PPB);
Serial.print("HCHO (ug/m3): ");
Serial.println(hcho_data.HCHO_UGM3); // no data here, 0 returned
Serial.println();
}
// Do other stuff...
}
被动模式读数
#include "WZ.h"
#include <SoftwareSerial.h>
/////////////////////////// Arduino Nano Board
/*
* There is only one serial port on the Arduino Nano, so software serial is needed to connect to the sensor.
*/
const byte PIN_RX = 8; // define software serial RX pin
const byte PIN_TX = 9; // define software serial TX pin
SoftwareSerial WzSoftSerial(PIN_RX, PIN_TX);
WZ wz(WzSoftSerial);
WZ::DATA hcho_data;
void setup()
{
Serial.begin(9600);
WzSoftSerial.begin(9600);
wz.passiveMode();
}
void loop()
{
wz.requestRead();
if (wz.readUntil(hcho_data))
{
Serial.print("HCHO (ppd): ");
Serial.println(hcho_data.HCHO_PPB);
Serial.print("HCHO (ug/m3): ");
Serial.println(hcho_data.HCHO_UGM3);
Serial.println();
}
delay(1000);
// Do other stuff...
}