Gas Sensor

This experiment aims to detect the presence of gas in an environment using a gas sensor connected to a Raspberry Pi Pico microcontroller. When gas is detected above a set threshold level, a buzzer is triggered, warning users of potential gas leakage. This setup is ideal for environments where gas detection is critical, such as laboratories or industrial settings, providing a reliable early-warning mechanism.



  1. Scope:

The project demonstrates how to use the analog output from a gas sensor with the Raspberry Pi Pico to monitor gas levels and trigger an alert. This experiment covers aspects of sensor integration, analog-to-digital conversion, signal processing, and output control via an alert mechanism (buzzer). The scope includes creating a functional system in both MicroPython and Embedded C.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Gas sensor

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.









  1. Connection Details:

Gas Sensor: Connect the analog output pin of the gas sensor to the ADC pin (e.g., ADC0 on GPIO26) on the Raspberry Pi Pico.

Buzzer: Connect the positive pin of the buzzer to a GPIO pin (e.g., GPIO15) on the Pico, and the negative pin to ground.

Power: Connect the Pico to a suitable power source and ground.

UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).



  1. Working Principle:

The gas sensor outputs an analog voltage proportional to the concentration of gas in the environment. This analog signal is read by the ADC on the Pico, which converts it into a digital value. When this digital value exceeds a predefined threshold (in this case, 50000), the program activates the buzzer, signaling the presence of gas above the safe limit.

Principle

The MQ-2 sensor works on the principle of resistance changes in the presence of different gases. When the target gas comes in contact with the heated MOS(Metal Oxide Semiconductor) material, it undergoes oxidation or reduction reactions that change the resistance of the MOS material. It is noteworthy that the MQ2 gas sensor is capable of detecting multiple gases, but lacks the ability to differentiate between them. This is a common characteristic of most gas sensors.

The sensor has a built-in potentiometer that allows you to adjust the sensor digital output (D0) threshold. When the concentration of gas in the air exceeds a certain threshold value, the resistance of the sensor changes. This change in resistance is then converted into an electrical signal that can be read by an Arduino board.

Calibrating the MQ2 Gas Sensor

Because the MQ2 is a heater-driven sensor, the calibration of the sensor may drift if it is left in storage for an extended period of time. When first used after a long period of storage (a month or more), the sensor must be fully warmed up for 24-48 hours to ensure maximum accuracy. If the sensor has recently been used, it will only take 5-10 minutes to fully warm up. During the warm-up period, the sensor typically reads high and gradually decreases until it stabilizes.

Specification

  • Model: MQ2

  • Supply Voltage: 5V

  • PCB Size: 32 x 20mm

  • Output Signal Type: DO and AO

  • Detection Concentration: 300 to 10000ppm

  • Preheat Duration: Over 24 hours (first time)

  • Detect Gas: LPG, Alcohol, Propane, Hydrogen, CO and even methane

Pinout

  • VCC: This is the positive power supply input from the main control.

  • GND: Ground connection.

  • DO: Digital output. It indicates the presence of combustible gases. When the gas concentration exceeds the threshold value (as set by the potentiometer), D0 becomes LOW; otherwise, it is HIGH.

  • AO: Analog output. It produces an analog output voltage proportional to gas concentration, so a higher concentration results in a higher voltage and a lower concentration results in a lower voltage.







  1. Application code:

  • MicroPython:

from machine import ADC, Pin

from time import sleep


# Define pins

GAS_SENSOR_PIN = 26

BUZZER_PIN = 15


# Set up ADC for gas sensor

gas_sensor = ADC(Pin(GAS_SENSOR_PIN))

buzzer = Pin(BUZZER_PIN, Pin.OUT)


# Gas threshold level for detection

GAS_THRESHOLD = 50000


while True:

gas_level = gas_sensor.read_u16() # Read gas sensor level

print("Gas Level:", gas_level)


if gas_level > GAS_THRESHOLD:

buzzer.value(1) # Turn ON buzzer if gas is detected

print("Gas detected! Buzzer ON.")

else:

buzzer.value(0) # Turn OFF buzzer otherwise


sleep(1) # Delay for 1 second


  • Embedded C:

#include <stdio.h>

#include "pico/stdlib.h"

#include "hardware/adc.h"


#define GAS_SENSOR_PIN 26 // ADC0 (GPIO26)

#define BUZZER_PIN 15 // GPIO15

#define GAS_THRESHOLD 50000 // Threshold for gas detection


int main() {

stdio_init_all();

adc_init();

adc_gpio_init(GAS_SENSOR_PIN);

adc_select_input(0); // Select ADC0 for gas sensor


gpio_init(BUZZER_PIN);

gpio_set_dir(BUZZER_PIN, GPIO_OUT);


while (1) {

uint16_t gas_level = adc_read(); // Read gas sensor level

printf("Gas Level: %d\n", gas_level);


if (gas_level > GAS_THRESHOLD) {

gpio_put(BUZZER_PIN, 1); // Turn ON buzzer if gas is detected

printf("Gas detected! Buzzer ON.\n");

} else {

gpio_put(BUZZER_PIN, 0); // Turn OFF buzzer otherwise

}


sleep_ms(1000); // Delay for 1 second

}

}



  1. Hardware Image:



  1. Output:

Console Output: The console will print the gas level in both MicroPython and Embedded C, and indicate if the gas is detected.

Buzzer: The buzzer will turn on when the gas level exceeds the threshold, providing an audible warning.



  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)


project(gas_detection C CXX ASM)

set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


pico_sdk_init()


add_executable(gas_detection

main.c

)


target_link_libraries(gas_detection pico_stdlib hardware_adc)

pico_enable_stdio_usb(gas_detection 1)

pico_enable_stdio_uart(gas_detection 0)


pico_add_extra_outputs(gas_detection)

  1. References:



Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)