ESP32 Arduino Interfacing with Gas sensor diagram,working,code

This app note covers ESP32 Arduino Interfacing with Gas sensor diagram and its working. It describes Gas sensor used and mentions ESP32 arduino code.

About Gas sensor

We have seen smoke detectors in home and office ceilings designed to detect fire or smoke. The gas sensor model MQ-02 is capable to detect various kinds of gases such as H2, alcohol, LPG, CH4 and smoke. It is very quick in responding to smoke. The figure depicts MQ-02 version of gas sensor.

Gas Sensor MQ-02

It generates voltage when gas enters its coil area. The voltage depends on gas concentration i.e. higher the gas concentration higher is the output voltage. This voltage is read by arduino or ESP32 board based microcontroller or MCU. Based on voltage measurement, appropriate action is taken as per programming.

Gas sensor (MQ-02) ESP32 board or arduino board
Supply Vcc (+5V) on Arduino
or 3V3 on ESP32 board
Ground GND
Analog output A0 pin (analog input)
Digital output Digital input of arduino or ESP32

The table mentions pin layout of MQ-02 board and its interfacing with arduino or ESP32 board. Refer advantages and disadvantages of Gas sensor types >>.

About ESP32 board

ESP32 pin out

ESP32 board houses 32 bit microprocessor. It has ROM and SRAM. It supports both wifi and bluetooth low energy specifications. One can use either arduino, ESP32 or ESP8266 to interface gas sensor.

ESP32 architecture

Following are the features of ESP32 board :
• CPU : Xtensa® single/dual core 32 bit LX6 microprocessor
• Memory: 448 KB ROM, 520 KB SRAM
• Clocks : Internal 8 MHz oscillator, Internal RC oscillator, External 2 MHz to 60 MHz crystal oscillator etc.
• Timers : Two timer groups, One RTC timer and RTC watchdog.
• Peripheral interfaces: 34 GPIOs, 4 SPIs, 2 I2S, 2 I2C, 3 UART, CAN 2.0, 1 host (SD/eMMC/SDIO), 1 slave (SDIO/SPI).
• ADC : 12 bit SAR ADC upto 18 channels.
• DAC : 2 x 8 bit DAC
• Sensors : 10 x touch sensors , Hall sensor
• Infrared: IR (Tx/Rx)
• PWM: motor PWM, LED PWM upto 16 channels
• Security : 1024-bit OTP upto 768 bit for customers

ESP32 Interfacing with Gas sensor diagram and its working

Arduino or ESP32 Interfacing with Gas Sensor

As shown in the figure-1, three pins of Gas sensor are connected with ESP32 board with details provided in the table above.

Arduino or ESP32 and Gas sensor interface code

Following is the ESP32 arduino code compiled and uploaded to the arduino board using Arduino IDE. Code snippet is used to upload the readings of the gas sensor to the cloud server such as adafruit. As mentioned interface A0 of Arduino or ESP32 is connected with analog output of MQ-02.

In the code, buzzer is connected with arduino pin number "10" and gas sensor is connected with pin number "A5" of arduino. The same code can be modified and compiled for ESP32 board porting. When smoke threshold reaches above threshold value set, buzzer can be switched ON.

Arduino Uno code

int buzzer = 10;
int smokeA0 = A5;
int sensorThreshold = 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 > sensorThreshold)
{
tone(buzzer, 1000, 200);
}
else
{
noTone(buzzer);
}
delay(100);
}

ESP32 code

#include <ESP32WiFi.h>
#include <EEPROM.h>
#include

int val;
#define wifi_name "Your SSID"
#define password "Your Wifi Password"
#define server "io.adafruit.com"
#define port 1883
#define username "Your Adafruit User ID"
#define aio key "Your AIO Key"
#define USE_SERIAL Serial
#define GAS_SENSOR A0
//A0 of NodeMCU is connected to Analog pin of MQ-02
void setup( )
{
Serial.begin(115200);
delay(10);
}

void loop()
{
val=analogRead(GAS_SENSOR):
// Analog read for reading any analog input.
Serial.print (val);
// Here, we print the value read through the serial monitor.
Serial.println (" ");
delay(100);
}

WiFiClient me;
Adafruit MQTT Client mqtt (&me, server, port, username, aiokey);
Adafruit MQTT_Publishgas_data = Adafruit_MQTT_Publish (&mqtt, username"/feeds/Gas sensor");
void setup()
{
Serial.begin (115200);
delay(10);
Serial.println("Connecting to");
Serial.print(wifi_name);
WiFi.begin (wifi_name, password);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

ESP32 code for uploading gas sensor data into the cloud

void loop()
{
if(mqtt.connected())
{
int Gas_value = analogRead(A0);
Serial.println(Gas_value);
Serial.print ("...") ;
if (Gas_data.publish(Gas_value))
{
Serial.println ("Success");
}
else
{
Serial.print ("Trying");
}
}
delay(2000);
}

Conclusion: In this application note we have seen gas sensor interfacing with ESP32 board. ESP32 board is also used for interfacing different types of sensors for applications. The typical sensors interfaced with arduino are sound sensor, gyro sensor, LDR sensor, GPS sensor, color sensor, heartbeat sensor, pH sensor etc.

IoT system on chip tutorial Related Links

This tutorial section on IoT (Internet of Things) covers following sub topics:
Main tutorial  IoT section  IoT article  Cellular IoT  Components  Sensors  INDIAN companies  Antenna Types  Wireless technologies  IoT transceiver  SoC 

RF and Wireless Terminologies