Nodemcu with MQ2 smoke sensor

Hello Blynk,

Till now, I have successfully used Blynk in all my projects though I possess zero programming knowledge; however, I’m stuck with MQ2 smoke sensor coding. I’ve used a few of the codes mentioned in your community, but no luck. Please help me edit the below mentioned code according to the Blynk.

Thanks for helping me with the beautiful app.

int buzzer = 10;
int smokeA0 = A5;

// Your threshold value. You might need to change it.
int sensorThres = 400;

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  Serial.begin(9600);
}

void loop() {
  int analogSensor = analogRead(smokeA0);

  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    tone(buzzer, 1000, 200);
  }
  else
  {
    noTone(buzzer);
  }
  delay(100);
}

You seem to be missing a bit of code… and I needed to fix the formatting of what you did post… as per the directions…

Blynk - FTFC

Meanwhile your void loop() should only contain the basics like blynk.run() while you put your sensor functions in a timed loop using BlynkTimer.

I recommend you read through the Documentation, Help Center and try out the examples in the Sketch Builder (all these links are also at the top of this page) to get a good understanding of how programming with Blynk works…

I also recommend this link to Arduinos Reference Page… experimenting with code and searching for programming tutorials via Google will help you understand how to merge Blynk in with other Arduino code.

And finally you can try searching this forum again for keywords like MQ2 and Smoke Sensor, to see what others have done.

Well, this code won’t work with a NodeMCU as it uses analogue pin A5. The NodeMCU only has one analogue pin which is A0.

Once you have this code working successfully without Blynk then you’ll need to move all of the existing code out of void loop and into a function that’s called on a regular basis using a timer. All that should be in void loop is Blynk.run and Timer.run.
You’ll also have to add-in the Blynk libraries and your Wi-Fi credentials and a Blynk.begin command in your void setup.

I have no idea how frequently these smoke detectors can be sampled, but as this sketch has the sampling code in void loop then I guess it can be hundreds of times per second. However, I’d set-up your timer to run the new function that samples the sensor so that it only runs once every second (1000ms) to begin with.

Pete.

Thanks for your reply, Pete.

I’ve used AO on nodemcu and conneced a buzzer to D1 of nodemcu. The code is working fine without Blynk, but I need your help in using the above mentioned code with Blynk. Since I don’t possess any programming knowledge, could you please help me with it.

Earlier, I’ve used blynk codes which were available in your community, and I was successful.

I don’t know whether you would help with programming or not, but I am just trying to seek your help!

Thanks for your reply, Gunner.

Like I mentioned earlier, I don’t possess any programming knowledge, but I was very much successful using all the codes available here except the code for MQ2.

I’d appreciate if you’d really help me edit the code so that I can use it with Blynk app.

You aren’t likely to get forum users to write your code for you, but we’re happy to help if you have a go yourself and post your code and the results of what happens when you try it.

Pete.

I will be more than happy to post the results once I use the code.

Below is the code where I need your help on:

int buzzer = 1;
int smokeA0 = A0;

// Your threshold value. You might need to change it.
int sensorThres = 400;

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  Serial.begin(9600);
}

void loop() {
  int analogSensor = analogRead(smokeA0);

  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    tone(buzzer, 1000, 200);
  }
  else
  {
    noTone(buzzer);
  }
  delay(100);
}

First of all, you need to format your code correctly when you post to this forum. @Gunner fixed it for you first time around, and explained how to do it in future using back-ticks and cpp. Edit your post to fix that issue.

I’d then suggest that you use the sketch builder (link at the top of the page). Select your NodeMCU and a simple sketch such as Push Data. This will give you the basic format for a Blynk sketch with the libraries needed and a simple timer. Copy and paste the stuff I discussed earlier, and put your Wi-Fi credentials and Blynk Auth Code in there’s and see whet you get. If it doesn’t work then post your code (correctly formatted this time) and the results you’re getting and we’ll point you in the right direction.

Pete.

And I fixed it again :stuck_out_tongue_winking_eye:

1 Like

Not sure you understand… this is NOT a code factory or repair shop :wink: About the only time others start chipping in with code is after the OP (original post/post’er - AKA you) shows solid initiative on their part.

And I seriously doubt that you have searched this forum, as I already found a few references to Blynk code with this sensor… perhaps not fully functional drop in, but more than you have so far :stuck_out_tongue:

PS, I also started out using Blynk with no almost programming knowledge (copy paste only)… If I can learn, anyone can :stuck_out_tongue:

Can you please provide the connection diagram for the following code.

None of the code that’s previously been posted in this topic works with a NodeMCU and Blynk, so a wiring diagram isn’t going to help, especially if you aren’t able to take the clues you need from this:

I’m going to close this topic.
If you have a question then I’d suggest you create a new topic with details of the hardware and code that you’re using and the problems that you’re experiencing.

Pete.

1 Like