Reference
Library
- Library files: DFRobot_GasSensor Library
- Installation instructions: Download the library and install it via Arduino IDE's "Sketch > Include Library > Add .ZIP Library" menu.
- Usage examples: Refer to the "examples" folder in the library, including passive/initiative data acquisition and threshold alarm.
Serial port protocol usage tutorial
Through the UART serial communication protocol, you can connect DFR0784 Gravity: Electrochemical Smart Gas Sensor Terminal to any controller with UART for data reading and sensor configuration.
Note: At this time, the SEL end of the DIP switch on the sensor must be placed in the "1" position
Serial Port Parameter Setting
| Baud rate | 9600 |
|---|---|
| Data bit | 8 bit |
| Check bit | 1 bit |
Communication Protocol Description

① 0x78——Modify Terminal Communication Mode
The terminal has two communication modes, active uploading and question and answer. The factory default is active uploading mode, and data is sent every 1s.
Send
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | addr | Command | Communication mode | -- | -- | -- | -- | Check value |
| 0xFF | 0x01 | 0x78 | Active upload mode: 0x03 Question and answer mode: 0x04 |
0x00 | 0x00 | 0x00 | 0x00 | 0x84 0x83 |
EXP.FF 01 78 03 00 00 00 00 84 (switch to initiative mode)
EXP.FF 01 78 04 00 00 00 00 83 (switch to passive mode)
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Back to calibration | -- | -- | -- | -- | -- | Check value |
| 0xFF | 0x78 | Success: 0x01 Failure: 0x00 |
0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x87 0x88 |
EXP.FF 78 01 00 00 00 00 00 87
② Initiative Mode,Data Format
In the active upload mode, the terminal will return data every 1s. The data format is as follows.
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Gas concentration high bit |
Gas concentration low bit |
Gas type | Decimal places | Temperature value High |
Temperature value lower |
Check value |
| 0xFF | 0x88 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | -- |
Note:
- Gas concentration value = (high gas concentration x 256 + low gas concentration) x resolution
- The decimal place is 0, the resolution is 1; the decimal place is 1, the resolution is 0.1; the decimal place is 2, the resolution is 0.01
- For the calculation method of temperature value, please refer to the sample code below: "Calculation of temperature value"
Gas Type Table
| Gas Type | Command | Gas Type | Command |
|---|---|---|---|
| NH3 | 0x02 | SO2 | 0x2B |
| H2S | 0x03 | NO2 | 0x2C |
| CO | 0x04 | HCL | 0x2E |
| O2 | 0x05 | CL2 | 0X31 |
| H2 | 0x06 | HF | 0x33 |
| O3 | 0x2A | PH3 | 0x45 |
③ 0x86——Passive Mode, Read Gas Concentration Data
In the question and answer mode, you need to send commands to read various parameters of the terminal. The method of reading the gas concentration is as follows.
Send
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | addr | Command | -- | -- | -- | -- | -- | Check value |
| 0xFF | 0x01 | 0x86 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x79 |
EXP.FF 01 86 00 00 00 00 00 79
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Gas concentration high bit |
Gas concentration low bit |
Gas type | Decimal places | -- | -- | Check value |
| 0xFF | 0x86 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x7A |
EXP.FF 86 00 00 00 00 00 00 7A
Note:
- Gas concentration value = (high gas concentration x 256 + low gas concentration) x resolution
- The decimal place is 0, the resolution is 1; the decimal place is 1, the resolution is 0.1; the decimal place is 2, the resolution is 0.01
④ 0x87——Passive Mode, Read Temperature Data
In the question and answer mode, you need to read various parameters of the terminal by sending commands. The terminal integrates the thermistor, which can obtain the real-time temperature of the terminal. The way to read the terminal temperature is as follows.
Send
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | addr | Command | -- | -- | -- | -- | -- | Check value |
| 0xFF | 0x01 | 0x87 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x78 |
EXP.FF 01 87 00 00 00 00 00 78
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Temperature data high bit |
Temperature data low bit |
-- | -- | -- | -- | Check value |
| 0xFF | 0x87 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x79 |
EXP.FF 87 00 00 00 00 00 00 79
Note:
For the calculation method of temperature value, please refer to the sample code below: "Calculation of temperature value"
⑤ 0x88——Passive Mode, Read Temperature and Gas Concentration Data
In the question and answer mode, you need to read various parameters of the terminal by sending commands, and the way to read the temperature and gas concentration data of the terminal is as follows.
Send
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | addr | Command | -- | -- | -- | -- | -- | Check value |
| 0xFF | 0x01 | 0x88 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x77 |
EXP.FF 01 88 00 00 00 00 00 77
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Gas concentration high bit |
Gas concentration low bit |
Gas type | Decimal places | Temperature value High |
Temperature value lower |
Check value |
| 0xFF | 0x88 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x78 |
EXP. FF 88 00 00 00 00 00 00 78
Note:
- Gas concentration value = (high gas concentration x 256 + low gas concentration) x resolution
- The decimal place is 0, the resolution is 1; the decimal place is 1, the resolution is 0.1; the decimal place is 2, the resolution is 0.01
- For the calculation method of temperature value, please refer to the sample code below: "Calculation of temperature value"
⑥ 0x89——Configure Threshold Alarm Function
The terminal has a threshold alarm function, the alarm threshold and judgment logic can be configured. The configuration method is as follows,After the configuration is successful, the entire system needs to be powered on again to take effect.
Note: When no external controller is connected and only the sensor is used to achieve this function, the sensor must be set to active upload mode after the parameters related to the threshold alarm function are configured.
Send
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Empty | Command | Function switch setting | Alarm concentration threshold high bit |
Alarm concentration threshold low bit |
-- | -- | Check value |
| 0xFF | 0x01 | 0x89 | On: 0x01 Off: 0x00 |
0x00 | 0x00 | 0x00 | 0x00 | 0x71 0x70 |
EXP. FF 01 89 00 00 05 00 00 71 (turn off the alarm function)
EXP. FF 01 89 01 00 05 00 00 70 (open the alarm function)
Please refer to ⑤ for how to calculate the concentration.
Return
| Byte0 | Byte1 | Byte2 | Byte3 | Byte4 | Byte5 | Byte6 | Byte7 | Byte8 |
|---|---|---|---|---|---|---|---|---|
| Start bit | Command | Return configuration result | Function switch status | Alarm concentration threshold high bit |
Alarm concentration threshold low bit |
-- | -- | Check value |
| 0xFF | 0x89 | Success: 0x01 Failure: 0x00 |
On: 0x01 Off: 0x00 |
0x00 | 0x00 | 0x00 | 0x00 | -- |

