Need some help making a button that plays sound

Thread Starter

fennnnnn

Joined Jun 24, 2026
1
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)
 

Attachments

MrChips

Joined Oct 2, 2009
34,887
Your photos show 3 or 4 different devices.
You should stay focused and tackle one problem at a time. This makes the code simpler and easier to diagnose.
Start with the rPi Pico and make an LED flash.

Forget about asking AI to write code for you.
 
Top