Arduino with speaker

I am using an SD card module to play audio using a speaker. But the volume of speaker is very low but in the code, I have given commands for maximum sound. The ratings of the speaker are 8ohm, 0.5W. The Arduino 9 no speaker pin is giving a maximum of 40mA to the speaker. Now, I want to know how to increase the volume of the speaker. I have also tried different types of amplifier circuits but didn't work out. The Arduino code for the project is given below:

#include<SD.h>
#include<SPI.h>
#include<TMRpcm.h>
TMRpcm tmrpcm;
#define SD_ChipSelectPin 4

int trigpin1 = 6;
int echopin1 = 7;
int trigpin2 = 3;
int echopin2 = 5;

float duration, distance, sensor1, sensor2, sensor3;

void setup()
{
  tmrpcm.speakerPin = 9;
  pinMode(trigpin1, OUTPUT);
  pinMode(echopin1, INPUT);
  pinMode(trigpin2, OUTPUT);
  pinMode(echopin2, INPUT);
  Serial.begin(9600);
  if(!SD.begin(SD_ChipSelectPin))
  {
    Serial.println("SD FAIL!");
    return;
  }
  else
  {
    Serial.println("SD OK");
    return;
  }
}

void loop()
{
  sonarsensor(trigpin1, echopin1);
  sensor1 = distance;
  Serial.print("Sensor 1: ");
  Serial.println(sensor1);
  if((sensor1 <10) & (sensor1 > 0))
  {
    tmrpcm.setVolume(6);
    tmrpcm.play("project.wav");
    delay(2500);
  }
  
  sonarsensor(trigpin2, echopin2);
  sensor2 = distance;
  Serial.print("Sensor 2: ");
  Serial.println(sensor2);
  if((sensor2 <10) &(sensor2 > 0))
  {
    tmrpcm.setVolume(6);
    tmrpcm.play("project.wav");
    delay(2500);
  }

}

void sonarsensor(int trigpin, int echopin)
{
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = 0.5*duration*0.0343;
  
}

The circuit diagram that I am following is:
ckt

You have probably damaged your Arduino pin by connecting a speaker like that. How did you measure the 40mA? If you used a multimeter, then you will not have measured the current accurately. It was probably double that 40mA and the pin is now permanently damaged, and perhaps other parts of the chip also.

For a louder sound, you need to use an amplifier of some kind. The simplest can be made from a single transistor, but better results can be had by using an amplifier chip. You can build a circuit yourself from an amplifier chip and some other components, or you can buy a small amplifier module.

An easier solution would be to use a dfplayer mini module. The SD card is inserted directly into the module and the speaker connected directly to it. The module contains an amplifier.

The 8 number digital pin is not responding actually but the 9 number is working (I have tested it in the morning). Yeah, I had measured the current by using a multimeter. I have used a lot of amplifier circuits, but the problem is the speaker was not working at all after adding an amplifier circuit. To be more specific, the amplifier circuit was not getting any power and I don't know how to solve this anymore.

The forum cannot help you with circuits that you did not share with the forum. Good luck.


when I am using this amplifier circuit the sd card module is not initializing. but when I am using only sd card module, it seems to be fine. can you please point out the problem here?
the code I am using right now to test the circuit is:

 #include<SD.h>
#include<SPI.h>
#include<TMRpcm.h>
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;

void setup() {
  tmrpcm.speakerPin = 9;
  Serial.begin(9600);
  if(!SD.begin(!SD_ChipSelectPin))
  {
    Serial.println("SD FAIL");
    return;
  }
  else
  {
    Serial.println("SD OK");
    tmrpcm.setVolume(6);
    tmrpcm.play("project.wav");
    delay(2500);
  }
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

5V / 8Ohm = 625mA while maximum permitted draw from a pin is 40mA. Dunno how much damage you inflicted on your board but if it doesn’t work as you expect now it could be just totalled

the 9 number pin is still giving 40mA. I have given connection a few times for a very little amount of time. can I consider that my Arduino board is still ok though the 8 number pin is not working?

Who knows, you burnt something inside the chip, I have no clue what damage this caused and whether or not it affected other functions

With a total resistance of 20+8R, the current drawn from the 12V supply will be around 430mA. The power dissipated by the speaker coil will be almost 1.5W, which is 3 times higher than its rating. I'm surprised you have not blown the speaker coil also. You need to get into the habit of doing some maths when choosing component values.

Give details about how the whole circuit is powered. The only reason I can think of for an attached circuit preventing the SD card from initialising would be a problem with the power supply.

Using "Vin" or the "barrel jack" instead of providing a proper 5 V supply?