To configure the threshold alarm by code using a controller such as Raspberry Pi, you can use this python code: GAS_ALA.zip
Checksum Calculation
unsigned char data[] = {0xFF,0x01,0x87,0x00,0x00,0x00};
unsigned char FucCheckSum(unsigned char *i,unsigned char ln)
{
unsigned char j,tempq=0;
i+=1;
for(j=0;j<6;j++)
{
tempq+=*i;
i++;
}
tempq=(~tempq)+1;
return(tempq);
}
void setup() {
Serial.begin(115200);
Serial.println(FucCheckSum(data,6),HEX);
}
void loop() {
}
Calculation of Temperature Value
byte Temp_H = 0x01;//Temperature data high bit
byte Temp_L = 0xD9;//Temperature data low bit
void setup() {
Serial.begin(115200);
uint16_t temp_ADC = (Temp_H << 8) + Temp_L;
float Vpd3 = 3 * (float)temp_ADC / 1024;
float Rth = Vpd3 * 10000 / (3 - Vpd3);
float Temp = 1 / (1 / (273.15 + 25) + 1 / 3380.13 * log(Rth / 10000)) - 273.15;
Serial.println(Temp);
}
void loop() {
}
API Description
DFR0784 Gravity: Electrochemical Smart Gas Sensor Terminal There are two data reading modes: active upload and passive response. The factory default is active upload mode, and users can adjust them in the code according to their needs.
Mode Selection Function "changeAcquireMode()"
Modify the parameters in brackets of the "changeAcquireMode()" function to adjust the data sending mode.
"INITIATIVE" is the active upload mode. In the active upload mode, the sensor will automatically upload parameters every 1 second;
"PASSIVITY" is the passive response mode. In the passive response mode, the sensor will feedback the parameters only every time the data reading function is called.
gas.changeAcquireMode(gas.INITIATIVE)
/*
gas.INITIATIVE // Active upload mode
gas.PASSIVITY // Passive response mode
*/
Set the Probe Type Function "setGasType()"
Set the probe type by the "setGasType()" function.
gas.setGasType(/*Gas type*/gas.O2);
Read the Probe Type Function "queryGasType()"
Through the "queryGasType()" function, You can get the type of current gas probe.
gas.queryGasType();
For probe compatible types and corresponding parameters, please refer to the table below.
| Gas type | CO | O2 | NH3 | H2S | NO2 | HCL |
|---|---|---|---|---|---|---|
| Detection range | (0-1000)ppm | (0-25)%VOL | (0-100)ppm | (0-100)ppm | (0-20)ppm | (0-10)ppm |
| Resolution | 1ppm | 0.1%VOL | 1ppm | 1ppm | 0.1ppm | 0.1ppm |
| V0 voltage output range | (0.6-3)V | (1.5-0)V | (0.6-3)V | (0.6-3)V | (2-0)V | (2-0)V |
| Response time (T90) | ≤30S | ≤15S | ≤150S | ≤30S | ≤30S | ≤60S |
| Gas type | H2 | PH3 | SO2 | O3 | CL2 | HF |
|---|---|---|---|---|---|---|
| Detection range | (0-1000)ppm | (0-1000)ppm | (0-20)ppm | (0-10)ppm | (0-20)ppm | (0-10)ppm |
| Resolution | 1ppm | 0.1ppm | 0.1ppm | 0.1ppm | 0.1ppm | 0.1ppm |
| V0 voltage output range | (0.6-3)V | (0.6-3)V | (0.6-3)V | (2-0)V | (2-0)V | (2-0)V |
| Response time (T90) | ≤120S | ≤30S | ≤30S | ≤120S | ≤60S | ≤60S |
Gas Concentration Reading Function "readGasConcentrationPPM()"
The feedback gas concentration value of the gas sensor can be read through the "readGasConcentrationPPM()" function.
gas.readGasConcentrationPPM();
Temperature Reading Function "readTempC()"
The onboard temperature sensor data can be read through the "readTempC()" function.
gas.readTempC();
Voltage Reading Function "getSensorVoltage()"
The original voltage output V0 of the gas probe can be read through the "getSensorVoltage()" function.
gas.getSensorVoltage();
Configure Temperature Compensation Function "setTempCompensation()"
You can enable/disable the temperature compensation function through the "setTempCompensation()" function.
gas.setTempCompensation();
/*
\tgas.ON \tTurn on
\tgas.OFF\t\tTurn off
*/
Threshold Alarm Function "setThresholdAlarm()"
You can configure the threshold alarm information through the "setThresholdAlarm()" function
gas.setThresholdAlarm(gas.ON, 200, gas.LOW_THRESHOLD_ALA ,gas.queryGasType());
/*
\tgas.ON \tTurn on
\tgas.OFF\t\tTurn off
\t200\t\t\tSet threshold
\tgas.LOW_THRESHOLD_ALA Jump to low level when alarming
\tgas.HIGH_THRESHOLD_ALA Jump to high level when alarming
\tgas.queryGasType() Set alarm gas type
*/
I2C Address Group Configuration Function "changeI2cAddrGroup()"
You can configure the I2C address group code and switch between different address groups through the "changeI2cAddrGroup()" function.
In order to prevent address conflicts when using multiple sensors, we have prepared 8 groups with a total of 23 addresses. If necessary, You can use "change_sensor_iic_addr.ino" in the library file "example",to switch by modifying the group serial number configuration of "changeI2cAddrGroup()". After the serial port information displays "IIC addr change success!", power on again.
gas.changeI2cAddrGroup(i);
/*
\ti\t\t\tGroup number
//Group serial number and DIP switch configuration table
A0 A1Dial level 00 01 10 11
Group number Group address
1 0x60 0x61 0x62 0x63
2 0x64 0x65 0x66 0x67
3 0x68 0x69 0x6A 0x6B
4 0x6C 0x6D 0x6E 0x6F
5 0x70 0x71 0x72 0x73
6(Default address group) 0x74 0x75 0x76 0x77
7 0x78 0x79 0x7A 0x7B
8 0x7C 0x7D 0x7E
*/
Other Supplementary Information
Was this article helpful?
