Protect your home: build a gas detector system with Arduino Cloud, MicroWakeupper and Wemos D1 Mini

Introduction

Welcome to the world of home security with this article through which you will make a smart gas detector using the Arduino Cloud, together with the MicroWakeupper and the versatile Wemos D1 Mini.

The heart of this project is the MQ-135 gas sensor, which provides precise readings of the presence of gases in the air. Thanks to its double output, both analogue and digital, we will have the possibility of constantly monitoring the gas level when it exceeds a preset threshold.

We will discover how to connect the gas sensor to the MicroWakeupper, which will act as an awakener for our Wemos D1 Mini, allowing it to transmit gas data in real time to the Arduino Cloud platform. Here, you can conveniently view your readings through an intuitive real-time dashboard.

If your home security is your priority, join forces with the Arduino Cloud, the MicroWakeupper, and the Wemos D1 Mini to create a smart, customized, and highly efficient gas detector system. Explore this exciting project step by step and ensure a safe home environment for you and your family.

We will use the excellent IDE PlatformIO for this project, as usual.

What components do we need for the gas detector?

The list of components is not particularly long:

  • a breadboard to connect the Wemos D1 Mini (or its clone) to the other components
  • some DuPont wires (male – male, male – female, female – female)
  • an MQ-135 gas sensor
  • a 2N2222 transistor
  • a 3.3kΩ resistor
  • a 27kΩ resistor
  • a battery holder for 4 AA or AAA batteries (in order to have 6V)
  • a MicroWakeupper
  • and, of course, a Wemos D1 Mini (or its clone)!

I used a clone of the Wemos D1 Mini made by the company AZ-Delivery.

Let’s now look at these new components in more detail.

TheWemos D1 Mini


The Wemos D1 Mini is a compact and powerful ESP8266-based IoT development board, designed to simplify the process of creating network-connected projects. This small but versatile board offers a 32-bit ESP8266 processor, integrated Wi-Fi connectivity, and a wide range of input/output peripherals, all in a compact form factor. Featuring 11 digital I/O pins and a single analog port, the Wemos D1 Mini lets you easily connect sensors, actuators, and other devices to create custom, interactive designs. Its ease of use and compatibility with the Arduino IDE and PlatformIO make this board an ideal choice for electronics enthusiasts and developers who want to explore the potential of the Internet of Things (IoT) with a reliable and accessible device . The model I purchased is a clone of the AZ-Delivery company and looks like this:

The Wemos D1 Mini device used in the project
The Wemos D1 Mini device used in the project

To be able to connect it to the breadboard and to be able to connect the MicroWakeupper on it I used female headers with fairly long terminals, as in the following photo:

The Wemos D1 Mini device with female connectors equipped with long terminals
The Wemos D1 Mini device with female connectors equipped with long terminals

To prepare it, insert the connector terminals into the holes as far as they will go and solder them from the back side of the module. If you are not familiar with soldering I recommend reading the article Yet another tutorial on how to solder to learn how to do it correctly. The two connectors are female to allow us to insert the MicroWakeupper module on top (like any Arduino shield).

The MicroWakeupper


The MicroWakeupper is an ingenious device designed by Tobias Stöger to simplify the management of “deep sleep” in microcontrollers, focusing mainly on the Wemos D1 Mini and ESP8266-based clones. “Deep sleep” is a state of low power consumption, used to extend battery life in battery-powered devices. During this state, the microcontroller enters a deep sleep state, dramatically reducing power consumption. In this context, the MicroWakeupper serves as a bridge between the gas sensor, such as the MQ-135, and the microcontroller. When the sensor detects gas levels above a pre-established threshold, the MicroWakeupper awakens the microcontroller from the “deep sleep” state, thus allowing immediate data transmission to the Arduino Cloud. This intelligent approach effectively balances environmental monitoring with the need to preserve battery power, making the MicroWakeupper a versatile solution for IoT projects that require efficient power management.

MicroWakeupper Resources:

