Touch Synth WIP: Atmega328pb unstable outputting audio over PWM into a speaker - Page 1
Forum section: electronics / projects

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 03:18:26 am
content: 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.

author: MK14
author_rank: Super Contributor
timestamp: July 12, 2024, 04:44:40 am
content: Quote from: chiptune on July 12, 2024, 03:18:26 amQuote from: chiptune on July 12, 2024, 03:18:26 amThe CR2032's are relatively very weak battery types. You are only suppose to take a maximum of something like 3 milliamps from them continuously, or 10 milliamps temporarily (very short term).They have rather high ESR's (internal resistances).But a 1 watt speaker (at full volume and ignoring efficiencies and other power consumers), would be something like 200 milliamps at 5 volts. 0.2 (Amps) x 5 (Volts) = 1 watt.So, I am NOT at all surprised, your battery voltage collapses, when trying to power a one watt speaker, from those CR2032's.I.e. This:Quote from: chiptune on July 12, 2024, 03:18:26 amPresumably, at such a low voltage, your Arduino crashes or resets, so stops driving the speaker.It is a long and complicated post, so I could have easily missed other problems/issues.

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 04:50:15 am
content: Agreed. It still crashes off of USB though, no voltage drop there. The prototypes run off of USB power and they both work. Any idea?Thank you for reading. I know it is a very long post, but I wanted to include my debugging thoughts too.

author: moffy
author_rank: Super Contributor
timestamp: July 12, 2024, 04:52:41 am
content: Please include the full schematic, especially anything power supply related, it might be voltage induced resets on the mcu. What diagnostic tools do you have? Perhaps the simplest first test would be to power the output amp separately and see if the problem disappears. I suspect the CR2032 have a quite high internal resistance, they are meant for low current loads not headphone amplifiers.

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 05:04:33 am
content: Here are the power and MCU schematics. At some point I plan to switch to STM32 for the native USB and the FPU, but for now I want to understand what went wrong.I have a cheap oscilloscope, which is where I saw the voltage drop off the batteries. Still don't know why the breadboard prototypes would work and the PCB off of USB won't.Thank you for the help.

author: MK14
author_rank: Super Contributor
timestamp: July 12, 2024, 05:17:56 am
content: What about the 16MHz crystal, for the MCU?

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 05:26:09 am
content: Because it is an Atmega328pb, I am running off a 20MHz crystal.

author: ch_scr
author_rank: Frequent Contributor
timestamp: July 12, 2024, 06:18:31 am
content: Quick observation: no Vcc decoupling on the Atmega power supply pins? Put 100nF X7R 16V close to the chip from Vcc to Gnd (one at each power pin). See if that improves things.

author: MK14
author_rank: Super Contributor
timestamp: July 12, 2024, 10:38:07 am
content: Quote from: chiptune on July 12, 2024, 05:26:09 amI had to go by the schematics, that you seemed to supply. Which don't seem to show any crystals.Here:https://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570493/#msg5570493

author: Psi
author_rank: Super Contributor
timestamp: July 12, 2024, 11:00:17 am
content: Be careful when you have series inductors followed by capacitors and throw an AC or pulsed signal into the mix.It's really easy to accidently make a voltage boost circuit and overvolt things. Which might be what's crashing your mcu

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 02:27:38 pm
content: Quote from: ch_scr on July 12, 2024, 06:18:31 amI have a 100nF and a 1uF there. Seehttps://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570493/#msg5570493Thank you!

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 02:28:21 pm
content: Quote from: Psi on July 12, 2024, 11:00:17 amThank you for the tip. I only have DC input in, so I think I should be fine there, right?

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 02:29:39 pm
content: Quote from: MK14 on July 12, 2024, 10:38:07 amI posted it in the reply herehttps://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570511/#msg5570511Sorry if I wasn't clear. Thank you.

author: MK14
author_rank: Super Contributor
timestamp: July 12, 2024, 02:42:39 pm
content: Quote from: chiptune on July 12, 2024, 02:29:39 pmYou did.But that is afterwards (after my initial comment, about the crystal appearing to be missing).The point I was trying to make, is that if we only get bits and pieces of your overall complete and full schematic(s). It makes it very difficult for other people to try and see what is going on, and comment on possible mistakes or missing things.In some cases, even the exact PCB layout, can cause issues.You need to decide and way up between releasing the full or a fuller schematic, versus how able and willing, we are going to be, to help spot possible issues.

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 02:51:30 pm
content: Quote from: MK14 on July 12, 2024, 02:42:39 pmFair enough. Here is the full schematic. Appreciate any help.

