In this DIY guide, we will build a gas sensor that can detect harmful gases, such as methane, propane, LPG, and carbon monoxide. It also sends you alerts to your smartphone or Echo speakers.
Things You Will Need
You will need the following components to build a DIY smart wireless gas leak detector.
An MQ-2 Gas Sensor. Wemos D1 Mini or NodeMCU board. Jumper wires. A 3D printed or any case assemble the components.
You will also need a Home Assistant server setup on a Raspberry Pi or any compatible hardware. If interested, you can learn everything about Home Assistant and see how it works before getting started.
Step 1: Compile the Firmware
To compile the firmware, install the ESPHome add-on in Home Assistant. Once installed, follow these steps to compile the firmware for D1 Mini or NodeMCU.
Open ESPHome and click +New Device. Type the device’s name, such as gas-leakage, and click Next. Select Pick specific board > Wemos D1 Mini from the options and click Next > Skip. If you are using NodeMCU, choose ESP8266. Click Edit. In the gas-leakage. YAML editor window, make the following changes:
Update the Wi-Fi credentials:
Then paste the following code just below the captive portal:
You can update the name: field in the code as per your convenience. The code should look like the screenshot below.
Once the code is ready, you can click Save > Install. Select Plug into this computer from the options. This will start compiling the firmware and may take roughly 10 minutes to finish.
Once finished, click Download project to download the compiled gas-leakage-factory.bin firmware file.
Step 2: Flash the Firmware
To flash the firmware, you can use the web-based flash tool. The steps are as follows:
Once the firmware is downloaded, click Open ESPHome web. This opens a new window. Click the Connect option, choose the COM port, and then click Connect. Make sure you have installed the CH240/CH341 or compatible drivers. Go to Install > Choose File and select the gas-leakage-factory. bin firmware file. Click Install. This will flash the firmware to your connected D1 Mini or NodeMCU board.
After the firmware is flashed, go to the ESPHome dashboard in Home Assistant and check the gas-leakage device. It should show ONLINE.
You may also download and use the ESPHome Flasher tool to flash the gas-leakage-factory.bin firmware file to your NodeMCU or D1 Mini board.
Step 3: Add Gas Leakage Sensor to Home Assistant Dashboard
Follow these instructions to add the sensor to the Home Assistant dashboard:
In the Home Assistant (HA), navigate to Settings > Devices & Integrations. The device should be visible with CONFIGURE button. Click CONFIGURE > SUBMIT. This will add the device to the ESPHome devices list. After adding the device, click on gas-leakage listed under ESPHome and then click 1 device. Click ADD TO DASHBOARD> select the View> ADD TO DASHBOARD.
That’s it. The device will be added to the selected Home Assistant dashboard. Next, you can connect the MQ-2 gas sensor to the microcontroller.
Step 4: Connect the MQ-2 to Microcontroller
Refer to the following diagram to connect the MQ-2 gas sensor to the D1 Mini or NodeMCU microcontroller board.
The connections are as follows:
Connect the VCC pin of MQ-2 to the Vin or 5V/3. 3V pin of the D1 Mini or NodeMCU board. Connect the GND pin of the MQ-2 sensor to the G pin on the D1 Mini or NodeMCU. Connect the AOUT pin of the MQ-2 sensor to the A0 pin of the NodeMCU or D1 Mini.
Once connected, you can check the updated reading in the Home Assistant dashboard. You can test the sensor by exposing it to smoke or gas. Be careful! Wear a mask and do not smell the gas or smoke. The percent value should increase in the Home Assistant dashboard.
Calibrate the Gas Sensor
While testing the sensor near gas or smoke you want to detect, you should also adjust or calibrate the sensor. Using a small screwdriver, you can rotate the preset on the MQ-2 sensor anticlockwise to decrease sensitivity or clockwise to increase sensitivity.
Put the sensor near the smoke or gas and look for the other LED to glow or check the sensor value in the Home Assistant dashboard and adjust the sensitivity according.
For example, the sensor we built normally shows 10-15%. When we exposed it to a little amount of LPG (methane) gas, the value rose to 35%+. This was the sweet spot for us, after which we installed it in our kitchen, close to the chimney.
Create Automation to Send Gas Leak Alerts
Once you have calibrated and adjusted the sensor sensitivity, you can create automation in the Home Assistant to receive notifications on your smartphone or voice alerts through Alexa smart speaker or Google Assistant.
To create an automation, go to Settings > Automations & Scenes and click Create Automation. Choose Start with empty automation and then click the three dots at the top left. Now, choose Edit in YAML and paste the following code.
Next, click the three dots again and choose Edit in visual editor. In the visual editor window, choose and change your Gas Leak sensor and the devices where you want to send notifications.
For instance, the above-shared automation code sends a notification alert to our Android or iOS device and also announces the voice alert via Alexa speakers (Echo Dot and Echo Show devices installed in the house) when the gas level exceeds the 30% threshold. Usually, the sensor remains at 10-15%.
Prevent Hazards and Save Money
It’s important to keep the main regulator switch off while the gas stove is not in use, especially when you go to sleep. Thankfully, a gas sensor, such as the one we built, can help you prevent hazards due to leakage of gas and safeguard your home and family. Plus, it will save you from inflated bills that may end up at your door due to gas leakage.