You are on page 1of 2

/* Tutorial for playing audio files

* Yuyang Yin 11/14/2018

* Required Hardware: 1.Interface SD Card Module


2.Mini Speaker
3.2GB MicroSD
4.SD Card Reader
5.Jump Wires
6.Arduino Uno

* Hardware connections:
Interface SD Card Module:
Connect SD Card Module VCC to 5V.
Connect SD Card Module GND to GND.
Connect SD Card Module CS to pin4.
Connect SD Card Module MOSI to pin11.
Connect SD Card Module MISO to pin12.
Connect SD Card Module SCK to pin13.

Speaker:
Use solder to connect two jump wires to the speaker jack.
One wire from the speaker jack will connect to GND.
Another wire frome the speaker jack will connect to pin9.

* The code is modified based on https://github.com/abhijitbrain/creative-


research/blob/master/_1mp3.ino
and https://github.com/TMRh20/TMRpcm/blob/master/examples/basic/basic.ino
*/

//Include libraries below


#include <SD.h> //include SD module library
#include <TMRpcm.h> //include speaker control library

#define SD_ChipSelectPin 4 //define pin4 as CS pin

TMRpcm tmrpcm; //crete an object for speaker library

void setup(){

//Define speaker pin. The speaker library is using pin9.

tmrpcm.speakerPin = 9;

// Set up the serial port:


Serial.begin(9600);

//See if the card is present and can be initialized.


if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return; //Don't do anything more if not
}

//The sound file "2" will play each time the arduino powers up, or is reset

tmrpcm.play("2.wav");

}
void loop(){

if(Serial.available()){

//send the letter a over the serial monitor to start playback

if(Serial.read() == 'a'){

// Set volume level. The range of numeber can be from 0-7.


// Set the volume as 5 at this time.
tmrpcm.setVolume(5); // Set the volume as 5.

// Play the file 3. The file 3 is audio for Red Light.


tmrpcm.play("3.wav");
}
}
}

You might also like