author: chiptune
author_rank: Contributor
timestamp: July 12, 2024, 08:16:02 pm
content: I might be onto something.I plugged my jack to bare wire cable and added 2 330Ohm resistors on the differential audio lines and plugged my 8Ohm 1W speaker after that and it works fine (albeit very quiet). I think the ferrite beads that I picked on those lines have very low resistance (300 mOhm, 600 Ohm@100Mhz). The datasheet mentioned picking very low Z at low frequency, which probably makes sense for externally powered speakers, but for the on-board speaker and headphones it might be too low. The Adafruit board doesn't mention the Ferrite bead impedance. I am thinking this was the issue and adding resistors in series solved things. Thoughts?Also, what resistances would make sense in this case? any best practices?Thank you.

author: chiptune
author_rank: Contributor
timestamp: July 13, 2024, 06:09:07 pm
content: Quote from: chiptune on July 12, 2024, 08:16:02 pmIt is definitely not the beads. I think I needed a fixed large resistor on the PWM line before the amp. I think the amp is overloaded when it gets a 5V input and is driving a load. This doesn't seem to be a problem with a powered speaker, but the volume at which it overloads changes if I decrease the duty cycle of the output to reduce the voltage. If anyone has a good explanation for me I would be grateful.

author: MK14
author_rank: Super Contributor
timestamp: July 13, 2024, 07:02:21 pm
content: Quote from: chiptune on July 13, 2024, 06:09:07 pmIf it was me trying to fix it, and I was still not sure what has gone wrong.I'd do something like, take the audio output (which you seem to call AUDIO pin 32 of MCU), optionally electrically disconnect it from the following circuitry, as it could be dragging the signal down or interfering/messing with it.By looking at it with a scope and/or connecting it to a known good/working amplifier. To make sure a good audio output is coming from your MCU.Similarly, I'd be tempted to take an audio signal, such as a 1kHz, somewhat low amplitude test signal, and connect it as input to the audio amplifier, you have built into your project. To see if it works ok.Optionally, going before and after your audio potentiometer configuration.Actually, just using an oscilloscope, with the probe(s), tracing it from where it exits the MCU, until it reaches the speaker (or whatever output you have configured it for), should also do the trick.Don't worry about doing it differently to how I describe, there are millions of possible variations.So, where (when looking at it on a scope), does it start to deteriorate/go-funny/disappear etc?Starting from the original source (MCU AUDIO output), though the potentiometer and through the audio amplifier?

author: MK14
author_rank: Super Contributor
timestamp: July 13, 2024, 07:34:41 pm
content: I'm not convinced by the way, you have grounded (after the 100 Ohm resistor and 1 Microfarad capacitor), the IN_N input. Because the Adafruit schematic, seems to leave it floating (as in, connected to the pot, but NOT connected to the actual GND), but connected to the pot.See here:https://cdn-learn.adafruit.com/assets/assets/000/112/257/original/components_schem.png?1654276084You might have done it correctly, depending on if you mean the common GND to everything or one of the 3 pot terminals, but it not otherwise connected to the common ground.It still could be ok (some connection diagrams, seem to show GND, but directly into the chip, not via the R/C, but it is not 100% clear), but it might not be.

