So I'm trying to make a button play sound and I can get an LED to light up for the button. I've also included photos of the wiring setup just in case I have something wrong there.
This is the code I am using (I did use AI to create this so I wouldn't be surprised if there's a mistake):
import time
from machine import Pin, I2S
button = Pin(19, Pin.IN, Pin.PULL_UP)
audio = I2S(
0,
sck=Pin(10),
ws=Pin(11),
sd=Pin(12),
mode=I2S.TX,
bits=16,
format=I2S.STEREO,
rate=44100,
ibuf=4096
)
print("System Ready! Press the button to play sound.")
while True:
print("Button state:", button.value())
if button.value() == 0:
print("Playing...")
try:
with open("sound.wav", "rb") as wav:
wav.seek(44)
buf = bytearray(2048)
while True:
n = wav.readinto(buf)
if n == 0:
break
audio.write(buf[:n])
print("Done.")
except OSError as e:
print("File error:", e)
time.sleep(0.3)
time.sleep(0.01)
This is the code I am using (I did use AI to create this so I wouldn't be surprised if there's a mistake):
import time
from machine import Pin, I2S
button = Pin(19, Pin.IN, Pin.PULL_UP)
audio = I2S(
0,
sck=Pin(10),
ws=Pin(11),
sd=Pin(12),
mode=I2S.TX,
bits=16,
format=I2S.STEREO,
rate=44100,
ibuf=4096
)
print("System Ready! Press the button to play sound.")
while True:
print("Button state:", button.value())
if button.value() == 0:
print("Playing...")
try:
with open("sound.wav", "rb") as wav:
wav.seek(44)
buf = bytearray(2048)
while True:
n = wav.readinto(buf)
if n == 0:
break
audio.write(buf[:n])
print("Done.")
except OSError as e:
print("File error:", e)
time.sleep(0.3)
time.sleep(0.01)
Attachments
-
282.8 KB Views: 8
-
256.2 KB Views: 9
-
214.9 KB Views: 8
-
287.9 KB Views: 8
-
264.9 KB Views: 7