Controlling volume from speakers

Hi I have a project to be made that would involve a wind sensor and a speaker. Is it possible to control the volume of the speaker through the data it receives from the wind sensor and if it is possible can you guys please tell me how to do it? :slight_smile:

For example: Soft Blow = Soft sound output
Medium Blow = Medium sound output
Hard Blow = maximum sound that the speaker can produce

I would like the volume output of the speaker would depend on the strength of my blow through the wind sensor is this possible?
Thanks. Ryan :slight_smile:

You could use a digital pot to controle the volume of a sound signal:-

Or do you want to generate the sound as well?

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.

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