Author Topic: Touch Synth WIP: Atmega328pb unstable outputting audio over PWM into a speaker  (Read 830 times)

chiptune, neil555, Hiemal and 5 Guests are viewing this topic.

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.
« Last Edit: July 12, 2024, 03:47:04 am by chiptune »
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
can run off 2 CR2032 batteries

on-board SMD 8Ohm 1W speaker

The 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:
when it is battery powered, the voltage drops on the MCU momentarily from ~5V to around 2.6V

Presumably, 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.
« Last Edit: July 12, 2024, 04:49:05 am by MK14 »
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.
« Last Edit: July 12, 2024, 04:52:06 am by chiptune »
 

Offline moffy

  • Super Contributor
  • ***
  • Posts: 1889
  • Country: au
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.
 
The following users thanked this post: MK14

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.
 
The following users thanked this post: MK14

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
What about the 16MHz crystal, for the MCU?
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Because it is an Atmega328pb, I am running off a 20MHz crystal.
 

Offline ch_scr

  • Frequent Contributor
  • **
  • Posts: 840
  • Country: de
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.
 
The following users thanked this post: Ian.M

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
Because it is an Atmega328pb, I am running off a 20MHz crystal.

I 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
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10086
  • Country: nz
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
« Last Edit: Yesterday at 02:17:09 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: MK14

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.

I have a 100nF and a 1uF there. See https://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570493/#msg5570493
Thank you!
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Be careful when you have series inductors followed by capacitors and throw an AC signal.

It's really easy to accidently make a voltage boost circuit and overvolt things. Which might be what's crashing your mcu

Thank you for the tip. I only have DC input in, so I think I should be fine there, right?
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
I posted it in the reply here https://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570511/#msg5570511
Sorry if I wasn't clear. Thank you.

You 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.
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
I posted it in the reply here https://www.eevblog.com/forum/projects/atmega328pb-unstable-outputting-amplified-audio-over-pwm-into-a-speaker/msg5570511/#msg5570511
Sorry if I wasn't clear. Thank you.

You 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.

Fair enough. Here is the full schematic. Appreciate any help.
 
The following users thanked this post: MK14

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.
« Last Edit: Yesterday at 12:50:57 am by chiptune »
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
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.

It 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.
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
It 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.

If 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?
 
The following users thanked this post: chiptune

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
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?1654276084

You 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.
« Last Edit: Yesterday at 07:51:27 pm by MK14 »
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Some parts of your (full) schematics, don't look 100% right to me.  I was hoping others would chime in, and help sort it out, as necessary.  Such as the combined battery voltage, being above 5 volts.

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?1654276084

You 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, but it might not be.

Thank 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 ground

I 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.
 
The following users thanked this post: MK14

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Found this project, a morse code tutor, that uses the PAM8302: http://w8bh.net/PocketTutor4.pdf
The 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.
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
Found this project, a morse code tutor, that uses the PAM8302: http://w8bh.net/PocketTutor4.pdf
The 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.

The 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).
« Last Edit: Yesterday at 09:15:33 pm by MK14 »
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Found this project, a morse code tutor, that uses the PAM8302: http://w8bh.net/PocketTutor4.pdf
The 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.

The 100k R13, seems to be limiting the input, to around 10%, even at full potentiometer volume at max, of the incoming voltage level.

Which makes sense, to stop possibly saturating the input to the amplifier (i.e. overloading the input).

I 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 volume

That 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 - see https://www.pololu.com/docs/0J74/3 and https://www.pololu.com/file/0J1463/a-star-328pb-micro-schematic.pdf
VCC 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!
« Last Edit: Today at 12:43:04 am by chiptune »
 
The following users thanked this post: MK14

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4700
  • Country: gb
The A-Star board has a 4.7uF decoupling capacitor - would that make that much of a difference?

If 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).
« Last Edit: Today at 01:24:50 am by MK14 »
 

Online chiptuneTopic starter

  • Contributor
  • Posts: 16
  • Country: us
The A-Star board has a 4.7uF decoupling capacitor - would that make that much of a difference?

If 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).

Very, 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 well https://www.arduino.cc/en/uploads/Main/Arduino_Nano-Rev3.2-SCH.pdf


« Last Edit: Today at 01:35:15 am by chiptune »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf