When I try to play music with my Arduino Mega with a speaker and SD card reader it buzzes a lot and makes noise. I used .wav files: 8-bit, 16000hz, mono, unassigned 8-bit. I tried to connect it to my earbuds and same thing. So the problem is not with the speaker. I also tried to play the .wav file on my computer and it was good quality. Note: the music still plays, but bad quality. (Please let me know if it's supposed to be bad quality).
My connections:
SD card:
CS: digital pin 53 on Arduino Mega
SCK: digital pin 52 on Arduino Mega
MOSI: digital pin 51 on Arduino Mega
MISO: digital pin 50 on Arduino Mega
VCC: 5 volts on Arduino Mega
GND: Ground on Arduino Mega
Speaker 4 ohms 3 watts:
Positive: digital pin 46 on Arduino Mega
Negative: ground on Arduino Mega
Those are all the connections.
//include libraries
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
//choose what pin your SD card is connected to
#define SD_ChipSelectPin 53
//choose the name for your speaker
TMRpcm musicplayer;
//void setup
void setup(){
//choose what pin your speaker are connected to
musicplayer.speakerPin = 46;
//start serial monitor at baud rate of 9600
Serial.begin(9600);
//if your SD card is not connected, the serial monitor will print "SD fail"
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
//start playing your music
musicplayer.setVolume(6);
musicplayer.play("mymix.wav");
}
//nothing happens after the void setup
void loop(){ }
It sounds like ground or line frequency noise. Are all of your low level signals twisted pairs or shielded? When using twisted pair if it is not a differential signal one of the leads should be ground connected only at one end same as the shields if they are used. I assume you are using an external power supply for the audio portion, we all know an Arduino Does NOT a Power Supply make. If this does not help post a schematic showing all connections, power etc, not a frizzy drawing.
You must not drive a speaker directly from a logic signal, the Mega outputs can handle upto a few tens of mA only. A 4 ohm speaker driven from 5V is trying to pull 1.25A
You are vastly overloading your pin.
Also speakers should not see DC, the coil will ram up against the end-stops if there is significant DC offset.
To drive a 4 ohm speaker requires some sort of amplification, and DC-blocking.
Hi,
A simple but effective DC blocker is just a capacitor in series with the speaker. It would also reduce the volume though. You could use this tutorial of a simple MOSFET audio amplifier. DIY Audio Player with Speaker - Hackster.io