author: chiptune
author_rank: Contributor
timestamp: July 13, 2024, 07:51:13 pm
content: Quote from: MK14 on July 13, 2024, 07:34:41 pmThank you for following up!With 2 new CR2032 I am getting around ~5.3V after the Schottky diode, which I think should be safe for the MCU and the amp.In the Adafruit schematic, it is connected to both the pot and pin 4 of JP1, and the product page (https://www.adafruit.com/product/2130) states that:The A+ and A- inputs of the amplifier go through 1.0uF capacitors, so they are fully 'differential' - if you don't have differential outputs, simply tie the Audio- pin to groundI should mention, my synth works fine with an externally powered speaker. I can output 4 wave types at the right frequencies and can do that polyphonically as well. The problem is connecting to headphones or the on-board speaker, both of which would be unpowered and act as load. If I reduce the PWM duty cycle in the code, I can lower the volume and then I can move the pot to say 50% before I crash, when earlier I would crash around 10% of the way. The big mystery to me is that the Adafruit board never crashes.Thank you again MK14.

author: chiptune
author_rank: Contributor
timestamp: July 13, 2024, 08:40:24 pm
content: Found this project, a morse code tutor, that uses the PAM8302:http://w8bh.net/PocketTutor4.pdfThe schematic on the last page is almost exactly the one Adafruit has except for an additional 100k Ohm resistor on the output.Still doesn't explain the mystery of the Adafruit board not crashing though.

author: MK14
author_rank: Super Contributor
timestamp: July 13, 2024, 09:12:44 pm
content: Quote from: chiptune on July 13, 2024, 08:40:24 pmThe 100k R13, seems to be limiting the input, to around 10% of its original voltage level, even at full potentiometer volume at max setting.Which makes sense, to stop possibly saturating the input to the amplifier (i.e. overloading the input).

author: chiptune
author_rank: Contributor
timestamp: July 13, 2024, 10:11:27 pm
content: Quote from: MK14 on July 13, 2024, 09:12:44 pmI did some more probing, so here is a structured list to explain that mystery:* With a powered speaker, every setup works* A-Star MCU-> Adafruit amp -> unpowered speaker == Works* My MCU -> My amp -> unpowered speaker == Crashes at >= 10% of volume* A-Star MCU-> My amp -> unpowered speaker == Works (!)* My MCU -> Adafruit amp -> unpowered speaker == Crashes at >= 10% of volumeThat shifted the blame now from the amp design to the power circuit in my opinion. Both the A-Star MCU and my MCU on the PCB are running the same exact code.It is not the Schottky diode - I poweded my MCU past it and still didn't work.I also powered the A-Star board through both VCCIN and VCC - seehttps://www.pololu.com/docs/0J74/3andhttps://www.pololu.com/file/0J1463/a-star-328pb-micro-schematic.pdfVCC skips the LDO regulator, so it is probably not that. The A-Star board has a 4.7uF decoupling capacitor - would that make that much of a difference?Thank you!

author: MK14
author_rank: Super Contributor
timestamp: July 14, 2024, 01:18:53 am
content: Quote from: chiptune on July 13, 2024, 10:11:27 pmIf you mean C3 4.7 μF and R2 (1 Ω), then they are probably a requirement of the specific LDO datasheet (I can't see the LDO part number), as it specifies (I suspect) a minimum ESR (equivalent series resistance, some types of capacitor can have a value, which is too low, to work well), for that capacitor, to ensure it damps down oscillations (for stability).So, it shouldn't matter, as you are not using an LDO.In your PCB layout. Are things like the crystal, its supporting (load) capacitors and the decoupling capacitors, very, very close to the IC pin(s), that need them?Did you source all your parts from reliable sources?I.e. Not from AliExpress/Ebay Chinese sellers, with x10 .. x50 parts for $0.25 total, for things that normally cost at least $2 each, everywhere else?(Because of possible, fakes, rejected components, substandard-clones, reused components, faulty parts, etc).

author: chiptune
author_rank: Contributor
timestamp: July 14, 2024, 01:28:32 am
content: Quote from: MK14 on July 14, 2024, 01:18:53 amVery, very close, on both the MCU and the amp. All my parts are from LCSC, the parts provider for JLCPCB.I am now wondering specifically about the value 4.7uF because I can see it on the Nano schematic as wellhttps://www.arduino.cc/en/uploads/Main/Arduino_Nano-Rev3.2-SCH.pdf

author: chiptune
author_rank: Contributor
timestamp: July 14, 2024, 04:50:19 am
content: Quote from: chiptune on July 14, 2024, 01:28:32 amOne last update: No more issues when I disable the crystal and use the internal oscillator instead (but of course the audio quality is now terrible). I am not sure what to make of that.Should I have a ground plane behind a 20MHz oscillator and its copper traces?Would the 4.7uF polarized capacitor have helped?

author: MK14
author_rank: Super Contributor
timestamp: July 14, 2024, 10:12:17 am
content: I'd suggest posting the PCB layouts. Then people here can check it out, especially as regards things like the crystal section, missing or poorly placed decoupling capacitors (as already mentioned by another poster), and other things.

author: chiptune
author_rank: Contributor
timestamp: July 14, 2024, 07:59:45 pm
content: Quote from: MK14 on July 14, 2024, 10:12:17 amI went over my PCB design and evaluated it against various posted guidelines and two things are clear violations from what I can tell:1) I used vias on my crystal signal lines, didn't cross the audio output though2) I violated the length tolerance for the audio differential pair - not sure I can do better because of the switching jack3) not clear: I have a ground plane under the crystal - lots of differences of opinion hereWhat I don't understand is why I need the combination of 3 things to fail:1) unpowered speaker2) using the external crystal3) high volume PWM output - either through the pot or through the codeIt must be all 3 happening for things to fail - I can see how 2) and 3) together might cause interference, but I don't see what role 1) would play here.