The MicroWakeupper device looks like this, seen from above:

The MicroWakeupper module seen from above
The MicroWakeupper module seen from above

To it you will have to solder two 8-pin male header connectors like this:

8 pin male header
8 pin male header

WARNING: The MicroWakeupper, by default, connects the battery (red wire) to its A0 terminal which, normally, is connected to the A0 terminal (which is the analog input) of the Wemos D1 Mini via the connector. This is because in normal use the MicroWakeupper makes the Wemos D1 Mini monitor the battery level. In our case, however, the A0 terminal of the Wemos D1 Mini will have to be connected to the analog output of the gas sensor in order to be able to measure it. So the operation to be done is to cut the A0 terminal of the connector soldered on the MicroWakeupper (or not solder it at all by removing it from the plastic before soldering the connector) so that it cannot be inserted into the corresponding female connector of the Wemos D1 Mini.

As you can see from this photo and the silk-screen prints of the two devices:

The MicroWakeupper (left) and the Wemos D1 Mini (right)
The MicroWakeupper (left) and the Wemos D1 Mini (right)

the terminals of the MicroWakeupper are arranged to match those of the Wemos D1 Mini by superimposing the first on the second.

Once you have soldered the connectors, you should have a situation like this:

On the left the Wemos D1 Mini with the female connectors ready to accommodate the MicroWakeupper terminals (on the right)
On the left the Wemos D1 Mini with the female connectors ready to accommodate the MicroWakeupper terminals (on the right)

You will also need to solder a 3-position female connector on the top side of the MicroWakeupper where the silkscreen says OUT, IN, GND as in the following photo:

The MicroWakeupper with the 3-position female connector on the top side
The MicroWakeupper with the 3-position female connector on the top side

The following photo shows the devices seen from the side:

The devices seen from the side with the connectors soldered
The devices seen from the side with the connectors soldered

As you will notice, the pin corresponding to the A0 terminal on the MicroWakeupper has been cut off.

In the next photo we see the MicroWakeupper inserted on the Wemos D1 Mini (seen from the side):

The MicroWakeupper inserted on the Wemos D1 Mini (seen from the side)
The MicroWakeupper inserted on the Wemos D1 Mini (seen from the side)

Obviously the two A0 terminals of the Wemos D1 Mini and the MicroWakeupper are not in contact since the one of the MicroWakeupper has been cut.

Notice how I left the Wemos D1 Mini female connector terminals long so I could plug it into the breadboard.

In the following image you can see a photo of the battery holder used in this project:

The battery holder
The battery holder

The complete gas detector, once programmed via PC, will be battery powered.

The MQ-135 sensor

The MQ-135 sensor is an electronic component that belongs to the gas sensor family. It is widely used for the detection and quantification of various types of gases, especially toxic gases and air pollutants. This sensor is a popular choice for projects requiring air quality monitoring in domestic, industrial or outdoor environments.

It can detect gases such as: ammonia, sulfides, LPG, propane, methane, hydrogen, alcohol, smoke and carbon monoxide and other harmful gases. Although it can detect such gases, it cannot distinguish the difference between them.

The one used in this article is produced by the company AZDelivery. Here is a detailed description of the MQ-135 sensor:

Principle of operation

The MQ-135 sensor operates on the principle of varying electrical resistance in response to the presence of certain gases in the air. The heart of the sensor is a sensitive element made of a thin organic semiconductor film that reacts chemically to the gases it comes into contact with. For best detection results, the gas sensor must be preheated. The best warm-up time for the sensor is more than 48 hours.

WARNING: the sensor is pre-heated/heated by powering it normally. You must absolutely NOT bring open flames near us!!

Detection of specific gases

