MQ Unit

This is the driver library of MQ Unit, which is used to obtain data from the MQ sensor.

Support the following products:

MQ

UiFlow2 Example

get MQ ADC value

Open the mq_core2_example.m5f2 project in UiFlow2.

This example gets the ADC value of the MQ Unit and displays it on the screen.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

get MQ ADC value

This example gets the ADC value of the MQ Unit and displays it on the screen.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import m5ui
 9import lvgl as lv
10from hardware import I2C
11from hardware import Pin
12from unit import MQUnit
13
14
15page0 = None
16label0 = None
17label1 = None
18label2 = None
19i2c0 = None
20mq_0 = None
21
22
23valid = None
24
25
26def setup():
27    global page0, label0, label1, label2, i2c0, mq_0, valid
28
29    M5.begin()
30    Widgets.setRotation(1)
31    m5ui.init()
32    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
33    label0 = m5ui.M5Label(
34        "Valid Flag:",
35        x=1,
36        y=76,
37        text_c=0x000000,
38        bg_c=0xFFFFFF,
39        bg_opa=0,
40        font=lv.font_montserrat_16,
41        parent=page0,
42    )
43    label1 = m5ui.M5Label(
44        "ADC 8bits:0",
45        x=1,
46        y=111,
47        text_c=0x000000,
48        bg_c=0xFFFFFF,
49        bg_opa=0,
50        font=lv.font_montserrat_16,
51        parent=page0,
52    )
53    label2 = m5ui.M5Label(
54        "ADC 12bits:0",
55        x=1,
56        y=145,
57        text_c=0x000000,
58        bg_c=0xFFFFFF,
59        bg_opa=0,
60        font=lv.font_montserrat_16,
61        parent=page0,
62    )
63
64    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
65    mq_0 = MQUnit(i2c0, 0x11)
66    mq_0.set_mq_mode(1)
67    page0.screen_load()
68
69
70def loop():
71    global page0, label0, label1, label2, i2c0, mq_0, valid
72    M5.update()
73    valid = mq_0.get_valid_tags()
74    if valid:
75        label0.set_text(str((str("Valid Flag:") + str(valid))))
76        label1.set_text(str((str("ADC 8bits:") + str((mq_0.get_adc_value(0))))))
77        label2.set_text(str((str("ADC 12bits:") + str((mq_0.get_adc_value(1))))))
78    else:
79        label0.set_text(str("Valid Flag: Wait heating"))
80
81
82if __name__ == "__main__":
83    try:
84        setup()
85        while True:
86            loop()
87    except (Exception, KeyboardInterrupt) as e:
88        try:
89            m5ui.deinit()
90            from utility import print_error_msg
91
92            print_error_msg(e)
93        except ImportError:
94            print("please update to latest firmware")

Example output:

None

API

MQUnit

class unit.mq.MQUnit(i2c, address=17)

Bases: object

Create a MQUnit object.

Parameters:
  • i2c (I2C) – The I2C bus the MQ Unit is connected to.

  • address (int) – The I2C address of the device. Default is 0x11.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hardware import I2C
from unit import MQUnit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
tof_0 = MQUnit(i2c0)
set_mq_mode(mode=1)

Set the working mode of the MQ sensor.

Parameters:

mode (int) – Working mode value.

Option:
  • 0 : Measurement off

  • 1 : Continuous heating mode

  • 2 : Pin Level Switching Mode

UiFlow2 Code Block:

set_mq_mode.png

MicroPython Code Block:

mq_0.set_mq_mode(1)
get_mq_mode()

Get the current working mode of the MQ sensor.

Returns:

Current working mode value.

Return type:

int

UiFlow2 Code Block:

get_mq_mode.png

MicroPython Code Block:

mode = mq_0.get_mq_mode()
set_led_status(status)

Set the LED status.

Parameters:

status (int) – When the LED is set to on, it lights up when a valid tag is detected, and the brightness is proportional to the ADC value, and it turns off when no tag is detected.

MicroPython Code Block:

mq_0.set_led_status(1)
get_led_status()

Get the LED status.

Returns:

True if LED status is on, False otherwise.

Return type:

bool

MicroPython Code Block:

led_on = mq_0.get_led_status()
set_heat_time(high_level_time=30, low_level_time=5)

Set heater high and low level time.

Parameters:
  • high_level_time (int) – Time for high heating level.

  • low_level_time (int) – Time for low heating level.

UiFlow2 Code Block:

set_heat_time.png

MicroPython Code Block:

mq_0.set_heat_time(30, 5)
get_heat_time()

Get heater high and low level time.

Returns:

[high_level_time, low_level_time]

Return type:

[int, int]

MicroPython Code Block:

times = mq_0.get_heat_time()
get_adc_value(precision)

Get ADC value.

Parameters:

precision (int) – 0 for 8-bit, 1 for 12-bit.

Returns:

ADC value.

Return type:

int

UiFlow2 Code Block:

get_adc_value.png

MicroPython Code Block:

value = mq_0.get_adc_value(1)
get_valid_tags()

Check if valid tags are detected.

Returns:

True if valid tags detected, False otherwise.

Return type:

bool

UiFlow2 Code Block:

get_valid_tags.png

MicroPython Code Block:

valid = mq_0.get_valid_tags()
get_ntc_adc_value(precision)

Get internal NTC ADC value.

Parameters:

precision (int) – 0 for 8-bit, 1 for 12-bit.

Returns:

NTC ADC value.

Return type:

int

MicroPython Code Block:

ntc = mq_0.get_ntc_adc_value(1)
get_ntc_res_value()

Get internal NTC resistance value.

Returns:

Resistance value.

Return type:

int

MicroPython Code Block:

res = mq_0.get_ntc_res_value()
get_voltage(channle)

Get voltage value from a specific channel.

Parameters:

channle (int) – Channel number.

Returns:

Voltage value.

Return type:

int

MicroPython Code Block:

voltage = mq_0.get_voltage(0)
get_firmware_version()

Get firmware version.

Returns:

Firmware version.

Return type:

int

UiFlow2 Code Block:

get_firmware_version.png

MicroPython Code Block:

ver = mq_0.get_firmware_version()
get_i2c_address()

Get current I2C address.

Returns:

MQ Unit I2C address, Default is 0x11.

Return type:

int

UiFlow2 Code Block:

get_i2c_address.png

MicroPython Code Block:

addr = mq_0.get_i2c_address()
set_i2c_address(addr)

Set new I2C address.

Parameters:

addr (int) – New I2C address (0x08~0x77).

UiFlow2 Code Block:

set_i2c_address.png

MicroPython Code Block:

mq_0.set_i2c_address(0x3A)