author: MK14
author_rank: Super Contributor
timestamp: July 16, 2024, 07:52:47 pm
content: Quote from: chiptune on July 14, 2024, 07:59:45 pmI'm probably stating the obvious here.I'll speak for myself.If the PCB design is hidden away, in some secret black box. I can't really see how you want me to help you with it.Hand waving, some descriptions of what YOU think is wrong with it don't really help, that much. As I can't see it.

author: chiptune
author_rank: Contributor
timestamp: July 17, 2024, 05:54:24 pm
content: Sorry, spent some time getting my hands on a better oscilloscope. I now see an occasional and very momentary voltage drop across the MCU, from 5.2V to ~4.3V. I think that is the main issue. I have decoupling caps on one pin of the MCU (100nF and 1uF) but I don't think they are close enough and I should have done the same on AVCC. I think on the higher external clock this drop causes a brown-out. I am thinking of adding another 4.7uF as well as a 120uF bulk capacitor to be safe.I also realized that I didn't pick a good crystal for the MCU. From the AVR hardware design guidelines:https://ww1.microchip.com/downloads/en/AppNotes/AN2519-AVR-Microcontroller-Hardware-Design-Considerations-00002519B.pdfPage 16, Section 5.3:"For the newer AVR PB, the recommended capacitor value range is 12 pF - 22 pF, the total capacitance (Ce + Ci + Cs) for each pin must not exceed 22 pF."My crystal's load capacitance alone is 20pF, so I probably have 30pF to 40pF at XTAL. The diagram in the guide has a crystal with 9pF load capacitance and external capacitors of value 6.5pF each.My power traces are also a bit too thin.The top and bottom PCB design are attached. Thank you MK14.

author: ch_scr
author_rank: Frequent Contributor
timestamp: July 17, 2024, 08:14:56 pm
content: You're correct about the crystal. The ATmega328PB's datasheet gives 6pF internal capacitance per pin.Herethey assume a stray of at least 3pF.As per the formula (Ce+Ci=2Cl-Cs), your crystal (Cl=20pF) would need a Ce of 30pF.Since the Appnote claims "For the newer AVR PB, the recommended capacitor value range is 12 pF - 22 pF, the total capacitance (Ce + Ci + Cs) for each pin must not exceed 22 pF.", and Ci=6pF; Cs assumed 3pF, Ce max would be 13pF. Since 12pF is a standard value, that seems good.That would need a crystal with Cl=10pF. If you go for a Cl=9pF one you'd need 10pF Ce. Both those options seem fine to me.For the decoupling capacitors, as a general rule, put one oneachIC power pin (Vreg, logic IC, VoltReg, Opamp, etc.).Look at the datasheet values but if unsure 100nF is a happy place. Those should beas close as possibleto the IC power pin.The ground pad of a capacitor should have a gnd via next to it's pad. The gnd pin of a IC should have a gnd via next to it's pad.This provides ground plane stitching, but also (more importantly) allows the high frequency currents to take the shortest path.(One should think about those currents while layouting and provide a path for them. An unbroken gnd plane is best.)The bigger the decoupling capacitor value, the further away it can be located from the IC pin. (The position you have them now would be OK for 1uf)Always check voltage regulator datasheets for the recommended supply decoupling.MLCC have a voltage dependend de-rating. Make sure how much of your e.g. 1uF is actually left at used vs. rated voltage for the used dielectric.

author: chiptune
author_rank: Contributor
timestamp: July 17, 2024, 08:46:06 pm
content: Thank you! The AVR guidelines also include a 4.7uF and I keep wondering if omitting that is the cause of what looks like brown-outs in my PCB.Do you think I'd need the bulk capacitor as well given that I have an amp onboard?I also measure the current on the 8Mhz internal oscillator at full volume and I am withdrawing ~260mA. Do you think my power traces at 0.381mm are sufficient?Thank you again.

author: ch_scr
author_rank: Frequent Contributor
timestamp: July 18, 2024, 08:28:21 am
content: A bit of extra bulk capacitance isn't hurting. When ordering a pcb, pads, like vias, are essentially free.So it's easier to provide some extra and populate as needed. The trace width seems fine to me. Isn't there no recommended bulk capacitance in the amplifier datasheet?Here'sa trace width calculator.My suspicion is the lack of decoupling / bad layout (especially with respect of ground return currents from the decoupling caps) is to blame.

author: xvr
author_rank: Frequent Contributor
timestamp: July 18, 2024, 12:27:54 pm
content: The crystal is located too far from the MCU. Ideally, the pins of the crystal should be directly connected to the pins of the microcontroller, and no other traces should cross these connections.You have plenty of room to move the crystal closer.And decoupling capacitors - as already said.