MQ2 gas sensor

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jadro
Posts: 25
Joined: Wed Apr 29, 2020 7:02 pm

MQ2 gas sensor

Post by jadro » Mon Jul 12, 2021 2:21 pm

Hi,
I have MQ2 gas sensor with digital and analog pins (4 pins, D0, A0, Gnd, Vcc).

Is there anyone do thw program to read this senso with pure micropython on ESP8266 Lolin board?

Thanx in advance,
Jadro

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MQ2 gas sensor

Post by jimmo » Mon Jul 12, 2021 3:27 pm

jadro wrote:
Mon Jul 12, 2021 2:21 pm
Is there anyone do thw program to read this senso with pure micropython on ESP8266 Lolin board?
Have you seen https://github.com/kartun83/micropython-MQ -- Looks like it should be exactly what you need.

jadro
Posts: 25
Joined: Wed Apr 29, 2020 7:02 pm

Re: MQ2 gas sensor

Post by jadro » Mon Jul 12, 2021 3:47 pm

jimmo wrote:
Mon Jul 12, 2021 3:27 pm
jadro wrote:
Mon Jul 12, 2021 2:21 pm
Is there anyone do thw program to read this senso with pure micropython on ESP8266 Lolin board?
Have you seen https://github.com/kartun83/micropython-MQ -- Looks like it should be exactly what you need.
Yes I try this scripts but does not work for me, shows some errors, same as reported...

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MQ2 gas sensor

Post by jimmo » Tue Jul 13, 2021 1:02 am

jadro wrote:
Mon Jul 12, 2021 3:47 pm
Yes I try this scripts but does not work for me, shows some errors, same as reported...
What are the errors you see? What do you mean by "same as reported" ?

jadro
Posts: 25
Joined: Wed Apr 29, 2020 7:02 pm

Re: MQ2 gas sensor

Post by jadro » Tue Jul 13, 2021 6:00 pm

Errors that is present on start using uPyCraft:
exec(open('test.py').read(),globals())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 3, in <module>
ImportError: no module named 'MQ2'

I dont see why it does not see MQ2?!?! I copy MQ2.py and BaseMQ.py in root folder with main test script test.py but unseccessfuly...
I try import folder as import MQ then from MQ2 import MQ2, even from MQ.MQ2 import MQ2 and from .MQ2 import MQ2...

Can somebody try this?

jadro
Posts: 25
Joined: Wed Apr 29, 2020 7:02 pm

Re: MQ2 gas sensor

Post by jadro » Wed Jul 14, 2021 7:28 am

At least, test.py does not work as is, it must have import MQ as module:

import MQ
from MQ import MQ2
import utime

class App:
def __init__(self, pin = 0):
self.sensor = MQ2.MQ2(pinData = pin, baseVoltage = 3.3)

def Run(self):
print("Calibrating")
self.sensor.calibrate()
print("Calibration completed")
print("Base resistance:{0}".format(self.sensor._ro))
while True:
print("Smoke: {0}".format(self.sensor.readSmoke()))
print("LPG: {0}".format(self.sensor.readLPG()))
print("Methane: {0}".format(self.sensor.readMethane()))
print("Hydrogen: {0}".format(self.sensor.readHydrogen()))
utime.sleep(5)

App().Run() # or app = App() and app.Run()

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MQ2 gas sensor

Post by jimmo » Wed Jul 14, 2021 12:15 pm

Can you try doing this at the REPL:

Code: Select all

>>> import os
>>> os.listdir()
>>> import MQ2
and show the output? It doesn't sound like the files are being copied to the right place?

FWIW, it seems that a lot of people run into these sorts of problems with uPyCraft... it appears to be unmaintained. Maybe you could try mpremote (https://pypi.org/project/mpremote/), rshell (https://github.com/dhylands/rshell) or Thonny (https://thonny.org/)

jadro
Posts: 25
Joined: Wed Apr 29, 2020 7:02 pm

Re: MQ2 gas sensor

Post by jadro » Thu Jul 15, 2021 12:54 pm

Thanx jimmo, I use Thonny for ESP32 and it is great and dont have this issues, is the best for such a things.

Post Reply