The MQ-135 sensor is known for its detection of various types of gases, including:

  1. Carbon Monoxide (CO): the MQ-135 sensor is particularly sensitive to carbon monoxide, an odorless and tasteless toxic gas produced by the incomplete combustion of carbon, often found in vehicle exhaust and fire situations.
  2. Ammonia (NH3): this gas is often present in agriculture and the chemical industry and is toxic when inhaled in high concentrations.
  3. Methane (CH4): methane is the main constituent of natural gas and is flammable and explosive in specific air/gas proportions.
  4. Benzene (C6H6): Benzene is a volatile chemical that can be present in various industrial environments and is known to be carcinogenic.
  5. Volatile Organic Vapors (VOCs): these gases include a wide range of volatile chemical compounds that can be released from chemicals, solvents and construction materials.

Construction

The sensor is enclosed within two layers of thin stainless steel mesh called anti-explosive mesh. As a result, it can detect flammable substances without incident. Likewise, it provides protection for the sensor and filters suspended particles. In this way only gases can pass inside the detection chamber. The module has an integrated LM393 comparator chip that converts the readings into digital and analog signals.

Adjustable threshold

The module offers two types of output: one digital and the other analog. The output value from the analog one ranges from 0 to 1023 and is proportional to the total gas concentration detected. The digital output instead switches to 0 when the total gas concentration exceeds a certain threshold. The MQ-135 sensor, in fact, offers the possibility of regulating, via an integrated trimmer, the threshold beyond which its digital output triggers (going from 1 to 0), signaling that the total gas concentration corresponding to the set threshold has been exceeded . The sensor has two LEDs: one red and one green. The red one indicates normal operation of the module and lights up when it is powered. The green LED lights up when the threshold adjusted via the trimmer has been exceeded.

Temperature and humidity

The accuracy of the MQ-135 sensor is affected by ambient temperature and humidity. Therefore, it is important to take these variables into account when using the sensor in critical applications and calibrate it accordingly.

Common applications

The MQ-135 sensor finds application in a wide range of industries and projects, including:

  1. Air quality monitoring: it is used to measure the concentration of toxic gases or pollutants in the air in indoor and outdoor environments.
  2. Gas alarm systems: can be integrated into alarm systems to detect leaks of dangerous gases such as carbon monoxide or methane gas.
  3. Agricultural ammonia detection: it is used to monitor ammonia levels in livestock environments for animal health.
  4. Home automation projects: can be incorporated into home automation systems to trigger ventilation devices or alerts in the event of dangerous gas concentrations.
  5. Educational projects: the MQ-135 sensor is often used in educational projects to teach young students about gas sensing and air quality concepts.

Limitations and precautions

It should be noted that the MQ-135 sensor has variable sensitivity and can be affected by gases other than the specific substance it is intended to detect. Therefore, it is important to calibrate the sensor carefully and take environmental conditions into account.

WARNING: this project has a purely educational intent and does not claim to create a precise and accurate gas monitoring device like a commercial one. Therefore, it should not be used for monitoring purposes in potentially hazardous environments.

In conclusion, the MQ-135 sensor is a versatile and reliable gas detection device, widely used in applications requiring monitoring of air quality and environmental safety. However, it is essential to understand its features and limitations before using it in a specific project.

Arduino Cloud


Arduino Cloud is a powerful online platform designed to simplify and centralize the management of Arduino-based projects and IoT devices. Offering a wide range of features, Arduino Cloud allows developers to monitor and control their devices remotely effectively. The platform offers an intuitive user interface that allows you to easily configure devices, dashboards and connections between components. One of the distinctive aspects of Arduino Cloud is the possibility of creating customized dashboards, viewing the data coming from the sensors in real time. Furthermore, the platform offers a remote control mechanism, allowing users to activate or deactivate devices remotely. With the ability to store historical data and provide real-time notifications, the Arduino Cloud is perfectly suited for environmental monitoring projects, such as the proposed gas detector system. Its flexibility and scalability make it ideal for a wide range of applications, offering a reliable and secure environment to manage IoT projects efficiently and intuitively.

Project implementation

The electrical diagram

Let’s now see the electrical diagram created with Fritzing:

Complete gas detector wiring diagram
Complete gas detector wiring diagram

