MQ-2 sensor

I have a a MQ-2 sensor which is a smoke and gases detection is there any idea how to write the arduino id code and which library sholud i install and help for this project.

regards

Arduino Playground - HomePage & http://www.instructables.com/id/How-to-use-MQ2-Gas-Sensor-Arduino-Tutorial/

Google :wink:

1 Like

I could not found any useful code

where did you try to find code?
i see that in instructables link above is a simple code for this sensor. just needs reading an analog pin…

also, if you mind to read the comments on instructable page, there are even more details how to interpret the analog reading.

1 Like

The idea is that i am using a nodemcu esp8266 not an ardunio chip and the esp is connected to the blynk so the code must read the data from the sensor attached to the esp and show it on the blynk

Actually most code you find online relating to “arduino” can also be directly used on ESP dev boards (such as the nodemcu, wemos etc).

Please post a screenshot of the sensor data in the Serial monitor on the ESP with no Blynk.

This official help document should guide you through everything: http://help.blynk.cc/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

You can use Arduino code on the NodeMCU… You should look into learning how to do that before anything else.

Or stick with Lua script, but I don’t think Blynk works natively with Lua?

I found this sketch and it works for me well maybe you can use it. I use a esp8266-01 and MQ-2 sends a notification when it detects gas, greetings

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <Ethernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Proje  ct Settings (nut icon).
char auth[] = "xxxxxxxxx"; // 

int buttonStatePrev;

void setup()
{
  
  Serial.begin(9600);
  Blynk.begin(auth, "xxxx", "xxxxxxxx");
   while (Blynk.connect() == false) {
// Wait until connected
 }
  
  pinMode(3, INPUT);  
  buttonStatePrev = HIGH;

}



void loop()
{
  Blynk.run();
int buttonStateCurr = digitalRead(3);
  if (buttonStateCurr == LOW && buttonStatePrev == HIGH) {
   Serial.println("Button is held down but activated the function only ONCE.");
   
   buttonStatePrev = buttonStateCurr;
   delay(50);
   } 

if (buttonStateCurr == HIGH && buttonStatePrev == LOW) {
Serial.println("Alerta, se ha detectado gases inflamables");
Blynk.notify ("Alerta, se ha detectado gases inflamables");
buttonStatePrev = buttonStateCurr;
delay(50);
  }   
}
1 Like

but have not you use any libraries only this code ?
in other work what did you include above in the code

@hkjavier, please edit your post and format your code using the template below

```cpp
CODE
```

1 Like

Already in code format with libraries

1 Like

correct!!!

that is the beauty of such a simplistic sensor - it sends value of 0 to 1023 or LOW/HIGH depending on what breakout board you have…

it is ENTIRELY up to you to decide what you do with that information :wink:

ok that is good and , just a simple question is it possible for example if i want to represent the percentage of the gas exist on the blynk screen ?

No, because it doesn’t sound like you have calibrated it?

Google “calibration mq-2” for methodology…

But you can do “backyard” calibration too…

pinMode(3, INPUT);
I did not get this , which pin was the sensor conncted to ESP