Using the Unit ByteButton ・ 使用 Unit ByteButton
M5StackのUnit ByteButtonをコントローラーに接続する最小構成。配線とBOMつき。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| AtomS3 Lite | Controller | grove_i2c, gpio, usb_c | official page — $7.5 |
| Unit ByteButton | unit | grove_i2c | official page — $7.5 |
AtomS3 LiteとUnit ByteButtonをGrove I2Cケーブルで接続する、最小構成のボタン入力装置です。8つのタクトスイッチと9つのRGB LEDを備えたByteButtonを、AtomS3 LiteからI2Cで制御します。
# M5Stack AtomS3 Lite + Unit ByteButton
# Wiring: ByteButton -> AtomS3 Lite Grove Port (Port.A, I2C)
# I2C address of ByteButton: 0x62
import time
from machine import Pin, I2C
# Initialize I2C on AtomS3 Lite (Grove port: SDA=GPIO1, SCL=GPIO2)
i2c = I2C(0, scl=Pin(2), sda=Pin(1), freq=100000)
# ByteButton I2C address
ADDR = 0x62
# Register addresses (from datasheet)
REG_BUTTON = 0x00 # read button states (8 bits)
REG_LED = 0x10 # write LED states (9 bits, but we use first 8)
def read_buttons():
data = i2c.readfrom(ADDR, 1)
return data[0]
def set_leds(led_mask):
# led_mask: 8-bit mask, bit0 = LED0 (button0), etc.
i2c.writeto(ADDR, bytes([REG_LED, led_mask]))
# Turn off all LEDs initially
set_leds(0x00)
while True:
buttons = read_buttons()
# buttons: bit0 = button0 pressed (active low? check datasheet)
# Assuming active low: 0 = pressed
pressed = (~buttons) & 0xFF
if pressed:
# Light up the pressed button's LED
set_leds(pressed)
# Print pressed buttons
for i in range(8):
if pressed & (1 << i):
print('Button %d pressed' % i)
else:
# No button pressed, turn off LEDs
set_leds(0x00)
time.sleep(0.05)
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。