[SOLVED (with MCP4161-502E/P)] Needing software-controlled speaker volume

I need a way to let software adjust the speaker volume when playing audio files, to any of 10 specific volume levels.

I think I can do it using a compander chip, such as the SSM2166A, on the audio input pin of the LM386 speaker amplifier. I would control the compander using an analog voltage, set by a well-filtered PWM Arduino output pin.

But perhaps you know a better way?

Don't people often use digital potentiometers for this? I remember seeing mention of that in the datasheets for digipots when I was working with those.

Have a look at the PGA2311 volume control chip.

Ian.

https://www.adafruit.com/products/1752 Leo..

Thank you, guys! All three of your comments helped steer me in the right direction. I didn't even know that digital potentiometers and volume controls existed!

I wound up ordering this one. A simple 8-pin DIP one-channel potentiometer (77 cents each), controlled through my Arduino's SPI pins.

Lets hope it passed the audio through without noise or distortion.

You might find this interesting or useful.

Human hearing is not lineair. You might have to write some code that uses say 50 values of your 256-value pot to simulate a log pot. Loading the output side with a resistor also might help. http://www.geofex.com/article_folders/potsecrets/potscret.htm Leo..

raschemmel:
You might find [this ](http://tronixstuff.com/2011/05/13/tutorial-arduino-and-
the-spi-bus/)interesting or useful.

Thank you for the SPI tutorial, raschemmel. I only wish you could have presented it to me a year ago when I first started learning to use the SPI bus. At that time, I was getting bizarre result (including the LED going dim from the power drain) and I had no idea why. After all, I had all three SPI peripherals hooked to the same Cable Select pin (SS), so what could possibly be wrong?? ::slight_smile:

I’ve saved your tutorial in my “Learn” folder. Currently, my SPI bus is running the color LCD display, the SD Card, and radio transmit/receive; all items working very well. In a few days, the speaker volume control will be joining the SPI bus.

Wawa:
Human hearing is not lineair… write some code to simulate a log pot.

So true, Wawa. Our ears are designed that way so we can hear well over a large range of loudness. If we were linear instead, most sound would be out range, either too soft or too loud to be heard.

Instead of 50, I’m giving the user just 10 volume levels to choose from to keep it simple for them. I’ve already applied your logrhythmic concept to white-sound generation, like this:

byte volLevel(byte n)
{
  switch (n)              // each volume level is close to DOUBLE the level before it.
  {                          //  and the effect on my ears  seems just right.
    case 1: return 2;
    case 2: return 4;
    case 3: return 7;
    case 4: return 12;
    case 5: return 20;
    case 6: return 33;
    case 7: return 55;
    case 8: return 92;
    case 9: return 153;
    case 10: return 255;
  }
  return 0;
}

byte vol = volLevel(chosenLevel);

      for (byte i=0; i<100; i++)
      {
        PORTC = random(vol);  //PORTC goes to my resistor bridge,
      }                                   //  converting digital to analog for the speaker amp.

Wawa:
Loading the output side with a resistor also might help.
The Secret Life of Pots
Leo…

Yes. With the speaker a resistor is not needed, but when someone wearing earbuds plugs them in, and the power that was filling the entire room is now suddenly going directly into their ear; their hearing could be permanently damaged for life! A resistor must be used to reduce the earbud sound to a level matching the percieved loudness of the speaker. Something like this:


NOTE: I’ve corrected this image according to the comments below.

You have understood the first part, but it would have been easier to use an online calculator for the values, like this one.

http://www.sengpielaudio.com/calculator-db.htm

Start with 48db. The ratio (value) is 251. (about 256). 3db steps >> 45 = 178 42 = 126 39 = 89 etc.

I think you didn't get the second part. If you use a resistor from pot wiper to ground, you can change a lineair pot into a log pot. Leo..

CosmickGold: Something like this: |500x250 And I'm not sure 15 ohms is enough.

For a 50 ohm earpiece, it isn't.

That circuit is wrong. It implies a basic stereo jack and you would - even with the correct 100 Ohm+ resistor - be feeding full audio to the left channel and attenuated audio to the right. What I presume you intend, is a jack with an isolated break contact, such as this one - type "C": .

