How to use the MQ2 sensor with Microbit board | Gas Level monitoring system with MicroBit
An MQ2 sensor detects gas levels and sends analog data to a Microbit. The Microbit processes this, displaying the level (e.g., on an OLED screen) and triggering a buzzer if levels are too high. Simple wiring and Microbit programming are required.
SCIENCE PROJECTS


This project uses a Micro:bit to build a gas level monitoring system with an MQ2 gas sensor, buzzer, and OLED display. The Micro:bit reads the analog output from the MQ2 sensor (connected to pin P2). If the reading (representing gas concentration) exceeds a threshold (250), the buzzer (connected to P5) activates, a warning message appears on the OLED display, and a warning icon is shown on the Micro:bit's LEDs. Otherwise, the alert is cleared. The provided Python code handles sensor reading, display updates, and alert logic. The OLED display shows the gas level reading and alerts. The system's effectiveness depends on the MQ2 sensor's calibration and the chosen threshold. More sophisticated gas identification might require additional sensors or signal processing.
OK, let's do this project step-by-step. The required components are given below:
Microbit board
MQ2 sensor x 1
Buzzer x 1
OLED display x 1
Microbit sensor shield
Jumper wires


Python script
value = 0 OLED12864_I2C.init(60) OLED12864_I2C.show_string(2, 0, "GAS Level", 1) OLED12864_I2C.show_string(1, 1, "Monitoring", 1) def on_forever(): global value value = pins.analog_read_pin(AnalogReadWritePin.P2) OLED12864_I2C.show_string(5, 2, "" + str(value) + " ", 1) if value >= 250: pins.digital_write_pin(DigitalPin.P5, 1) OLED12864_I2C.show_string(3, 3, "Warning!", 1) basic.show_leds(""" . . # . . . . # . . . . # . . . . . . . . . # . . """) else: pins.digital_write_pin(DigitalPin.P5, 0) OLED12864_I2C.show_string(3, 3, " ", 1) basic.clear_screen() basic.forever(on_forever)