Help my sound is bad

Hi i am trying to make a sound efect when i push a button but the sound comes out really bad. Does anyone know how to have clear sound?
I am using a 12w speaker and a 10w amp.
This is my code

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif
#define LED_PIN 6
#define LED_COUNT 20

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

#include <ServoTimer2.h>
#include <SimpleSDAudio.h>
const int buttonPin = 1;
int buttonState = 0;
int servoPin = 5;
ServoTimer2 Servo1;

void setup() {
#if defined(AVR_ATtiny85) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.show();
strip.setBrightness(50);
const int BUTTON_PIN = 1;
Servo1.attach(servoPin);
Serial.begin(9600);
pinMode(1,INPUT);

SdPlay.setSDCSPin(10);

if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))
{ while(1); }
if(!SdPlay.setFile("4.wav")){;}

}

void loop(){

buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {

colorWipe(strip.Color( 255, 0, 255), 4); // Purple
colorWipe(strip.Color( 255, 135, 0), 4); // Yellow

    SdPlay.play();

while(!SdPlay.isStopped()){;}
delay(100);

} else {

colorWipe(strip.Color( 255, 135, 0), 1); // Yellow

}

}
void colorWipe(uint32_t color, int wait)
{ for(int i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}

Please post a schematic of your project and describe in more detail what this means:

Sorry I don't have a schematic and the sound it makes is static.

So make one. It's going to be essential, I'm afraid.
Alternatively, some GOOD photos of your setup might also do the trick (but a schematic is better).

What does that mean?
Is it loud, or not?
Does it sound OK some of the time, but with pops or clicks?
Does it sound distorted like a screaming heavy metal guitar?
...etc?

It's loud. It never sounds ok. It just sounds like static.

What happens here is that everything in that block will be repeated (with a 100ms delay, so roughly 10 times per second) as long as the button is pressed.
Also google for 'button debounce'; a button often doesn't press cleanly from off to on, but flippers between these states before settling on a value. The Arduino is fast enough to pick up on that and alternate between both parts of your loop as the button bounces.

Interesting. 'Wait' isn't defined anywhere as far as I can tell. Does the code you posted compile? Is this the full code of your sketch?

This is synonymous to
iSdPlay.setFile("4.wav");
I.e. you're not really doing anything with the if-statement.

Looks like you tried to copy-paste code from different places together; nothing wrong with that, but whenever running into trouble with it, it's best to isolate what you're having trouble with and do just that. So make a sketch that only plays sound, just once, and does nothing else. See if you can get that to work properly. If so, you have a software/coding problem. If the problem with bad sound persists, look at hardware.

BTw, I'll bet you a dollar that you have both coding problems (see some above; there's more) and likely a hardware issue as well. Given your description of the bad sound, you're doing something wrong in hooking up the amplifier. But without a schematic or additional info, that's impossible to troubleshoot.

How do you want to proceed?

Here are some photos. (It is a big mess)




How do i make a schematic??

Did you try and google that? This looks promising:

There's likely more good videos & web sites on this.

Your photos illustrate the need for a schematic. And perhaps for some simplification. How about having just the arduino, sd card reader and power amplifier hooked up to a suitable power source and giving it a try that way.

Oh, btw:

You're not powering this from USB through your Arduino board, are you? If so, (1) you're not getting 10W from that into an 8Ohm speaker at least, and (2) you're (ab)using your Arduino board as a power supply, and it's not made for that.
I also see mention of one or more servo's in your code. Do not power those through your Arduino. Do not attempt to drive them directly from an Arduino pin either, unless they have a builtin-driver board that directly accepts microcontroller signals.

PS: you don't really need 10W of audio power, fortunately. Put that speaker into an appropriate enclosure and even 0.5W will sound pretty loud.

I played around with the audio file and got one to finally play but it is very quiet. Any solutions to that?

Yeah, see above.
Isolate just the audio circuitry and code.
Post a schematic.

How can i get 10W then?

Using an external power supply. Something like a 12V DC "wall wart" adapter (not a 9V smoke alarm battery). I assume you're vurrently powering all this through the USB connector of thr Arduino board although it's not on the schematic.
But you don't need 10W, and lack of power is not your primary problem here.

Good job on the schematic; can you confirm that the signal to the audio amplifier comes from Arduino pin 9? Because in that case, it's evident that audio quality will be horrible. An Arduino can only output digital signals. Your audio amplifier needs an analog audio signal. You'd need some sort of DAC to do what you want. Usually people go for an SD card module with integrated audio circuitry for this; I assumed that that's what you had.

Sorry for the very late reply

I can't because i need this whole circuit to be portable.

Yes but when it is all complete it will be powered using a 9V battery

Yes it is in pin 9

What does that mean???

What is that??

I have bought a DFPlayer - A Mini MP3 Player. Hopefully this will solve some problems.

Yeah, I think that's a good choice, in any case to get started. You can always make things more complex later on, but give it a go with this one first.

Uhmmm...I would advise against this. 9V batteries are made for very, very low-current applications where they need to last years, but don't have to do much work. An application like yours is pretty much the opposite: shorter run times are perfectly acceptable, but higher peak currents are common. Under these conditions a 9V battery will be the worst of both worlds: since it can't source peak currents very well, it drains very quickly and you'll likely be very dissatisfied with the lifetime you get on your battery.

It's a better idea to get a holder for 3xAA batteries and preferably use them with NiMH rechargeable batteries. These can source lots of current and last a long time. Moreover, the combined output voltage will be ca. 4.5V which can directly go into the +5V pin on your Arduino. As a consequence, no power is wasted in the onboard regulator on the Arduino. With a 9V battery, nearly half of the power is thrown away in the first place if you run it through the Vin pin on the Arduino.

So repeat after me: I will not use a 9V smoke alarm battery because I will be unhappy with it! :wink:

Analog vs digital signals: Analog vs. Digital - SparkFun Learn
A speaker needs an analog signal. An Arduino (at least the one you're using) cannot produce one. This explains your 'static'.

A chip (usually) that converts a digital value to an analog signal. Your DFPlayer has one integrated into it.

Thanks

Can i use 4XAA ??

I will not use a 9V battery and i will use 3×AA batteries.

Finally something good :relieved:

Well done :slight_smile:

Please let us know how it goes and if you run into any issues.

Even more good news: the module you bought is quite popular, so there must be many tutorials for it and if you have trouble, you'll find that your questions are probably already asked and hopefully answered on this forum. So keep your favorite search engine at hand, it'll come in useful!

Can i use 4 AA's?

Not easily; I would recommend 3 because it's a more or less direct fit with the 5V input on your Arduino. 4 x AA would get you something like 6V and that's a bit too high. You could bodge something with a series pass diode, but it's just easier (and less weight) to get 3 AA's. There are holders for 3AA's as well if that's the issue you're running into.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.