Unfortunately I could not find the correct circuit in diagram form either! You would connect the speakers through the isolated "back" contacts, and the earphones through two 100+ Ohm resistors.

A very crude way of doing this of course, is to put the resistor in series with the jack sleeve.

Thanks again, both of you. I fully agree with your earbud jack corrections. And I'm glad to have a better idea what resistor to use.

And yes, wawa, I was totally missing what you meant about the resistor. I didn't even know it was possible to change a linear taper to a log taper. So I googled the concept and found this article. It shows the below chart, where the red line is a linear pot. The blue line is an audio taper (log) pot. And the yellow line is what you get with a 100k linear pot that has a 10K resistor added to the center wiper. I'm astonished the blue and yellow lines match so closely, since they are created in such different ways.

And here's the circuit I think you had in mind: NOTE: I've corrected the errors in this image according according to the comments below.

Second diagram is a REVERSE-log pot.

Wiper resistor has to go to ground for a LOG pot. Leo..

Not sure whether those graphs relate to an actual "log" pot, which it has been suggested here in the past is far more crude having really only two slopes.

Thank you for your comments and corrections.

I only wish I could go back and correct my several mistakes. But a moderator once asked me to not do that if someone has already commented on my mistakes; because doing so would make their comments -- about what's no longer there -- seem confused and illogical.

Hopefully, anyone wanting to use this information will read through and grasp your corrections.

"Mistake" is just another word for "learning opportunity". Leo..

CosmickGold: I only wish I could go back and correct my several mistakes. But a moderator once asked me to not do that if someone has already commented on my mistakes; because doing so would make their comments -- about what's no longer there -- seem confused and illogical.

On the other hand, if you correct it, we can correct our corrections. :grinning:

I only wish I could go back and correct my several mistakes. But a moderator once asked me to not do that if someone has already commented on my mistakes; because doing so would make their comments -- about what's no longer there -- seem confused and illogical.

I go back and correct them anyway and just say "I have no idea what you're talking about " "I've never seen that post before " ;)

OK guys! I went back and corrected an image in comment #7, and another image in comment #10.

Under each correction, I added the line:

“NOTE: I’ve corrected this image according to the comments below.”

CosmickGold:
“NOTE: I’ve corrected this image according to the comments below.”

Of course, if the comments are themselves deleted, you will have to re-adjust your adjustment!

One superb aspect of the development of computers in business (or indeed, any other form of enterprise) is the ability to make corrections ad libitum.

A singularly important of the IBM PC - and particularly the “AT” version - is the set of ten cursor keys (not to mention the original numeric set which make a standard four-function calculator). The “insert” key is problematic as the “overtype” function is almost never useful and in fact, a menace unless programmers are careful to disable it. A further nuisance is this horrible variant which removes the insert key, but fouls up the positioning of all the remaining keys causing immense frustration.

The point is that the ability to rapidly and accurately move to any part of the document is the most basic pre-requisite for word processing, preceding and substantially augmenting the use of the mouse. :grinning:

This is the MCP4161-502E/P digipots I ordered.

It works great as my speaker volume control. It is very simple, but the datasheet for it makes it look impossibly hard, with 88 pages of details such as this page:

How could ANYONE figure all that out out?

Fortunately, after two long days of searching the forum (and Google) for a simpler explanation, I developed a solution that is truly easy, and want to share it with anyone else who might need this great little 8-pin chip:

#include <SPI.h>

void setup()
{
  pinMode(10,OUTPUT);
  SPI.begin();
  delay(10);
}

void setPot(byte vol)
{
  digitalWrite(10, LOW);
  SPI.transfer(0);
  SPI.transfer(vol);
  digitalWrite(10, HIGH);
}

In the above:
(1) 10 is the Arduino pin number I chose for “Chip Select”. (Labeled “out” below.)
(2) The line “SPI.transfer(0);” is the command line, telling it to change the volume.
(3) “vol” should be a number between 0 and 255 (0 = quiet. 255 = loud.)

And as for how to wire it up; here’s the chip:

And here’s a circuit to use it in:

Note: The volume-control chip is the pot above labeled “5k”.

1 Like