Hello everyone,
First off, I am a software guy venturing into hardware design. Assume I made some newbie mistakes - I am here to learn.
I am working on a wavetable synthesizer with a capacitive touch keyboard. I first had a prototype with the main bits working with an Arduino Nano - I sweep the wavetable for the selected oscillator, modulate it with the envelope value, and output the notes over PWM. This output goes into the Adafruit PAM8302 breakout board to get amplified, and then that output is fed into a switching jack breakout board and then an 8Ohm 1W speaker. The entire thing worked. I also tested it with the Pololu A-star 328PB board.
I then went and had the PCBs made, built around the Atmega328PB, with the mono audio amplification circuit identical to the Adafruit PAM8302 board, and the layout very similar to sjm4306's touch piano. My synthesizer can run off 2 CR2032 batteries or off USB power. Here is the problem: if I plug in an externally powered speaker, things work fine just like the intial prototypes, but if I plug in headphones or let it run off of the on-board SMD 8Ohm 1W speaker, the MCU crashes when the volume is more than ~10%. Less than that and it is fine, but cross that threshold and I get random clicks and the MCU crashes.
I tried to plug a jack to pigtail cable and try my original speaker that works with the prototype: that didn't work.
I noticed that when it is battery powered, the voltage drops on the MCU momentarily from ~5V to around 2.6V, so I thought I need bigger decoupling capacitors (right now I have 1uF and 100nF on the MCU and 10uF on the PAM8302), but I don't see that voltage drop when running off of USB (not sure why) and I still have the same issue there.
I suspected EMI issues with my PCB, but then why would it run fine with an external powered speaker? I have ground planes, hatched around the capacitive sensors, and I didn't violate any DRC rules.
I now mainly suspect I am drawing too much current, but I don't know why that didn't happen in the original prototypes. The one difference I saw was that the Pololu board for instance has an LDO regulator onboard, so I had it running off of the power from a USB breakout board to skip its voltage regulator, and I still don't have issues there raising the volume.
Thank you all for the help.