Water tank level monitor ・ 水箱水位监测
超音波距離センサで貯水タンクの水位を監視します。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| M5Stack Core2 | Controller | grove_i2c, grove_analog, grove_uart, mbus | official page — $46.9 |
| Unit Ultrasonic-IO (UNIT SONIC IO) | Ultrasonic distance sensor | grove_analog | official page — $5.95 |
M5Stack Core2と超音波距離センサー「Unit Ultrasonic-IO」を使って、貯水タンクの水位を監視する装置です。センサーが水面までの距離を測り、Core2の画面に水位を表示します。水が少なくなるとアラートを出すこともできます。
# M5Stack Core2 + Unit Ultrasonic-IO (water tank level monitor)
# Wiring: Unit Ultrasonic-IO -> Core2 black Port.B (analog/GPIO)
import time
from machine import Pin, ADC
from m5stack import lcd
# Port.B pins on Core2: GPIO 36 (analog input) and GPIO 26 (digital output)
# For Unit Ultrasonic-IO, we use GPIO 26 to trigger and GPIO 36 to read echo.
# However, the unit uses a single-wire protocol (trigger + echo on same pin).
# We'll use GPIO 26 as both trigger and echo (open-drain with pull-up).
# Actually, the unit is connected to Port.B, which has two pins: 36 (ADC) and 26 (GPIO).
# The unit uses a single signal line (SIG) which we connect to GPIO 26.
# We'll use GPIO 26 for both trigger and echo.
TRIG = Pin(26, Pin.OUT)
ECHO = Pin(26, Pin.IN)
def get_distance():
# Send 10us trigger pulse
TRIG.value(0)
time.sleep_us(2)
TRIG.value(1)
time.sleep_us(10)
TRIG.value(0)
# Wait for echo start (timeout 1s)
timeout = time.ticks_us() + 1000000
while ECHO.value() == 0:
if time.ticks_us() > timeout:
return -1
start = time.ticks_us()
# Wait for echo end
timeout = time.ticks_us() + 1000000
while ECHO.value() == 1:
if time.ticks_us() > timeout:
return -1
end = time.ticks_us()
# Calculate distance (speed of sound = 34300 cm/s)
duration = end - start
distance = duration * 34300 / 2 / 1000000 # cm
return distance
lcd.clear()
lcd.print("Water Level Monitor", 10, 10)
while True:
dist = get_distance()
if dist < 0:
lcd.print("Error: no echo", 10, 50)
else:
lcd.print("Distance: %.1f cm" % dist, 10, 50)
# Assume tank depth is known, e.g., 100 cm
tank_depth = 100 # change to your tank depth
water_level = tank_depth - dist
if water_level < 0:
water_level = 0
lcd.print("Water level: %.1f cm" % water_level, 10, 80)
if water_level < 20:
lcd.print("WARNING: Low water!", 10, 110)
time.sleep(1)
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。