TMRpcm Library: Using another speakerPin

Hello everyone,

I want to programm a small music player on my Arduino Uno.
I'm reading .wav-files from a SD Card and put them out on a speaker. Everything works fine and I'm happy with the result, but there is one Problem:

I'm using the TMRpcm Library, and wehen using an Arduino Uno, you have to choose digital Pin 9 as the speakerPin. But in my case Pin 13-9 are used for a 2.8" TFT Display.

Is there any possibility to change the speaker Pin to other Pins, for example 3, 5 or 6?
I already wanted to change the library, but I really don't know what to do there :roll_eyes:

Thank you very much for every answer you can give me.

Greetings, Wackala

In the library I found this piece of code:

void TMRpcm::setPin(){
disable();
pinMode(speakerPin,OUTPUT);

#if !defined (USE_TIMER2) //NOT using TIMER2
switch(speakerPin){
case 5: tt=1; break; //use TIMER3
case 6: tt=2; break;//use TIMER4
case 46:tt=3; break;//use TIMER5
default:tt=0; break; //useTIMER1 as default
}
#else //Using TIMER2 only
tt = 0;
#endif

#if defined (SD_FULLSPEED)
SPSR |= (1 << SPI2X);
SPCR &= ~((1 <<SPR1) | (1 << SPR0));
#endif
}

That's where the speakerPin has to be selected, or am I wrong?
What do I have to change?

Greetings, Wackala

Change the statement where speakerPin is defined. If your code compiles it is defined because of your statement: pinMode(speakerPin,OUTPUT);

Thank you for the reply!
Here is the short example code I used:

#include <TMRpcm.h>
#include <SPI.h>
#include <SD.h>
#define CHIP_SELECT_PIN  4
#define SPEAKER_PIN      9



TMRpcm tmrpcm;

void setup( ) {
    Serial.begin( 9600 );
  tmrpcm.speakerPin = SPEAKER_PIN;
  
  while ( !Serial ) /*mt*/ ;
  if ( !SD.begin( CHIP_SELECT_PIN ) )
    Serial.println( "SD fail" );
  else {
    tmrpcm.play( "AUFST2~1.WAV" );
    delay( 5 );
  }
}

void loop( ) {
}

Changing to any other PWM Pin (3, 5 or 6) by changing this line:

#define SPEAKER_PIN      9

or this line:

tmrpcm.speakerPin = SPEAKER_PIN;

disables Pin 9, but doesn't activate the wished Pin.

I don't know what I'm doing wrong...

I don't know what I'm doing wrong...

Basically you are trying to follow Gill’s incorrect advice.

You can not change the pin because it is connected to an internal timer and that timer is used to generate PWM to generate the analogue output.

It is far easier to change the pin on your display.