Quickstart: Using MQSensorsLib with an MQ-4 sensor
masterTo use the library, include MQUnifiedsensor.h and define your hardware and software parameters. You must specify the board type, the analog pin, the sensor type, the voltage resolution, and the ADC bit resolution. After declaring the sensor object, configure the regression method (e.g., "Exponential"), set the coefficients (A and B) for the gas concentration equation, and provide the $R_0$ value obtained from calibration. Finally, call .init() in setup and .update() in the loop to read the sensor values.
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("Arduino UNO")
#define Pin (A4) //Analog input 4 of your arduino
/***********************Software Related Macros************************************/
#define Type ("MQ-4") //MQ4
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10) // For arduino UNO/MEGA/NANO
#define RatioMQ4CleanAir (4.4) //RS / R0 = 60 ppm
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
// Setup
MQ4.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
MQ4.setR0(3.86018237); // Value getted on calibration
// Loop
MQ4.init();
MQ4.update();
float ppmCH4 = MQ4.readSensor();