On the left of the diagram we see the top of the MicroWakeupper although, in reality, this is a “sandwich” between the MicroWakeupper (above) and the Wemos D1 mini (below). It is the terminals of the Wemos D1 mini that are inserted into the breadboard. The IN terminal of the MicroWakeupper (connected via the light blue wire to the Collector of the transistor) causes the MicroWakeupper to wake up the Wemos D1 mini if ​​it is in the HIGH state while bringing it to the “deep sleep” state if it is in the LOW state. The A0 analog output of the gas sensor is directly connected to the analog input of the Wemos D1 mini (internally disconnected from the A0 terminal of the MicroWakeupper since we cut it).

As you will notice, in the diagram there is a 2N2222 type transistor. It is used as an inverter, i.e. it takes as input (its Base via the 27kΩ resistor) the digital output of the gas sensor and inverts it. Why do we do this operation?

Let’s make a little introduction. The digital output D0 of the sensor changes state depending on whether or not the gas concentration exceeds a certain threshold established by adjusting the trimmer on the sensor itself. We use this digital signal (which can therefore only be high or low) to control the MicroWakeupper so that it sends the Wemos D1 Mini into the “deep sleep” state or wakes it up. In particular, it will send the Wemos D1 Mini into deep sleep when the gas concentration is below the threshold while it will wake it up when the gas concentration is above the threshold. In this case the Wemos D1 Mini wakes up, starts reading the gas concentration values ​​coming from the A0 output of the sensor and will send them to the Arduino Cloud to be represented on a graph on the dashboard (consider that once awakened it will take a few seconds to connect to WiFi and then to the Cloud).

The MicroWakeupper needs a high signal on the IN input to wake up the Wemos D1 Mini while when it receives a low signal it sends it into deep sleep. Unfortunately, the sensor’s digital signal has an opposite logic: it is low when the gas concentration exceeds the threshold while it is high when the gas concentration is below the threshold. Hence the need to invert the levels of this signal in order to correctly control the MicroWakeupper.

Let’s now create the objects and the dashboard on the cloud

First, if you haven’t already done so, you need to create an account on Arduino Cloud.

Once you have logged in, follow this video in which you will learn how to:

  • create the device that will map the board
  • create the object (thing) with the variable associated with the device created in the previous step
  • create the dashboard to associate with the variable

In this video we created:

  • the object (thing) called Thing_Gas with its variable gasVal (both as Name and as Declaration) of integer type, read only and with periodicity of 1 second;
  • the device named Device_Gas type ESP8266 LOLIN(WEMOS) D1 mini Lite (remember that the one used in this article is a clone distributed by the AZ-Delivery company);
  • the dashboard named Dashboard_Gas with a chart-type diagram named Gas level which displays the variable gasVal (variable belonging to Thing_Gas).

PLEASE NOTE: it is important to note that in the video, at minute 1:46, I downloaded a pdf that contains the connection parameters to the device. They are called Device ID and Secret Key and we will need them later. So download this pdf too and keep it somewhere.

Obviously the Device ID and Secret Key parameters are specific to each device, therefore they change as the device changes.

The sketch

Let’s create the PlatformIO project

We have already seen the procedure for creating a PlatformIO project in the article How to create a project for NodeMCU ESP8266 with PlatformIO. In this specific project as a platform you will have to choose the WeMos D1 mini Lite (WEMOS)

Do not install the libraries indicated in that article but install the Arduino_ConnectionHandler library, always following the usual procedure:

Installing the Arduino_ConnectionHandler library
Installing the Arduino_ConnectionHandler library

and the ArduinoIoTCloud library:

Installing the ArduinoIoTCloud library
Installing the ArduinoIoTCloud library

Also install the MicroWakeupper Wemos D1 Mini Battery Shield by Tobias library:

Installing the MicroWakeupper Library Wemos D1 Mini Battery Shield by Tobias
Installing the MicroWakeupper Library Wemos D1 Mini Battery Shield by Tobias

Now edit the platformio.ini file to add these two lines:

