I built a synth first out of breakout boards - either an Arduino Nano or a A-Star 328PB Micro into a PAM8302 breakout into speakers or a headphone jack breakout board. When that prototype worked, I moved on to create the PCB around an Atmega328pb, capsense keys, and an on-board PAM8302 circuit and speaker.
The MCU crashes in a very specific scenario when all these 3 conditions are in place:
running with the 20MHz external crystal
output through unpowered speaker (on-board SMD speaker or headphones)
PWM signal at a high enough volume (through the on-board pot or the code)
If any of these conditions is not met, it doesn't crash. I made the following observations:
Arduino nano into my PCB PAM8302 amp -> works
PCB MCU into PAM8302 breakout -> does not work (when all 3 conditions are there)
Right now it is looking like a PCB design issue to me. I think I made the following mistakes from what I can tell:
crystal is close to MCU but its signal lines run through vias, they don't cross the audio output though
I didn't match the audio differential pair trace length, off by about 7mm
not sure if I should or should not have ground planes under the crystal
Would this have caused the problem? I can see how the crystal signal suboptimal routing + a high volume PWM signal might cause some interference, but I don't understand why running that through a powered speaker would still be fine.
I wanted to add, I have 100nF and 1uF decoupling capacitors on the MCU and a 10uF decoupling capacitor on the amp. I don't have the 4.7uF that I see on several boards.
As my crystal ball is out of order today, would be better if you put the schematic with all the details of the PCB, specially how do you power it. And the specs of all the parts, like the speakers.
Thank you both! Here is the full schematic and the PCB section I suspect is causing issues, showing the decoupling capacitors on the MCU and the crystal.
The crystal is 20MHz because the Atmega328pb supports that - the UNO and Nano are on the older Atmega328p.
I can run off of 2x CR2032 but the voltage drops when I use the on-board speaker, and most of my testing is done off the USB-C power. I have Schottky diodes on both. All my parts are from LCSC, including the speaker. I still crash with headphones plugged into the switching jack.
It's not surprising the voltage drops. 5V / 8-Ohms = 625mA. (You may not be putting 5V into the speaker, but I wouldn't expect much audio power out of those batteries.)
Exactly. I accepted that the batteries can only run things with an externally powered speaker. Every scenario works with an externally powered speaker. The problem now is the crashes on USB power with an unpowered speaker at >= 20% of the volume (and on the 20Mhz crystal).
As you havent provided deatils of the load you are attaching - speaker or hedphones - I cna only guess; however as has been said if the load impedance is 8 ohm you should not expect to drive it directly - thats why you have added an amplifier.
Have you calculated the ripple you can expect when driving an 8 ohm load?
I'd expect much bigger decoupling capacitors woud be needed.
Apologies. The on-board speaker is 8 Ohm 1 W, same issue plugging in headphones.
I did not. Is this the best practice? What is the process? I went by the the capacitors that I have on the breakout setup, except I missed the 4.7uF capacitor. Is it critical?
I should mention, I am a software person, still learning as I go here. Thank you.
You said that you also removed the batteries, but it looks like a power problem anyway. I don't know, but could be that the batteries provide more than 5V and then D4 from the USB is reverse biassed? But without batteries it should work.
And where is the USB-C cable connected to? a USB charger or a computer?
I tried both a charger and from the computer, same issue. I also tried running off a USB-C breakout board that I then attach directly to the ICSP pins (H1), bypassing all diodes entirely. Same issue.