#include <SD.h> //include SD module library
#include <TMRpcm.h> //include speaker control library
#define SD_ChipSelectPin 4 //define CS pin
TMRpcm tmrpcm; //crete an object for speaker library
void setup(){
tmrpcm.speakerPin = 9; //define speaker pin.
//you must use pin 9 of the Arduino Uno and Nano
//the library is using this pin
if (!SD.begin(SD_ChipSelectPin)) { //see if the card is present and can be initialized
return; //don't do anything more if not
}
tmrpcm.setVolume(6); //0 to 7. Set volume level
tmrpcm.play("eighty"); //the sound file "1" will play each time the arduino powers up, or is reset
}
void loop(){}
Hello,
I am trying to make Arduino play a audio file from a Micro SD card. I have used the above diagram but the code gives me this warning: ISO C++ forbids converting a string constant to 'char*', and when the code uploads, nothing plays.
I have converted the file 8 bit,16000HZ, mono , and wav file. "eighty.wav"
I have gone through other similar forums yet none of them could solve my issue.
You should add some Serial.print() statements to tell you that the initialization worked or did not
void setup(){
Serial.begin(115200);
tmrpcm.speakerPin = 9; //define speaker pin.
//you must use pin 9 of the Arduino Uno and Nano
//the library is using this pin
if (!SD.begin(SD_ChipSelectPin)) { //see if the card is present and can be initialized
Serial.println("SD card not detected");
//return; //don't do anything more if not
while(1);
}
Serial.println("SD card present");
tmrpcm.setVolume(6); //0 to 7. Set volume level
tmrpcm.play("eighty"); //the sound file "1" will play each time the arduino powers up, or is reset
}
You can also fix the warning by making the filename a PROGMEM constant with the F() macro [and also remove the comment since it is not true anymore]
Hey, Thanks for the prompt reply. I tried your suggestion for printing the initialization in the serial monitor, and it shows SD card is present, so I guess nothing is wrong in the card. I have also played these .wav files on my PC and they work fine. but when I tried using
tmrpcm.play(F("eighty"));
it gives me an error:
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Uno"
C:\Users\Admin\Desktop\CiiA codes\Voice_FIles\Voice_FIles.ino: In function 'void setup()':
Voice_FIles:20:26: error: no matching function for call to 'TMRpcm::play(const __FlashStringHelper*)'
tmrpcm.play(F("eighty"));
^
In file included from C:\Users\Admin\Desktop\CiiA codes\Voice_FIles\Voice_FIles.ino:2:0:
C:\Users\Admin\Documents\Arduino\libraries\TMRpcm/TMRpcm.h:31:7: note: candidate: void TMRpcm::play(char*)
void play(char* filename);
^~~~
C:\Users\Admin\Documents\Arduino\libraries\TMRpcm/TMRpcm.h:31:7: note: no known conversion for argument 1 from 'const __FlashStringHelper*' to 'char*'
C:\Users\Admin\Documents\Arduino\libraries\TMRpcm/TMRpcm.h:55:8: note: candidate: void TMRpcm::play(char*, long unsigned int)
void play(char* filename, unsigned long seekPoint);
^~~~
C:\Users\Admin\Documents\Arduino\libraries\TMRpcm/TMRpcm.h:55:8: note: candidate expects 2 arguments, 1 provided
exit status 1
no matching function for call to 'TMRpcm::play(const __FlashStringHelper*)'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I set this up - and it didn't work. . .
till I added the file extension!
(I was going to suggest this earlier, but I didn't see it in any examples.)
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
void setup()
{
tmrpcm.speakerPin = 9;
//tmrpcm.soundBuff = 500;
Serial.begin(19200);
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD fail");
return; // don't do anything more if not
}
tmrpcm.setVolume(6);
Serial.println("Let's Go");
tmrpcm.play("pipssd.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
}
void loop()
{
if(Serial.available())
{
if(Serial.read() == 'p')
{ //send the letter p over the serial monitor to start playback
Serial.println("ok");
tmrpcm.play("pipssd.wav");
}
}
}
Thanks, so much, this worked... But I'm getting distorted audio from my speaker... That would be an error in the .wav file right? because when I play this file on my pc it sounds just fine...
Possibly. The wiki doesn't talk-up the great audio quality. My trial is radio time pips, seemed 'ok'. The volume, so called, leaves much to be desired.
To be honest, the audio that is being produced is not even understandable... Anyways, thanks for your help, I will just retry creating the audio files and check, and once it starts working I will mark it as solution.
Thanks so much.