WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [SOLVED] Audio over PWM on PiZero  (Read 12158 times)

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: [SOLVED] Audio over PWM on PiZero
« Reply #15 on: March 31, 2021, 09:51:30 PM »
We need some more details at this point. Check the following 2 items with the speaker connected and disconnected:
Does the output of the amp have a (relatively) stable square wave when the pi is playing nothing?
Does the square wave coming from the amp only get jittery when the speaker-test utility gets run?

Yes, the output of the amp appears to be relatively stable, both with and without the speaker connected, when the pi is playing nothing.

and

Yes the square wave coming from the amp only gets jittery when I use the speaker-test utility, or attempt to play a wav file, again both with and without the speaker connected.

basically the only difference I can see between the pi's gpio output square wave, and the square wave coming off of the amp appears to be the voltage levels.  The pi's gpio output ranges from about 2V to about 6V, and the amp's output ranges from about 2V to about 8V.

Since this is an amplifier, you would think the output level should be at least as great as the input level, right?
See if you can barely hear the output when the speaker is connected to the amp.

Tried this, and if I hold the speaker up to my ear, as in, touching my ear, I can hear the audio playing.  I'm starting to feel stupid over here, thinking there must be some volume control on the pi/alsa layer that I'm missing or something. 

Oh, and btw, thanks for all the help on this Rich, it's much appreciated.   :)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #16 on: March 31, 2021, 10:22:50 PM »
Hi creitzel
A couple more items:
... The pi's gpio output ranges from about 2V to about 6V, and the amp's output ranges from about 2V to about 8V. ...
What are the voltage levels (at both locations) with the speaker connected?

Sync on the square wave and adjust the timebase so (approximately) 1 full cycle occupies the width of the screen.
With no audio, the first transition should be approximately centered (50%) horizontally on the screen.
Play some audio. If you are near full volume, you should see the transition point move between 10% and 90% of
the screen width.

You are AC coupling the speaker, right?

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: [SOLVED] Audio over PWM on PiZero
« Reply #17 on: March 31, 2021, 10:54:30 PM »
Rich,

I've shut down my rig for the night, as I'm getting ready to hit the sack.  I'll do the tests you asked for tomorrow after work.  Oh, and I should mention that the voltage levels I quoted earlier for the pi gpio were taken without the pi connected to the rest of the circuit (i.e. as if the scope was connected right to the pi's pin).

I do have a question though, what does
Quote
AC coupling the speaker
mean?  Are you talking about the dc coupling/ac coupling setting on my oscilloscope?

Remember, I'm a software guy, playing around in hardware land as a hobby lol.

Thanks,

Chris

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #18 on: March 31, 2021, 11:05:35 PM »
Hi creitzel
... I do have a question though, what does
Quote
AC coupling the speaker
mean?  Are you talking about the dc coupling/ac coupling setting on my oscilloscope? ... 
Typically a capacitor is placed in series with the speaker in this type of circuit.

Quote
... Remember, I'm a software guy, playing around in hardware land as a hobby lol.
That's OK. If I say something that's over your head, just speak up. :)

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: [SOLVED] Audio over PWM on PiZero
« Reply #19 on: April 01, 2021, 11:34:34 PM »
Ok, finally got some time to do some tests today.  Had an impromptu dinner invitation from some of our friends, and spent some time hanging out afterwards. :)  So:

What are the voltage levels (at both locations) with the speaker connected?
They look like they are the same to me with the filter/amp/speaker connected.  So pi gpio ~2V to ~6V and amp output ~2V to ~8V.  I'm attaching 2 screen grabs from my scope, so you can see for yourself.  Maybe I'm reading the scope incorrectly. :)

Sync on the square wave and adjust the timebase so (approximately) 1 full cycle occupies the width of the screen.
With no audio, the first transition should be approximately centered (50%) horizontally on the screen.
Play some audio. If you are near full volume, you should see the transition point move between 10% and 90% of
the screen width.

I performed this test as well, and it does appear as you describe, when no audio is playing.  When audio is playing however, the transition only seems to be moving from around 30% to 70% or so.  I never saw it get as low as 10% nor as high as 90%.