monitor_speed = 115200
upload_speed = 921600

so that it looks like this:

[env:d1_mini_lite]
platform = espressif8266
board = d1_mini_lite
monitor_speed = 115200
upload_speed = 921600
framework = arduino
lib_deps = 
	tstoegi/MicroWakeupper Wemos D1 Mini Battery Shield@^6.3.1
	arduino-libraries/ArduinoIoTCloud@^1.13.0
	arduino-libraries/Arduino_ConnectionHandler@^0.8.1

Now download the project from the link below:

Unzip the project you just downloaded and overwrite the main.cpp file in the src folder of the project created before.

Then, from the include folder of the project you just downloaded and unzipped, take the thingProperties.h file and copy it to the include folder of the project created before.

At this point you will have to edit this file in order to connect it to the cloud:

const char DEVICE_LOGIN_NAME[]  = "YYYYYYYYYYYYYYYYYYYYYYYYYYYY";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password

As you can see, in this section there are some parameters to set.

The first parameter (DEVICE_LOGIN_NAME) can be found in the pdf you downloaded when you created the object (you can find the reference in the previous video). In the pdf the parameter is called Device ID.

The SECRET_SSID and SECRET_OPTIONAL_PASS parameters are, respectively, the name and password of your WiFi network. So, instead of SECRET_SSID you will put the name of your network in double quotes (“) and instead of SECRET_OPTIONAL_PASS you will put your network password (always in double quotes).

The last parameter, SECRET_DEVICE_KEY, can always be found in the pdf you downloaded from the platform with the name Secret Key.

So the section in question should look like this:

const char DEVICE_LOGIN_NAME[]  = "Device ID from the downloaded pdf";
const char SSID[]               = "my_wifi_ssid";    // Network SSID (name)
const char PASS[]               = "my_wifi_password";    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = "Secret Key from the downloaded pdf";    // Secret device password

As you can see, in the DEVICE_LOGIN_NAME field I put the value taken from the pdf under Device ID (in this case Device ID from the downloaded pdf), in the SSID field the SSID of my WiFi network (in this case my_wifi_ssid), in the PASS field the password of my WiFi network (in this case my_wifi_password) and in the DEVICE_KEY field the value taken from the PDF under Secret Key.

Let’s now analyze the sketch.

We start, as usual, with the inclusion of the necessary library and the thingProperties.h file which contains the connection parameters to the WiFi network and the Arduino Cloud:

#include <MicroWakeupper.h>

#include "thingProperties.h"

The microWakeupper object that manages our MicroWakeupper, the analog input on pin A0 of the Wemos D1 mini and two variables (one integer and one String) that collect the value detected by the gas sensor are then defined:

//MicroWakeupper instance (only one is supported!)
MicroWakeupper microWakeupper;

#define ANALOGPIN0 A0  // ESP8266 Analog Pin ADC0 = A0

int analogGasSensorValue = 0;
String analogGasSensorValue_s = "";

We then meet the setup function.

Initially the serial port is initialized and the microWakeupper object is initialized:

Serial.begin(115200);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500); 

microWakeupper.begin();     // For correct initialisation

The next section is more of a debugging function:

if (microWakeupper.resetedBySwitch()) {
Serial.println("\n\n\nLooks like ESP was reseted by reset/switch");
} else {
Serial.println("\n\n\nLooks like ESP woke up by internal timer");
}

prints a message depending on whether the Wemos D1 mini has been “woken up” by an external signal or by an internal timer.

The rest of the setup function manages the part that interfaces with the Arduino Cloud:

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

At this point the loop function begins.

Initially we find the function that always maintains the connection with the Cloud and then the function that re-enables the microWakeupper object to capture a new trigger:

ArduinoCloud.update();

microWakeupper.reenable();                    // Re-enable MicroWakeupper for new triggering

The function ends with an if block:

if(microWakeupper.isActive()) {
    analogGasSensorValue = analogRead(ANALOGPIN0);
    gasVal = map(analogGasSensorValue, 0, 1023, 0, 100);
    String analogGasSensorValue_s = String(gasVal);
   //Serial.print("\nGas: " + analogGasSensorValue_s + "%"); 
} else {
  Serial.print("\nGoing to sleep...zzzzzzzz");    
  ESP.deepSleep(0);

}

delay(200);

If the signal at the IN input of the MicroWakeupper is HIGH it means that the gas percentage exceeds the threshold value. In this case, at the hardware level the Wemos D1 mini is awakened by the MicroWakeupper, at the software level the if argument is true and then the gas level reading detected by the sensor is made. However, if the signal at the IN input of the MicroWakeupper is LOW it means that the gas percentage is less than the threshold value and therefore the command is given to send the Wemos D1 mini into deep sleep (ESP.deepSleep(0);).

How to flash your device

To be able to flash the sketch on the Wemos D1 mini with the MicroWakeupper inserted on top, a precaution must be taken.

Look at this photo:

The MicroWakeupper commands
The MicroWakeupper commands

The top button marked in red is used to flash the sketch when the MicroWakeupper is inserted on top of the Wemos D1 mini. So, before giving PlatformIO the flashing command, hold down this button and release it only when the flashing process is finished.

We leave the switch at the bottom marked in green as it appears in the photo.

How to use the device

As soon as the device is connected to the PC’s USB connector, the red LED on the sensor lights up. Upload the sketch as already described. Rotate the gas sensor trimmer so that the green LED lights up. The turning on of the green LED indicates that the gas level threshold has been exceeded. On the Serial Monitor you will see the device connection messages to WiFi and then to the Arduino Cloud. In this case the device sends the measurements to the Cloud dashboard. If you then turn the trimmer in the opposite direction to turn off the green LED you will see the message indicating that the Wemos D1 mini is going into deep sleep. In this case the device falls asleep and therefore disconnects from the Cloud and WiFi. New data will no longer appear on the dashboard as it is no longer being sent.

If you then turn the trimmer again to make the green LED light up, you will see on the Serial Monitor the awakening of the Wemos D1 mini, the connection to the WiFi and the connection to the Cloud. The dashboard should start displaying new data again.

If everything went as described (therefore with the device connected, and therefore powered, to the PC) it means that the testing went well.

But we actually said that this device runs on battery power. Then disconnect the USB cable and connect everything to the battery holder as in the following image:

Complete wiring diagram with battery holder
Complete wiring diagram with battery holder

In this battery holder there are 4 1.5V batteries for a total of 6V (it doesn’t matter whether they are AA or AAA).

The behavior will obviously be the same as that described above except for the possibility of viewing the Serial Monitor messages since we are no longer connected to the PC. Then adjust the trimmer to adjust the intervention threshold. Under normal conditions the gas percentage should be less than the threshold and the Wemos D1 mini should be in deep sleep. In this case the Cloud dashboard should not record new values. If unfortunately the gas level were to exceed the threshold level, the green LED would light up, the Wemos D1 mini would wake up which, once the necessary connection operations to the WiFi and the Cloud were carried out, would send the measured values ​​to the Arduino Cloud dashboard which would be then updated.

Video of flashing and operation of the system connected to the PC

In this video you can see the device connected to the PC and the flashing phase of the sketch. After flashing you can see how it works. By varying the threshold (adjusted via the trimmer) you will see how the device goes into deep sleep or wakes up to send the measured data to the Arduino Cloud:

Video of the operation of the battery-powered system

Assuming that the device has already been flashed, the video shows its operation as the threshold varies (adjusted via the trimmer on the sensor). Obviously, since it is not connected to the PC and therefore to the Serial Monitor, it will not be possible to see the messages as in the previous case:

Newsletter

If you want to be informed about the release of new articles, subscribe to the newsletter. Before subscribing to the newsletter read the page Privacy Policy (UE)

If you want to unsubscribe from the newsletter, click on the link that you will find in the newsletter email.

Enter your name
Enter your email
Scroll to Top