Separating a MQ4 gas Sensor code into a .h and .cpp files

Hi members
I am new here. I was working with particle Photon and MQ4 I2C gas sensor.
I have a normal I2C code for this sensor

#include <application.h>
#include <spark_wiring_i2c.h>

// ADC121C_MQ4 I2C address is 0x50(80)
#define Addr 0x50

int raw_adc = 0;
double ppm = 0.0;
void setup()
  // Set variable
  Particle.variable("i2cdevice", "ADC121C_MQ4");
  Particle.variable("PPM", ppm);

  // Initialise I2C communication as MASTER
  // Initialise serial communication, set baud rate = 9600

void loop()
  unsigned int data[2];

  // Start I2C transmission
  // Select data register
  // Stop I2C transmission

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // raw_adc msb, raw_adc lsb
  if (Wire.available() == 2)
    data[0] =;
    data[1] =;

  // Convert the data to 12-bits
  raw_adc = ((data[0] & 0x0F) * 256) + data[1];
  ppm = (10000 / 4096.0) * raw_adc + 200.0;

  // Output data to dashboard
  Particle.publish("Methane concentration : ", String(ppm));

So i am a noob in coding and dont know how to seperate this code into .cpp and .h files to get the clean code.

Please help me out, how this code can be converted into .cpp and .h files.

Thanx in advance.

@rjrajbir, one approach is to create an MQ4 Class object which you define in the .h file and provide the class code in the .cpp file. The class would have a constructor and one function for reading the sensor which is essentially most of the code in loop() and another for getting the calculated ppm value. The constructor would be empty. The begin() class function could do the Wire.begin() and set the private or public class raw_adc and ppm variables to zero. A read_raw() function would return an int and a get_ppm() function would return a float of the last read raw value.

Using a class us the basis of a proper Arduino/Particle library so there are plenty of examples to draw from. Give it a shot and come back for help as you progress.


thanx for your response @peekay123,i’ll try this…but in the meantime could you share some example of this

This example is a little “over complicated” but the idea behind is maintained :slight_smile:
You have to add all 3 files (MQ4.ino, MQ4.cpp, MQ4.h) to your Web IDE
Please note I didn’t tested this, is compiling without any errors but I don’t have MQ4 sensor.


#include "MQ4.h"

MQ4 sensor;

double ppm_results = 0.0;

void setup(){ 

  Particle.variable("i2cdevice", "ADC121C_MQ4");
  Particle.variable("PPM", ppm_results);

void loop(){
  ppm_results = sensor.convert_To_ppm();

  Particle.publish("Methane concentration : ", String(ppm_results));


#include "MQ4.h"
#include "Particle.h"

int MQ4::getRawAdc(){
    int raw = getRawMQ4Reading();
    return raw;

double MQ4::convert_To_ppm(){
    int raw_adc = getRawAdc();
    double ppm = (10000 / 4096.0) * raw_adc + 200.0;
return ppm;


int MQ4::getRawMQ4Reading(){
   // unsigned int data[2];
    byte status = Wire.endTransmission();
    if(status != 0){
        Serial.println("read MQ2 failed");
        return 0;
        //It worked
        Wire.requestFrom(address, 2);

        if (Wire.available() == 2){
            byte msb =;
            byte lsb =;
            int reading = ((msb & 0x0F) *256)+lsb;
            return reading;


class MQ4{
    int getRawAdc();
    double convert_To_ppm();
    int address = 0x50;
    int MQ4_Register = 0x00;
    int getRawMQ4Reading();
1 Like

Thank you so much @dreamER, I’ll try this out.

Thanx Buddy Its working fine.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.