Using the Hat SPK2 ・ 使用 Hat SPK2
M5StackのHat SPK2をコントローラーに接続する最小構成。配線とBOMつき。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| AtomS3 Lite | Controller | grove_i2c, gpio, usb_c | official page — $7.5 |
| Hat SPK2 | hat | i2s, gpio | official page — $4.95 |
M5Stack AtomS3 LiteとHat SPK2で作る、かんたんなスピーカーハットです。AtomS3 LiteにHat SPK2を直接差し込むだけで、I2Sオーディオを再生できるようになります。音楽や効果音を鳴らしたいときに便利です。
# M5Stack AtomS3 Lite + Hat SPK2 (I2S speaker)
# Wiring: Hat SPK2 directly attached to AtomS3 Lite via 8-pin connector
# Uses I2S on GPIO 33 (BCLK), 34 (LRC), 35 (DIN) - typical for AtomS3 Lite
import time
from machine import Pin, I2S
# I2S pins for AtomS3 Lite (check your board's pinout)
bclk = Pin(33)
lrc = Pin(34)
dout = Pin(35)
# Create I2S object
i2s = I2S(0,
sck=bclk,
ws=lrc,
sd=dout,
mode=I2S.TX,
bits=16,
format=I2S.MONO,
rate=44100,
ibuf=4096)
# Generate a simple sine wave tone (440 Hz) for 2 seconds
import math
sample_rate = 44100
freq = 440
duration = 2 # seconds
num_samples = sample_rate * duration
samples = bytearray(num_samples * 2) # 16-bit samples
for i in range(num_samples):
value = int(32767 * math.sin(2 * math.pi * freq * i / sample_rate))
samples[i*2] = value & 0xff
samples[i*2+1] = (value >> 8) & 0xff
# Play the tone
i2s.write(samples)
# Clean up
i2s.deinit()
print("Playback finished")
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。