ostaquet / Arduino-MQ131-driver

Arduino driver for gas sensor MQ131 (Ozone / O3)
MIT License
64 stars 19 forks source link
arduino arduino-driver arduino-library driver gas-sensor ozone sensor

Arduino-MQ131-driver

Arduino library for ozone gas sensor MQ131

GitHub GitHub release GitHub issues

This is a comprehensive Arduino library to obtain ozone (O3) concentration in the air with the Winsen MQ131 sensor. The library supports both versions of the sensor (low concentration and high concentration), the calibration, the control of the heater, the environmental adjustments (temperature and humidity) and the output of values in ppm (parts per million), ppb (parts per billion), mg/m3 and µg/m3.

To know before starting...

How to install the library?

The easiest way to install the library is to go to the Library manager of the Arduino IDE and install the library.

  1. In the Arduino IDE, go into Menu Tools -> Manage Libraries...
  2. Search for MQ131
  3. Install MQ131 gas sensor by Olivier Staquet

Circuit

Remarks:

Breadboard schematics

MQ131 pinout

Schematics

Basic program to use your MQ131

#include "MQ131.h"

void setup() {
  Serial.begin(115200);

  // Init the sensor
  // - Heater control on pin 2
  // - Sensor analog read on pin A0
  // - Model LOW_CONCENTRATION
  // - Load resistance RL of 1MOhms (1000000 Ohms)
  MQ131.begin(2,A0, LOW_CONCENTRATION, 1000000);  

  Serial.println("Calibration in progress...");

  MQ131.calibrate();

  Serial.println("Calibration done!");
  Serial.print("R0 = ");
  Serial.print(MQ131.getR0());
  Serial.println(" Ohms");
  Serial.print("Time to heat = ");
  Serial.print(MQ131.getTimeToRead());
  Serial.println(" s");
}

void loop() {
  Serial.println("Sampling...");
  MQ131.sample();
  Serial.print("Concentration O3 : ");
  Serial.print(MQ131.getO3(PPM));
  Serial.println(" ppm");
  Serial.print("Concentration O3 : ");
  Serial.print(MQ131.getO3(PPB));
  Serial.println(" ppb");
  Serial.print("Concentration O3 : ");
  Serial.print(MQ131.getO3(MG_M3));
  Serial.println(" mg/m3");
  Serial.print("Concentration O3 : ");
  Serial.print(MQ131.getO3(UG_M3));
  Serial.println(" ug/m3");

  delay(60000);
}

The result gives us:

Calibration in progress...
Calibration done!
R0 = 1917.22 Ohms
Time to heat = 80 s
Sampling...
Concentration O3 : 0.01 ppm
Concentration O3 : 7.95 ppb
Concentration O3 : 0.02 mg/m3
Concentration O3 : 16.80 ug/m3

Usage

The driver has to be initialized with 4 parameters:

Before using the driver, it's better to calibrate it. You can do that through the function calibrate(). The best is to calibrate the sensor at 20°C and 65% of humidity in clean fresh air. If you need some log on the console, mention the serial in the function begin() (example by using the standard Serial: MQ131.begin(2,A0, LOW_CONCENTRATION, 1000000, (Stream *)&Serial);).

The calibration adjusts 2 parameters:

Those calibration values are used for the usage of the sensor as long as the Arduino is not restarted. Nevertheless, you can get the values for your sensor through the getters:

MQ131.getR0();
MQ131.getTimeToRead();

And set up the values in the initialization of your program through the setters:

MQ131.setR0(value);
MQ131.setTimeToRead(value);

In order to get the values from the sensor, you just start the process with the sample() function. Please notice that the function locks the flow. If you want to do additional processing during the heating/reading process, you should extend the class. The methods are protected and the driver can be extended easily.

MQ131.sample();

The reading of the values is done through the getO3() function. Based on the parameter, you can ask to receive the result in ppm (PPM), ppb (PPB), mg/m3 (MG_M3) or µg/m3 (UG_M3).

MQ131.getO3(PPM);
MQ131.getO3(PPB);
MQ131.getO3(MG_M3);
MQ131.getO3(UG_M3);

The sensor is sensible to environmental variation (temperature and humidity). If you want to have correct values, you should set the temperature and the humidity before the call to getO3() function with the function setEnv(). Temperature are in °C and humidity in %. The values should come from another sensor like the DHT22.

MQ131.setEnv(23, 70);

Links