You are AC coupling the speaker, right?
Thanks for the explanation on this.  Currently, my circuit does not include a capacitor in series with the speaker.  The speaker is connected directly to the outputs on the amp.  Though, I've successfully had this amp, and speaker combination (wired up the same way) working with a project on an Esp32, not too long ago.  Maybe the amp already has the cap(s) on the output lines? 

Here's a link to the breakout board amp I'm using, if it helps:

https://www.amazon.com/HiLetgo%C2%AE-PAM8403-Digital-Amplifier-2-5-5V/dp/B00LODGV64/ref=sr_1_19?dchild=1&keywords=pam+8403&qid=1617334208&sr=8-19


Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #20 on: April 02, 2021, 12:46:40 AM »
Hi creitzel
... I performed this test as well, and it does appear as you describe, when no audio is playing.  When audio is playing however, the transition only seems to be moving from around 30% to 70% or so.  I never saw it get as low as 10% nor as high as 90%. ...
I would think even that should be enough to be audible.

Are you sure that's a 4 or 8 ohm speaker and not a 100 ohm speaker?

You are AC coupling the speaker, right?
Forget about that. I didn't realize your amplifier had differential outputs.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #21 on: April 02, 2021, 12:55:04 AM »
Hi creitzel
What command are you using for speaker-test? Is it something like this:
Code: [Select]
speaker-test -c2 -t wav

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: [SOLVED] Audio over PWM on PiZero
« Reply #22 on: April 02, 2021, 05:34:26 PM »
I would think even that should be enough to be audible.

Are you sure that's a 4 or 8 ohm speaker and not a 100 ohm speaker?
Definitely a 4 ohm speaker.  Here's a link to the speaker in quesiton:

https://www.amazon.com/gp/product/B01LN8ONG4/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

What command are you using for speaker-test? Is it something like this:
Code: [Select]
speaker-test -c2 -t wav

I've been using:
Code: [Select]
speaker-test wav
I didn't know about any of the options for it before, I just looked up the man page, after reading this.   I see that the -t option allows you to specify different generated noise, since I wasn't specifying anything, it defaults to pink noise right?  I also see that the -c option is to specify the number of channels.  It doesn't say what the default is though.  I'm only looking for mono audio, so I have everything wired up so that only the left channel is eventually hooked to the speaker.  Do you think I need to specify -c1?


Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #23 on: April 02, 2021, 08:17:04 PM »
Hi creitzel
On my system, if you don't specify the number of channels, it defaults to left. By using  -c2  it alternates between the
left and right channel. This way, even if you didn't connect to the channel you thought you did, you should still hear
something.

This command alternates saying the phrase  "Front Left"  and  "Front Right":
Code: [Select]
speaker-test -c2 -t wav
This alternates a tone between the left and right speaker:
Code: [Select]
speaker-test -c2 -t sine

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: [SOLVED] Audio over PWM on PiZero
« Reply #24 on: April 03, 2021, 08:41:44 PM »
On my system, if you don't specify the number of channels, it defaults to left. By using  -c2  it alternates between the left and right channel. This way, even if you didn't connect to the channel you thought you did, you should still hear
something.

This command alternates saying the phrase  "Front Left"  and  "Front Right":
Code: [Select]
speaker-test -c2 -t wav
This alternates a tone between the left and right speaker:
Code: [Select]
speaker-test -c2 -t sine

Thanks for the info on the speaker-test utility.  I've since read the man page, and I'm getting to know it a bit better.  Nice utility for this type of project for sure.

So, I've got good news.  I finally have sound.  After quite a bit of testing, and sending screen grabs from my oscilloscope to my buddy (the one who helped me build the daughter card), I was able to identify that the amp was in fact bad.  I soldered pins on another one, and plugged it in, and immediately got sound like I was expecting.

Thanks a ton for all the help you guys have given me on this, I feel like, not only is this part of my project working now, but I've gained quite a bit of knowledge as well.  Great forum :-)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: [SOLVED] Audio over PWM on PiZero
« Reply #25 on: April 03, 2021, 09:46:59 PM »
Hi creitzel
Glad to hear you got it working. Thanks for reporting the solution.