Reference

Library

  • Library files: DFRobot MHZ9041A Github
  • Installation instructions: Download the library and install via .zip in Arduino IDE, or search and install "DFRobot_MHZ9041A" in the library manager.

UART protocol

UART config: 115200bps 8-N-1
Note: The UART output is an ACSII string that is uploaded every 1S, totaling 44 bytes.

Function Code Frame Head Concentration Space Temperature Space Reserve Space Reserve
Byte Index 0 1~7 8 9~13 14 15~21 22 23~27
Byte Length 1 7 1 5 1 7 1 5
Unit / %LEL / / / /
Example A +000.00 < SP > +23.7 < SP > 0666.22 < SP > +16.1
Space Reserve Space Space Error Code Space XOR CheckSum CR LF
Byte Index 28 29~35 36 37~38 39 40~41 42 43
Byte Length 1 7 1 2 1 2 1 1
Unit / V / / / / < CR > < LF >
Example < SP > B+066.0 < SP > 00 < SP > 3B < CR > < LF >

Output String Parsing Guide:
Example: A+000.00 +24.0 0665.38 +16.1 B+066.0 00
A: Frame header
+000.00: Methane concentration value, with two decimal places; the second decimal digit is always 0. Unit: %LEL
+24.0: Ambient temperature. Unit: °C
0665.38: Reserved
+16.1: Reserved
B+066.0: Reserved
00: Error code value — 0: normal; 1: abnormal

BIT7~5 BIT4 BIT3 BIT2 BIT1 BIT0
Reserve Laser signal abnormal Weak laser signal Reserve Ambient temperature abnormal Temperature control status abnormal

API Description

/**
 * @enum eFaultCode_t
 * @brief Types of sensor fault codes
 */
typedef enum {
  eSensorNormal = 0x00,                   // 00 Sensor operating normally
  eTempControlError = 0x01,               // 01 Temperature control error
  eAmbientTempError = 0x02,               // 02 Ambient temperature error
  eAmbientAndTempControlError = 0x03,     // 03 Ambient temperature error & temperature control error
  eLaserSignalWeak = 0x04,                // 04 Weak laser signal
  eAmbientAndSignalWeakError = 0x06,      // 06 Ambient temperature error & weak laser signal
  eLaserSignalError = 0x10,               // 16 Laser signal error
  eAmbientAndSignalError = 0x12,          // 18 Ambient temperature error & laser signal error
} eFaultCode_t;

/**
 * @enum eModuleMode_t
 * @brief sensor work mode
 */
typedef enum {
  ePassivityMode = 0x00,
  eActiveMode = 0x01,
} eWorkMode_t;

/**
 * @fn setMode
 * @brief Set the working mode
 * @param mode Working mode
 * @note 0x00: Active mode, 0x01: Passive mode
 * @return null
 */
void setMode(eWorkMode_t mode);

/**
 * @fn getSourceData
 * @brief Get raw sensor data
 * @return Raw data string, up to 43 bytes in length
 */
String getSourceData(void);

/**
 * @fn getCH4Concentration
 * @brief Get methane (CH4) concentration
 * @return CH4 concentration value, two decimal places, the second digit is always 0, unit: %LEL
 */
float getCH4Concentration(void);

/**
 * @fn getTemperature
 * @brief Get ambient temperature
 * @return Temperature in degrees Celsius
 */
float getTemperature(void);

/**
 * @fn getErrorMsg
 * @brief Get error code information
 * @return Error code
 */
eFaultCode_t getErrorMsg(void);

/**
 * @fn getActiveData
 * @brief Get raw data in active reporting mode
 * @return Raw data string in active mode
 */
String getActiveData(void);

/**
 * @fn reset
 * @brief Reset the onboard methane sensor; hold the reset pin low for 2 seconds to complete the reset
 * @return null
 */
void reset(void);

/**
 * @fn setDeviceID
 * @brief Set the module address,range 0x03~0x7F
 * @return Returns 1 if successful, otherwise returns 0
 */
bool setDeviceID(uint8_t id);

/**
 * @fn getDeviceID
 * @brief Get the module address
 * @return Module address
 */
uint8_t getDeviceID(void);

Was this article helpful?

TOP