Previous                                    Tutorial                                  Tutorial

Introduction

In this lesson, we will show how to use the Osoyoo Yun IoT Kit to get the MQ2 Gas sensor status remotely.

HARDWARE

SOFTWARE

Connection

Build the circuit as below:

Here we connect the MQ2 gas sensor Pin A0 to Arduino analog Pin A1.

Code Program

After above operations are completed, make sure that the Yun Shield is on the same network with the computer. Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

#define BLYNK_PRINT Console
#include <Bridge.h>
#include <Console.h>
#include <BlynkSimpleYun.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
SimpleTimer timer;

//Widget LED
WidgetLED led1(V0); //register to virtual pin 0

//smoke sensor mq2
//connect to A1
 int smoke_mq2 = A1;
int smoke = 0; 

void SMOKESENSOR () {
    Console.println("SMOKE SENSOR START!");
smoke = analogRead(smoke_mq2);
Console.print("smoke_val = ");
  Console.println(smoke);   //The serial will print the smoke value
  if(smoke>=130)       // the point at which the state of LEDs change 
  {
    digitalWrite(Buzzer, HIGH);
    led1.on();
  }
  else
  {
    Console.println("No smoke!");
    digitalWrite(Buzzer, LOW);
    led1.off();
  } 
  //  delay(500);
}

void setup()
{
  Blynk.begin(auth); 
  Bridge.begin();
  Console.begin();
  timer.setInterval(10000, FLAMESENSOR);
  while (!Console);{}
    
}

void loop() {
  // this is where the "polling" occurs
  timer.run();
  Blynk.run();

}

Add a Widget

Overhere we need to add an LED wiget. Follow the next operations:

Tap anywhere on the canvas to open the widget box. All the available widgets are located here. Now pick an LED.

Drag-n-Drop - Tap and hold the Widget to drag it to the new position.

Widget Settings - Each Widget has it’s own settings. Tap on the widget to get to them. The most important parameter to set is PIN. Connect the LED widget to V0.

Running Result

After you finished all above operations, open the Serial Monitor, then open the Blynk APP, press the PLAY button. This will switch you from EDIT mode to PLAY mode where you can interact with the hardware. While in PLAY mode, you won’t be able to drag or set up new widgets, press STOP and get back to EDIT mode.

At the same time, you can see the hardware side as below:

So, you can remote check the gas sensor status now.

Previous                                    Tutorial                                  Tutorial