Question

I've got the code to play a little sound from an arduino. The song has been encoded from numeric values. but how do I make it play louder from the arduino?

#include <PCM.h>

int switchPin = 8;

const unsigned char sample[] PROGMEM = {
  140, 124, 130, 126, 129, 126, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 127, 128, 128, 128, 128, 
};

void setup()
{
  pinMode(switchPin, INPUT);
}

void loop()
{
  if(digitalRead(switchPin) == HIGH)
  {
    startPlayback(sample, sizeof(sample));
  }
}
Was it helpful?

Solution

This sample code uses a subroutine that uses a PCM technique to control the intervals that the digital pins are turned on and off (and assuming that those output pins are connected to a simple speaker). Remember that sound's frequency is what makes the tones and the volume is the amplitude of those frequencies. So what you are asking is, to make a sound louder, how to increase the amplitude of the signal. But since this simple demo is only capable of changing the time the digital signals are toggled (frequency) and not the amplitude (a bit is either On of Off. a bit is never "REALLY ON" [caps denote yelling here :-) ] ) so your only choice here is to add another layer of hardware between the Arduino and the speaker to increase the tone's amplitude - aka an amplifier. This can be something simple like an opamp as described from this design tutorial or replacing the speaker with a jack to the input of a boombox.

OTHER TIPS

Audio signals are inherently signed quantities. If your library uses unsigned char for audio data, then it's probably mapping 0 -> -128 and 255 -> 127. So let's look at your signal.

The first 6 samples:

140, 124, 130, 126, 129, 126

are a slight oscillation around 127. Then the rest of your signal

128, 127, 128, 127, ...

is more or less DC. It's going to be inaudible.

Try feeding in a signal of {0, 255, 0, 255, ...}, it should be much louder.

If you read the comments in pcm.h, you'll see it says the volume will be very low. the PCM technique adjusts the percentage of time that the pin is high. I concur with jdh's recommendation that volume will be limited no matter what and an amp would help. For something louder in software, I'd think you'd want more of a square wave with an adjustable frequency, so that the pin is high as often as it's low, and the actually frequency adjusts. I haven't used it, but take a look at the arduino Tone() library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top