Controlling volume from speakers

Then use the tone out function to generate the noise and use the digital pot to control the output signal before you put it into an audio amplifier.

xxryan1234:

Grumpy_Mike:
You could use a digital pot to controle the volume of a sound signal:-
http://arduino.cc/en/Tutorial/SPIDigitalPot
Or do you want to generate the sound as well?

Yes, I do want to generate sound as well. I want the sound to depend on the strength of my blow through the wind sensor.

My toneAC library is a replacement for the standard tone library and includes a volume setting. You can specify a volume for each note. There's also a host of other advantages of using my library over tone.

Tim

I think you need to brush up on your theory:-

the loudest is at 50% duty cycle (only on half the time). Which at 5 volts, is like sending only 2.5v to the speaker. With toneAC, we're sending out of phase signals on two pins. So in effect, the speaker is getting 5 volts instead of 2.5, making it nearly twice as loud.

This is wrong.

What is happening is that the standard tone library you are sending 5V in a single ended drive. With what you are doing you are sending +/- 5V in a push pull arrangement so in effect boosting the output to 10V. Which is why it is twice as loud.

I tried using teckel's toneAC library but it doesn't work(it just says 'toneAC' was not declared in this scope even though I already imported the library to my project), I don't think it's compatible with my Arduino ATmega644. I would have loved to use it to save money using a digital potentiometer because i'm really low on budget right now but I guess I have to follow what mike has said. Been searching tutorials on how to use digital potentiometers and hopefully when my order arrives everything will turn out okay. Sorry for the very late reply guys! been very busy these days. :slight_smile:

I have a question Mike, so before I connect my speaker to the arduino it goes through the digital potentiometer first right?

and also...
I ordered a X9C103P Digital Potentiometer will this be okay to do the job? I ordered this one because it's the only one that is available here in our region. :slight_smile:

1

so before I connect my speaker to the arduino it goes through the digital potentiometer first right?

Not quite, the order goes:-
Arduino tone out -> digital Pot -> audio amplifier -> speaker

X9C103P Digital Potentiometer will this be okay to do the job? I

Yes

Grumpy_Mike:
I think you need to brush up on your theory:-

the loudest is at 50% duty cycle (only on half the time). Which at 5 volts, is like sending only 2.5v to the speaker. With toneAC, we're sending out of phase signals on two pins. So in effect, the speaker is getting 5 volts instead of 2.5, making it nearly twice as loud.

This is wrong.

What is happening is that the standard tone library you are sending 5V in a single ended drive. With what you are doing you are sending +/- 5V in a push pull arrangement so in effect boosting the output to 10V. Which is why it is twice as loud.

I had first said that it was 10 volts, but others corrected me that technically the standard tone library is only getting 2.5 volts because it's being pulsed half the time like a switched-mode power supply. Basically, I have 50% of people saying one thing and telling me I need to brush up my theory and I have the other 50% saying the opposite saying I also need to brush up my theory. I think you two groups need to get together and decide which theory is correct and brush up together.

In either case, twice as much voltage, current or whatever you two groups decide is getting to the speaker with my toneAC library over the standard tone library. You all can fight over if that's 5v or 10v as it doesn't really matter either way to me :wink: I'm more of a software than hardware guy. The end result is a significant volume increase and the ability to control volume.

Tim

xxryan1234:
I tried using teckel's toneAC library but it doesn't work(it just says 'toneAC' was not declared in this scope even though I already imported the library to my project), I don't think it's compatible with my Arduino ATmega644. I would have loved to use it to save money using a digital potentiometer because i'm really low on budget right now but I guess I have to follow what mike has said. Been searching tutorials on how to use digital potentiometers and hopefully when my order arrives everything will turn out okay. Sorry for the very late reply guys! been very busy these days. :slight_smile:

I have a question Mike, so before I connect my speaker to the arduino it goes through the digital potentiometer first right?

and also...
I ordered a X9C103P Digital Potentiometer will this be okay to do the job? I ordered this one because it's the only one that is available here in our region. :slight_smile:

toneAC v1.2 that was just released adds support for the ATmega644 (or at least it should based on the datasheet). But, I think the problem you had was that you were not loading the library correctly. Either it can't find the library or you didn't include it at the top of your sketch. First get v1.2, next make sure you uncompress it and put it in your libraries folder just like the rest of the libraries, then just use the sample toneAC sketch for testing, finally incorporate it into your sketch once you're already confident it works.

If you do have a problem using the library with the ATmega644, let me know. There's specific defines in the library for that chip so it should work. But, I don't have one to test so if there's a problem I'd like to know.

