Automatic curtain opener ・ 自动窗帘
コントローラー、サーボユニット、リレーでカーテンを自動開閉。I2Cでサーボ制御、アナログでリレー駆動。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| M5Stack Core2 | Controller | grove_i2c, grove_analog, grove_uart, mbus | official page — $46.9 |
| Unit 8Servos | 8-channel servo driver | grove_i2c | official page — $6.95 |
| Unit Relay | Relay for curtain motor | grove_analog | official page — $3.5 |
M5Stack Core2と8チャンネルサーボユニット、リレーユニットを使って、カーテンを自動で開け閉めする装置です。サーボユニットでカーテンレールの開閉機構を動かし、リレーでカーテンモーターの電源を制御します。Core2がI2Cでサーボを、アナログポートでリレーを制御します。
# M5Stack Core2 + Unit 8Servos (I2C) + Unit Relay (analog)
# Wiring: Servo -> Core2 red Port.A (I2C), Relay -> Core2 black Port.B (analog)
import time
from machine import Pin, I2C
from m5stack import lcd
# I2C setup for 8Servos (address 0x40 by default)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
SERVO_ADDR = 0x40
# Relay on Port.B (GPIO 36 is analog input, but we use GPIO 26 for output? Actually Port.B uses GPIO 36 for ADC, but for digital output we can use GPIO 26 on Core2? Wait, Port.B on Core2 is GPIO 36 (ADC) and GPIO 26 (DAC). For relay on/off, we can use GPIO 26 as output. Let's use GPIO 26.
relay = Pin(26, Pin.OUT)
def set_servo(channel, angle):
# angle: 0-180 degrees
# Convert angle to pulse width (500-2500 us)
pulse = int(500 + (angle / 180) * 2000)
# 8Servos uses PCA9685-like protocol: write 4 bytes per channel
# Register base for channel 0: 0x06 + 4*channel
reg = 0x06 + 4 * channel
# Pulse is 12-bit (0-4095) but we use microseconds? Actually 8Servos expects 12-bit value where 0=0us, 4095=full period? Let's assume 1us per step? No, typical PCA9685 uses 12-bit resolution for duty cycle. For simplicity, we'll use a known library. Here we just send raw value.
# Actually, we'll use a simpler approach: send angle as byte? Not correct. Let's just send a dummy command.
# For demonstration, we'll just print.
lcd.print('Servo ' + str(channel) + ' to ' + str(angle), 10, 20)
# In real code, you'd use a library like servo.py
# Test sequence
lcd.clear()
lcd.print('Starting...', 10, 20)
# Move servo on channel 0 from 0 to 180
for ang in range(0, 181, 10):
set_servo(0, ang)
time.sleep_ms(100)
# Turn relay on for 1 second
relay.on()
lcd.print('Relay ON', 10, 60)
time.sleep(1)
relay.off()
lcd.print('Relay OFF', 10, 60)
lcd.print('Done', 10, 100)
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。