Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsAn MQ-2 gas sensor changes its analogue output as LPG or smoke increases. The Pico reads it five times a second and prints the value. After a 30-second warm-up, anything above the limit starts the buzzer and LED and latches them on, so a short puff cannot hide a real problem. Reset the Pico after checking the room.
A learning project, not a certified gas detector. If you smell gas, close the regulator, open windows and do not touch switches.
This is a learning project, not a certified gas or smoke alarm. Keep a certified alarm installed.
Tick anything you already have and the total updates.
Heated tin-oxide sensor that responds to LPG, smoke, butane and hydrogen.
Active buzzer with a built-in oscillator: apply power and it beeps at about 2.3 kHz.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico gas and smoke alarm with a warm-up and a latch (MicroPython)
from machine import Pin, ADC
import time
sensor = ADC(26) # MQ-2 analogue output on GP26
buzzer = Pin(15, Pin.OUT)
led = Pin(14, Pin.OUT)
LIMIT = 40000 # raise or lower after checking clean-air readings
WARMUP_S = 30
print("Warming up the sensor for", WARMUP_S, "seconds")
for i in range(WARMUP_S):
led.toggle(); time.sleep(1)
led.value(0)
alarm = False
while True:
v = sensor.read_u16()
if v > LIMIT:
alarm = True # stays on until the Pico is reset: you must check the room
buzzer.value(1 if alarm and (time.ticks_ms() // 300) % 2 else 0)
led.value(1 if alarm else 0)
print(v, "ALARM" if alarm else "ok")
time.sleep_ms(200)
Normal: the heater needs a few minutes to settle. Wait before calibrating the threshold.
Low-triggered module: invert the logic in setBuzzer().
LED is reversed; flip it. Check the 220 Ω resistor is in series.
7 parts: MQ-2 Smoke / LPG Gas Sensor Module – Analog/Digital Output, Active Buzzer Module 3.3–5V, Through-Hole LED Pack (Pack of 10), Resistor Assortment Kit (30 Values), Raspberry Pi Pico, Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 45 minutes. The build on this page has 4 steps.
No. This page has the complete, tested code (main.py).
About ₹634 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Active vs passive buzzers; Analog sensors, ADCs and warm-up drift; Current-limiting resistors for LEDs.
A learning project, not a certified gas detector. If you smell gas, close the regulator, open windows and do not touch switches. This is a learning project, not a certified gas or smoke alarm. Keep a certified alarm installed.
Ideas that reuse this build: Gas leak voice alarm and Gas level ring. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.