Carbon monoxide monitor ・ 一氧化碳监测
MQ-5ガスセンサーユニットで一酸化炭素濃度を監視し、危険なレベルになるとブザーで警報を鳴らします。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| M5Stack Core2 | Controller | grove_i2c, grove_analog, grove_uart, mbus | official page — $46.9 |
| Unit PaHub2 | I2C Hub (1→6) | grove_i2c | official page — $7.95 |
| Unit MQ | CO / Combustible Gas Sensor | grove_i2c | official page — $5.5 |
| Unit Buzzer | Buzzer Alarm | grove_analog | official page — $4.5 |
M5Stack Core2とMQ-5ガスセンサーユニット(Unit MQ)で作る、一酸化炭素(CO)濃度監視アラームです。Unit MQがCO濃度を検出し、危険なレベルに達するとブザーが鳴ります。PaHub2を使って複数のI2Cユニットを接続します。
# M5Stack Core2 + PaHub2 + Unit MQ (CO sensor) + Unit Buzzer
# Wiring:
# PaHub2 -> Core2 red Port.A (I2C)
# Unit MQ -> PaHub2 (any port)
# Unit Buzzer -> Core2 black Port.B (analog/GPIO)
import time
from machine import Pin, PWM, I2C
from m5stack import lcd
# I2C setup for PaHub2 (address 0x70)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
# Unit MQ I2C address (default 0x12)
MQ_ADDR = 0x12
# Buzzer on Port.B: GPIO 36 (analog input) but we use PWM on GPIO 26? Actually Port.B on Core2 is GPIO 36 (ADC) and GPIO 26 (DAC).
# For buzzer, we need a PWM-capable pin. Core2 Port.B: black connector: pin 36 (ADC) and pin 26 (DAC). DAC can output analog, but buzzer expects PWM.
# Actually, Port.B on Core2 uses GPIO 36 (ADC) and GPIO 26 (DAC). The DAC can be used as PWM? No, DAC is analog output. For PWM, we need a digital pin.
# Core2 has PWM on many pins, but Port.B is not ideal. Let's use Port.C (blue) which has GPIO 13 and 14, or use internal pin.
# For simplicity, we'll use GPIO 2 (internal LED) as buzzer output? No, better to use a free pin. Let's assume we use Port.C (blue) for buzzer.
# But the wiring says grove_analog (Port.B). Actually, Unit Buzzer is driven by PWM, and Port.B has GPIO 26 which can be used as PWM.
# Let's use GPIO 26 for PWM.
buzzer = PWM(Pin(26), freq=4000, duty=512) # 4kHz, 50% duty
buzzer.deinit() # start off
CO_THRESHOLD = 500 # adjust after calibration
def read_co():
# Read CO concentration from Unit MQ via I2C
# Unit MQ returns 2 bytes: high byte, low byte
data = i2c.readfrom(MQ_ADDR, 2)
value = (data[0] << 8) | data[1]
return value
while True:
co_value = read_co()
lcd.clear()
lcd.print('CO: %d' % co_value, 10, 20)
if co_value > CO_THRESHOLD:
lcd.print('DANGER!', 10, 60)
buzzer = PWM(Pin(26), freq=4000, duty=512)
time.sleep(0.5)
buzzer.deinit()
time.sleep(0.5)
else:
buzzer.deinit()
time.sleep(1)
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。