How to play WAV audio files with Arduino Uno and MicroSD card

A couple days ago I printed Yoda on my 3d Printer – http://instagram.com/p/U2HR__StrH/

Yesterday I put him on a remote controlled micro robotic platform, watch video of it in action here:

 

And today I made him speak.

The basics setup is this :

  • Arduino Uno.
  • MicroSD card reader (4$ at DX.com ).
  • MicroSD card (6$ on Amazon)
  • Speaker and a transistor to act as amplifier.
  • LED
  • 3D printed Yoda.

I found some Yoda WAV sound files online and encoded them from iTunes into 8bit 16Khz mono sound. Then put the files on the microSD card from the computer.

Put the MicroSD card into the card reader.

Hook up the card reader as follows:

  • 3.3v goes to 3.3v on the Arduino UNO (for power)
  • GND goes to Ground on Arduino UNO
  • D0 goes to pin 12 on Arduino UNO
  • D1 goes to pin 11 on Arduino UNO
  • CLK goes to pin 13 on Arduino UNO
  • D3 goes to pin 10 on Arduino UNO

Now you are able to read and write files to the MicroSD card.

The next step is to download this Arduino library to play WAV files and install it :

https://github.com/TMRh20/TMRpcm

Here is the code for my Arduino sketch :

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...

TMRpcm tmrpcm;   // create an object for use in this sketch
char mychar;

void setup(){

  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  tmrpcm.play("beware.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
}

void loop(){  

  if(Serial.available()){   
    mychar = Serial.read();

    if(mychar == 'o'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("helpyou.wav");
    } else if(mychar == 'r'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("chortle.wav");
    } else if(mychar == 'q'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("helpyou.wav");
    } else if(mychar == 'p'){
      tmrpcm.play("beware.wav");
    }
    else if(mychar == 'w'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("impresiv.wav");
    }
    else if(mychar == 't'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("seekyoda.wav");
    }
    else if(mychar == 'y'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("sensefear.wav");
    }
    else if(mychar == 'u'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("strongami.wav");
    }
    else if(mychar == 'i'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("whyhere.wav");
    }
  }

}

Upload that sketch to the Arduino after installing the TMRpcm WAV library.

All that’s left is connecting a speaker!

I found a spare transistor (2N2222) and hooked it up before connecting the speaker (you can also use a piezo speaker without transistor just to test if it works as intended),

Follow this schematic to connect the speaker to the arduino : http://blog.bryanduxbury.com/2012/01/one-transistor-audio-amplifier-for.html

My speaker is on pin 9 so do the same and you will be all set.

After connecting the speaker and uploading the sketch I open up serial connection terminal and send commands – letters from q to p on the keyboard, sending each character will play a different sound on the Arduino.

Now you can play audio files through your Arduino, on the cheap and with a lot of fun to come!

Here’s a shopping list for making a similar project:


You may also like

More in Blog, Code Blog
Login to Laravel web application from PhoneGap or Backbone

Login with AJAX - including PhoneGap apps Oh God. For a few weeks I was researching what is a good...

Close