Tim

but others corrected me that technically the standard tone library is only getting 2.5 volts because it's being pulsed half the time.

The others are wrong.
There is no need to get together. PWM is a 0 to 5V signal. At 50% it is still 0 to 5V, it is never 2.5V.
However the average voltage is 2.5V but the peak to peak voltage is 5V.
So your push pull system produces a +5 to -5V signal as seen by the speaker. That is a 10V signal. It has an average voltage of 5V but the problem is that an average voltage tells you nothing. Suppose you use a 25% duty cycle on your system. The average voltage is 2.5V but it is still a 10V peak to peak signal. A 25% duty cycle will sound as loud as a 50% duty cycle tone. The only change a duty cycle makes is to add or subtract a DC offset to the signal and you can't here DC.

When measuring loudness of an electrical audio signal there are several ways you can express it.
Peak
Peak to peak
RMS
Avrage
and peak music
They all give different answers.

If you do have a problem using the library with the ATmega644, let me know. There's specific defines in the library for that chip so it should work. But, I don't have one to test so if there's a problem I'd like to know.

I just tried it on my 644P based board and just using a O-scope and one of your example sketches I could see the clocking on two pins. Different pins then what your library states, but I'm using a Bobuino pin 1284P/644P mapping scheme rather then the older standard Sanguino pin mapping, or whatever ever pin mapping you may have used as you developed the library. As there is no 'official Arduino' pin mapping defined one needs to be able to backtrack to the port/pin number used by the various timers sometimes.

Anyway thanks for adding the 1284P/644P support.

Lefty

I tried the library out but even the loudest sound sounds so weak. :frowning:

X9C103P is a digital potentiometer available in my country, is this okay to be used for volume control?

Grumpy_Mike:

but others corrected me that technically the standard tone library is only getting 2.5 volts because it's being pulsed half the time.

The others are wrong.
There is no need to get together. PWM is a 0 to 5V signal. At 50% it is still 0 to 5V, it is never 2.5V.
However the average voltage is 2.5V but the peak to peak voltage is 5V.
So your push pull system produces a +5 to -5V signal as seen by the speaker. That is a 10V signal. It has an average voltage of 5V but the problem is that an average voltage tells you nothing. Suppose you use a 25% duty cycle on your system. The average voltage is 2.5V but it is still a 10V peak to peak signal. A 25% duty cycle will sound as loud as a 50% duty cycle tone. The only change a duty cycle makes is to add or subtract a DC offset to the signal and you can't here DC.

When measuring loudness of an electrical audio signal there are several ways you can express it.
Peak
Peak to peak
RMS
Avrage
and peak music
They all give different answers.

Awesome! Debate this topic with the people who challenged me about it being 10v AC on this thread:

http://arduino.cc/forum/index.php/topic,141844.0.html

PeterH, PaulS & sbright33 specifically.

I don't have a horse in this race, but more power to those of you who do. Let me know who "wins" and who needed to brush up on their theory, which I see you've modified from your post so it seems you're already questioning yourself.

But, you are wrong in one area. Adjusting the duty cycle of a push/pull PWM does change the volume. 50% is the loudest, but 25% will be lower volume. No reason to believe me, just use toneAC and change the volume parameter which changes the duty cycle and therefore the volume. The volume is controlled by the duty cycle, and quite well I might add.

Tim

retrolefty:

If you do have a problem using the library with the ATmega644, let me know. There's specific defines in the library for that chip so it should work. But, I don't have one to test so if there's a problem I'd like to know.

I just tried it on my 644P based board and just using a O-scope and one of your example sketches I could see the clocking on two pins. Different pins then what your library states, but I'm using a Bobuino pin 1284P/644P mapping scheme rather then the older standard Sanguino pin mapping, or whatever ever pin mapping you may have used as you developed the library. As there is no 'official Arduino' pin mapping defined one needs to be able to backtrack to the port/pin number used by the various timers sometimes.

Anyway thanks for adding the 1284P/644P support.

Lefty

The Bobuino maps pins differently than Arduino. I believe it's pin 8 and 30 for the Bobuino.

Basically, toneAC uses the PWM pins assigned to timer 1. You need to look at the datasheet or contact the manufacture to see which pins are OC1A and OC1B and connect to those pins. The Arduino maps the 1284P/644P to pins 12 & 13. Other systems may map to other pins.

Tim

xxryan1234:
I tried the library out but even the loudest sound sounds so weak. :frowning:

X9C103P is a digital potentiometer available in my country, is this okay to be used for volume control?

Sounds like you've connected to one of the correct pins, but not the other. No matter what system you connect to, you need to connect to the OC1A and OC1B pins. These could be mapped to any pin number. I have no idea what derivative Arduino you have, but you need to get the datasheet or with the manufacture and tie to those pins. The OC1A and OC1B pins are the timer 1 PWM pins, and that's always what toneAC uses.

If you figure out which pins are used, let me know what system you have and what the pins are so I can note it in the library.

Tim

teckel:

xxryan1234:
I tried the library out but even the loudest sound sounds so weak. :frowning:

X9C103P is a digital potentiometer available in my country, is this okay to be used for volume control?

Sounds like you've connected to one of the correct pins, but not the other. No matter what system you connect to, you need to connect to the OC1A and OC1B pins. These could be mapped to any pin number. I have no idea what derivative Arduino you have, but you need to get the datasheet or with the manufacture and tie to those pins. The OC1A and OC1B pins are the timer 1 PWM pins, and that's always what toneAC uses.

If you figure out which pins are used, let me know what system you have and what the pins are so I can note it in the library.

Tim

http://www.e-gizmo.com/KIT/gizduino+%20164,324,644.html
this is the arduino atmega644 that I am using.
Here is it's pdf file inside the attachment.

gizduino+ hardware manual.pdf (753 KB)

nevermind changing the pins for the atmega 644, i got it working already. :slight_smile:

but i didn't seem to get what I wanted with this approach. Although I can control it's volume now I get spiky volume output.

Here is my code:

#include <toneAC.h>

int senseMic = 0; //analog pin of gizduino //pin14
//int speakerPin = 5; //analog pin of gizduino

void setup(){
  //pinMode(senseMic, INPUT);
  

  Serial.begin(9600);
}

void loop(){
   //Serial.println(analogRead(senseMic));
  //delay(1000); 
  int mapData = 0;
  int val = analogRead(senseMic);
 // Serial.println(val);
  mapData=map(val,350,52,200,20);
  //If wind is blown
  Serial.println(mapData);
  
   if (mapData > 20 || mapData < 29) {
    //Play tone
    toneAC(300,5);
  } 
   else if (mapData > 30 || mapData <39) {
    //Play tone
    toneAC(300,6);
  }
     else if (mapData > 40) {
    //Play tone
    toneAC(300,7);
  } 
  
  //no wind is blown
  else{
    toneAC();
  }

}

I tried adding delay and erased the spiky output that i'm hearing but it is so slow on changing volume. hmm..

xxryan1234:
nevermind changing the pins for the atmega 644, i got it working already. :slight_smile:

but i didn't seem to get what I wanted with this approach. Although I can control it's volume now I get spiky volume output.

Here is my code:

#include <toneAC.h>

int senseMic = 0; //analog pin of gizduino //pin14
//int speakerPin = 5; //analog pin of gizduino

void setup(){
  //pinMode(senseMic, INPUT);

Serial.begin(9600);
}

void loop(){
   //Serial.println(analogRead(senseMic));
  //delay(1000);
  int mapData = 0;
  int val = analogRead(senseMic);
// Serial.println(val);
  mapData=map(val,350,52,200,20);
  //If wind is blown
  Serial.println(mapData);
 
   if (mapData > 20 || mapData < 29) {
    //Play tone
    toneAC(300,5);
  }
   else if (mapData > 30 || mapData <39) {
    //Play tone
    toneAC(300,6);
  }
     else if (mapData > 40) {
    //Play tone
    toneAC(300,7);
  }
 
  //no wind is blown
  else{
    toneAC();
  }

}




I tried adding delay and erased the spiky output that i'm hearing but it is so slow on changing volume. hmm..

You need to connect to the timer 1 PWM pins for your derivative Arduino device. The datasheet will tell you. I list the official Arduino pins, I can't list every Arduino derivative.

Your sketch is doing just what you tell it to do, which is not what you want. You need AT LEAST a 2 ms delay for a 500 Hz note to be played. The way you have it, there's not enough time to actually produce the note you're requesting. Basically, the problem isn't library, it's your sketch.

Tim

Debate this topic with the people who challenged me about it being 10v AC on this thread:

http://arduino.cc/forum/index.php/topic,141844.0.html

PeterH, PaulS & sbright33 specifically.

I don't see anything in that thread to support your assumption that they were disagreeing with what I said. Yes some questioned the -5V bit but no one said it was wrong. It does not generate -5V BUT the speaker "SEES" +/- 5V.

