Need some help making a button that plays sound

Thread Starter

fennnnnn

Joined Jun 24, 2026
3
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,972
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.
 

Thread Starter

fennnnnn

Joined Jun 24, 2026
3
I guess it's python? Not sure what the blurry item is - an amp?

Maybe you have a diagram?
Yes it is miropy. The blurry thing is part of the amp I have two pictures of it one of the front and then the back. I do have a diagram. This is all of the parts and wiring for each component
 

Attachments

Your wiring appears to be

  • GP16 -->DIN on the PCM5102
  • GP17 -->BCK (BCLK)
  • GP18 --> SCK/LRCK (WS)
  • GP19 --> Button
  • GP13 --> Other side of button
  • PCM5102 analog output --> PAM8403 --> Speaker

The biggest issue I see is that your code and your wiring don't match.


Your code says:
Code:
sck = Pin(10)
ws  = Pin(11)
sd  = Pin(12)
But your drawing shows:
GP18 --> SCK/LRCK
GP17 --> BCK
GP16--> DIN
Those are completely different GPIOs.
 
Top