LPG gas leak alarm
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
Shop by category
Shop all componentsThe Raspberry Pi Pico's RP2040 has two cores. MicroPython runs your program on core 0 and the `_thread` module lets you start a second function on core 1. Here core 1 reads the slow DHT22 every two seconds and stores the result in shared variables protected by a lock, while core 0 keeps a smooth animation going on the OLED. The aim is to see why a blocking sleep in one loop stalls everything, and how a second core avoids it.
Tick anything you already have and the total updates.
A slow sensor to demonstrate the problem
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB cable.
main.pyimport time, _thread, dht
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
sensor = dht.DHT22(Pin(15))
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
lock = _thread.allocate_lock()
latest = {"t": 0.0, "h": 0.0}
def sensor_core(): # runs on core 1
while True:
try:
sensor.measure()
with lock:
latest["t"], latest["h"] = sensor.temperature(), sensor.humidity()
except OSError:
pass
time.sleep(2) # sleeping here does not stall the display
_thread.start_new_thread(sensor_core, ())
x = 0
while True: # runs on core 0
with lock:
t, h = latest["t"], latest["h"]
oled.fill(0)
oled.text("T %.1f C" % t, 0, 0)
oled.text("H %.0f %%" % h, 0, 12)
oled.fill_rect(0, 40, x, 8, 1)
oled.show()
x = (x + 4) % 128
time.sleep_ms(20)
Do not touch the same I2C bus from both cores. Here only core 0 uses the OLED.
The first read takes about two seconds. Check the DHT22 wiring if it never changes.
Remove any blocking sleep from the main loop; keep it under 20 ms.
A blocking task on one core no longer slows the other.
It works on the RP2040 but is marked experimental; keep the shared code small.
No. The DHT22 is slow by design. The second core just hides the wait.
5 parts: Raspberry Pi Pico, DHT22 AM2302 Digital Temperature & Humidity Sensor, 0.96" OLED Display 128x64, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a advanced project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (main.py).
About ₹734 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Starting a function on the second core with _thread; Sharing data safely with a lock; Why sleeping blocks an animation.
Ideas that reuse this build: ESP32 FreeRTOS two tasks. They are listed under Take it further on this page.
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
See the smoke level live on a crisp screen and get a loud warning when it spikes.
An infrared eye that spots an open flame and raises the alarm.
Walk into the room and it sounds the alarm: the classic first security build.
Learning project. Check every circuit and the datasheet of each part before you power it up.