which I see you've modified from your post so it seems you're already questioning yourself.

No I haven't, I am explaining more of the theory to you. You seem to be latching onto words you don't fully understand.

50% is the loudest, but 25% will be lower volume.

No it sounds different but the peak volume is the same because the peak amplitude is the same. There are a different mix of harmonics in the 25% wave than the 50% wave so maybe your ears are not responding equally to the different harmonic mix. You seem to have a very simplistic view of an audio signal.

I don't have a horse in this race,

That saddens me, I would have thought you would want to understand what you have done.

Grumpy_Mike:

Debate this topic with the people who challenged me about it being 10v AC on this thread:

http://arduino.cc/forum/index.php/topic,141844.0.html

PeterH, PaulS & sbright33 specifically.

I don't see anything in that thread to support your assumption that they were disagreeing with what I said. Yes some questioned the -5V bit but no one said it was wrong. It does not generate -5V BUT the speaker "SEES" +/- 5V.

I suggest you read that thread again. Here's their quotes if you had a problem finding them:

PeterH:
So it gets 5V in alternate directions, instead of 5V one way and then 0V for half a cycle? In effect it's 5V AC rather than the 2.5V AC of the standard output. Yes, I can see how that would increase the output.

sbright33:
Or you could use a 10c transistor to amplify the 2.5v AC signal.

vasquo:
But I still don't get how you'll generate a -5 volts (with respect to ground), to achieve a +5V - (-5V) = 10V range

PaulS:
I'm missing something, I guess. How will two +5V to 0 to +5V pins toggling out of phase result in a -5V output?

These people are clearly saying that the tone library is normally 2.5 volts and toneAC can't be 10 volts. They're not being as argumentative as you, but they are challenging your argument. Also, if you read my comments you'll see that I was clearly stating that the speaker sees it as a +/- 5 volt signal. We're on the exact same side dude! But, they wanted to argue that it was only 5 volts, they couldn't understand that the speaker would see it as +/- 5 volts, and wanted to argue that the normal tone library was only 2.5 volts. I didn't care to argue so I didn't. But, it's obvious you're looking for an argument. This sounds like a great debate that you all can get involved in. Argue your differences with those that care instead of being a troll here, you're not helping this thread.

Grumpy_Mike:

which I see you've modified from your post so it seems you're already questioning yourself.

No I haven't, I am explaining more of the theory to you. You seem to be latching onto words you don't fully understand.

Yeah, I don't understand. Yet, I wrote the library and you didn't. How about you and the people listed above come to a consensus and then let us all know who doesn't fully understand. I, the guy who wrote the library, doesn't care which of you are more right. The concept is exactly the same, twice as much voltage is getting to the transducer, and it's therefore much louder.

Grumpy_Mike:

50% is the loudest, but 25% will be lower volume.

No it sounds different but the peak volume is the same because the peak amplitude is the same. There are a different mix of harmonics in the 25% wave than the 50% wave so maybe your ears are not responding equally to the different harmonic mix. You seem to have a very simplistic view of an audio signal.

Yeah, that's me, simplistic. The guy who wrote the library and tested the sound pressure level with a SPL meter (not my ears) and created a linear volume control for the library that works. Have you even tried the library? Have you listened to it with your ears? Or measured the output with a sound pressure level meter as I have? It doesn't seem you have, as if you did, volume level 1 and 10 are clearly quite a bit different in SPL, to my ears, yours, and to a meter designed to measure SPL. But, maybe your superior ears will hear volume level 1 and level 10 as the same SPL. You can then call my library worthless and delete it, I won't be offended. But, you probably shouldn't comment on something you haven't tried. Who knows, maybe you can still learn something? Unlikely, I'm sure, but you never know.

Grumpy_Mike:

I don't have a horse in this race,

That saddens me, I would have thought you would want to understand what you have done.

It would seem that you should want to know what I have done, not the other way around. I wrote the library on purpose, it wasn't an accident as you seem to want to imply. I didn't discover something, this is common knowledge, and I'm surprised that the tone library wasn't written to work like toneAC in the first place. Or, wasn't rewritten correctly by someone far smarter than myself, like you maybe?

Honestly, how about you just go troll somewhere else. I've already given you the people who have an alternate view and tried to express that we have the same view (speaker sees it as +/- 5v). If you're looking for an argument, take it up with them. Maybe after you can come back and let us know who got schooled. Can I express anymore how little I care who is right? I think it boils down to perception, or semantics at best.

Tim