Example Code for Arduino-Use I2C to Read Sensor Data

Last revision 2026/01/15

Download the program to Arduino UNO and open the serial monitor to check the gas concentration using I2C communication.

Hardware Preparation

  • DFRuino UNO R3 x1
  • DFR0784 Smart Gas Sensor Terminal x1
  • Gas probe x1
  • Jumper wires

Software Preparation

Other Preparation Work

  • Connect the module to the Arduino according to the connection diagram above. Of course, you can also use it with Gravity I/O Expansion Board to build the project prototype more conveniently and quickly.
  • Turn the selector switch on the sensor to the I2C side.
  • The default I2C address is 0x77, which corresponds to ADDRESS_3 in the code. If you need to modify the I2C address, you can first configure the hardware I2C address through the DIP switch on the module, and modify the I2C address definition ADDRESS_X in the sample code. The corresponding relationship between the DIP switch and the I2C address parameter is as follows:
    • ADDRESS_0: 0x77, A0=0, A1=0
    • ADDRESS_1: 0x76, A0=1, A1=0
    • ADDRESS_2: 0x75, A0=0, A1=1
    • ADDRESS_3: 0x74, A0=1, A1=1
  • Download and install the DFRobot_GasSensor Library. (About how to install the library?)
  • Open Arduino IDE and upload the following code to Arduino UNO.
  • Open the serial port monitor of Arduino IDE, adjust the baud rate to 115200, and observe the serial port print result.

Sample Code

/*!
  * @file  initiativereport.ino
  * @brief The sensor actively reports all data
  * @n Experimental method: Connect the sensor communication pin to the main control, then burn codes into it. 
  * @n Communication mode selection, dial switch SEL:0: IIC, 1: UART
@n I2C address selection, the default I2C address is 0x77, A1 and A0 are combined into 4 types of IIC addresses
                | A1 | A0 |
                | 0  | 0  |    0x77
                | 0  | 1  |    0x76
                | 1  | 0  |    0x75
                | 1  | 1  |    0x74   default i2c address
  * @n Experimental phenomenon: Print all data via serial port
  * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
  * @licence     The MIT License (MIT)
  * @author      PengKaixing([email protected])
  * @version     V1.0
  * @date        2021-03-28
  * @get         from https://www.dfrobot.com
  * @url         https://github.com/dfrobot/DFRobot_GasSensor
*/
#include "DFRobot_GasSensor.h"
//Enabled by default, use IIC communication at this time. Use UART communication when disabled
#define I2C_COMMUNICATION
#ifdef  I2C_COMMUNICATION
#define I2C_ADDRESS    0x77
  DFRobot_GAS_I2C gas(&Wire ,I2C_ADDRESS);
#else
#if (!defined ARDUINO_ESP32_DEV) && (!defined __SAMD21G18A__)
/**
  UNO:pin_2-----RX
      pin_3-----TX
*/
  SoftwareSerial mySerial(2,3);
  DFRobot_GAS_SoftWareUart gas(&mySerial);
#else
/**
  ESP32:IO16-----RX
        IO17-----TX
*/
  DFRobot_GAS_HardWareUart gas(&Serial2); //ESP32HardwareSerial
#endif
#endif
void setup() {
/**
  Serial port initialization, used to view print output
*/
  Serial.begin(115200);
  
/**
  Sensor initialization, used to initialize the serial port or initialize the IIC, determined by the communication method used at the time
*/
  while(!gas.begin())
  {
    Serial.println("NO Deivces !");
    delay(1000);
  }
/**
  The data acquisition mode: the sensor actively reports data
*/
  while (!gas.changeAcquireMode(gas.INITIATIVE))
  {
    Serial.println("waiting for success!");
    delay(1000);
  }
  Serial.println("change acquire mode success!");  
  delay(1000);  
}
/**
  Change the probe type to: O2
*/
  while (!gas.setGasType(gas.O2))
  {
    delay(1000);
  }
  Serial.println("change Gas Type success!");
}
void loop() {
  if(true==gas.dataIsAvailable())
  {
    Serial.println("========================");
    Serial.print("gastype:");
    Serial.println(AllDataAnalysis.gastype);
    Serial.println("------------------------");
    Serial.print("gasconcentration:");
    Serial.print(AllDataAnalysis.gasconcentration);
    if (AllDataAnalysis.gastype.equals("O2"))
      Serial.println(" %VOL");
    else
      Serial.println(" PPM");
    Serial.println("------------------------");
    Serial.print("temp:");
    Serial.print(AllDataAnalysis.temp);
    Serial.println(" ℃");
    Serial.println("========================");
  }
  delay(1000);
}

Result

Open the serial monitor and you can get the O2 gas concentration value.

Note:
The product needs to be warmed up for more than 5 minutes when powered on for the first time, and it is recommended to warm up for more than 24 hours if it has not been used for a long time.

Additional Information

  • The sample code uses an O2 probe. If you use other probes, you need to modify the detection gas type of the corresponding probe.

Was this article helpful?

TOP