Example Code for Arduino-Read Gas Concentration (PPM)

This project demonstrates how to read gas concentration data (in PPM) directly from the sensor.

Hardware Preparation

Software Preparation

Wiring Diagram

Other Preparation Work

  1. Connect the module to the Arduino according to the wiring diagram above.
    You may also use a Gravity I/O adapter for faster and more convenient prototyping.

  2. Turn the selector switch to the I2C position.

  3. Verify or modify the I2C address:

    • The default I2C address is 0x78, corresponding to ADDRESS_3 in the code.

    • To change the address, adjust the DIP switch on the module, then update the corresponding ADDRESS_X definition in the code.

    • DIP switch and I2C address mapping:

      1. ADDRESS_0: 0x75 (A0=0, A1=0) — Default
      2. ADDRESS_1: 0x76 (A0=1, A1=0)
      3. ADDRESS_2: 0x77 (A0=0, A1=1)
      4. ADDRESS_3: 0x78 (A0=1, A1=1)
  4. Download and install the DFRobot_MICS library.

  5. Open Arduino IDE:

    • Upload the sample code to the Arduino UNO, or
    • Open getGasPPM.ino from the library examples and upload it.
  6. Open the Serial Monitor, set the baud rate to 115200, and observe the output.

Sample Code

 /*!
  * @file  getGasPPM.ino
  * @brief Reading Gas concentration, A concentration of one part per million (PPM).
  * @n When using IIC device, select I2C address, set the dialing switch A0, A1 (Address_0 is [0 0]), (Address_1 is [1 0]), (Address_2 is [0 1]), (Address_3 is [1 1]).
  * @n When using the Breakout version, connect the adcPin and PowerPin
  * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
  * @licence     The MIT License (MIT)
  * @author      ZhixinLiu([email protected])
  * @version     V1.1
  * @date        2021-04-19
  * @get         from https://www.dfrobot.com
  * @url         https://github.com/dfrobot/DFRobot_MICS
  */
#include "DFRobot_MICS.h"

#define CALIBRATION_TIME   3                      // Default calibration time is three minutes

// When using I2C communication, use the following program to construct an object by DFRobot_MICS_I2C
/**!
    iic slave Address, The default is ADDRESS_3
       ADDRESS_0               0x75             // i2c device address
       ADDRESS_1               0x76
       ADDRESS_2               0x77
       ADDRESS_3               0x78
*/
#define Mics_I2C_ADDRESS ADDRESS_3
DFRobot_MICS_I2C mics(&Wire, Mics_I2C_ADDRESS);

// When using the Breakout version, use the following program to construct an object from DFRobot_MICS_ADC
/**!
  adcPin is A0~A5
  powerPin is General IO
*/
#define ADC_PIN   A0
#define POWER_PIN 10
//DFRobot_MICS_ADC mics(/*adcPin*/ADC_PIN, /*powerPin*/POWER_PIN);

void setup() 
{
  Serial.begin(115200);
  while(!Serial);
  while(!mics.begin()){
    Serial.println("NO Deivces !");
    delay(1000);
  } Serial.println("Device connected successfully !");

  /**!
    Gets the power mode of the sensor
    The sensor is in sleep mode when power is on,so it needs to wake up the sensor. 
    The data obtained in sleep mode is wrong
   */
  uint8_t mode = mics.getPowerState();
  if(mode == SLEEP_MODE){
    mics.wakeUpMode();
    Serial.println("wake up sensor success!");
  }else{
    Serial.println("The sensor is wake up mode");
  }

  /**!
     Do not touch the sensor probe when preheating the sensor.
     Place the sensor in clean air.
     The default calibration time is 3 minutes.
  */
  while(!mics.warmUpTime(CALIBRATION_TIME)){
    Serial.println("Please wait until the warm-up time is over!");
    delay(1000);
  }
}

void loop() 
{
  /**!
    Gas type:
    MICS-4514 You can get all gas concentration
    MICS-5524 You can get the concentration of CH4, C2H5OH, H2, NH3, CO
    MICS-2714 You can get the concentration of NO2
      Methane          (CH4)    (1000 - 25000)PPM
      Ethanol          (C2H5OH) (10   - 500)PPM
      Hydrogen         (H2)     (1    - 1000)PPM
      Ammonia          (NH3)    (1    - 500)PPM
      Carbon Monoxide  (CO)     (1    - 1000)PPM
      Nitrogen Dioxide (NO2)    (0.1  - 10)PPM
  */
  float gasdata = mics.getGasData(C2H5OH);
  Serial.print(gasdata,1);
  Serial.println(" PPM");
  delay(1000);
  //mics.sleepMode();
}

Result

Open the serial port monitor and warm up for approximately 3 minutes to obtain the alcohol gas concentration data.

If you need to detect other gases, you should modify the detected gas settings in the sample code.
The sensor takes 3 minutes to warm up.

Was this article helpful?

TOP