Arduino Audio Player: Best tutorial for playing audio files!

From our STAR Maker : Sarvagnya Purohit

Arduino Audio

This Arduino project is one of the most simple and fun projects you can build with Arduino in 10-15 minutes. The result of this make will be an Arduino Audio Player which will play “.wav” files. It consist of a speaker, a simple transistor acting as an amplifier and a micro-SD card adapter with a micro-SD in it through which the .wav files are loaded and played.

What are the stuff required to make this?

Hardware:

  1. Arduino Uno (Any other Arduino except Duo will work)
  2. Micro-SD card adapter
  3. 8-ohms speaker
  4. Micro-SD Card
  5. Mini Breadboard
  6. BC 546b NPN transistor
  7. 7k Resistor
  8. Male-to-male jumpers

Software:

How does it work?

The Arduino in the below circuit loads the .wav files from the micro-SD card. It then generates a signal and outputs it through the speaker connected to digital pin 9. This makes the speaker create sounds and play music. It can play many different songs saved on the micro-SD card. In this tutorial, I have programmed the Arduino Audio Player to play the famous song ‘Rain Over Me”, by Pitbull and Marc Anthony.

The .wav files used in this circuit have a slight limitation in playing audio. As a transistor is used as an amplifier, it cannot read much complex .wav files. Therefore, the .wav files should be converted to:

-Samples Per second (Hz): 16000
-Channel : Mono
-Bits Per Sample: 8

Step 1: Converting files to much detailed .wav file format for your Arduino Audio Player

You don’t need any software to install in order to convert the songs to .wav files. There is an online music converter to do the work. The link is :  http://audio.online-convert.com/convert-to-wav

Follow the steps given below to make songs compatible with your Arduino audio player:

  1. Upload a music file or enter a link for the song or music file to be converted. You can even choose files from Dropbox or Google Drive.
  2. In optional settings, change bit resolution to 8 bit.
  3. Change sampling rate to 16000 Hz.
  4. Change audio channels to Mono.
  5. Click on Show advanced options.
  6. Select PCM format as PCM unsigned 8-bit.
  7. Click on Convert and the files are converted!

Arduino Audio

Step 2: Formatting a micro-SD card

  1. Download the SD Formatter tool from https://www.sdcard.org/downloads/formatter_4/
  2. Run the tool as administrator.
  3. Select the proper drive to format. Give the card your favourite name by writing it on Volume label and click on the “Format” button. It will prompt you a couple of dialogues. Click on them and your card will be formatted perfectly.
  4. Add the previously converted .wav files to the card and save them with simple names such as ’81.wav’.

Arduino Audio

Step 3: Preparing the circuit

Arduino Audio

MOSI – pin 11 on Arduino Uno/Duemilanove/Diecimila
MISO – pin 12 on Arduino Uno/Duemilanove/Diecimila
CLK – pin 13 on Arduino Uno/Duemilanove/Diecimila
CS – depends on your SD card shield or module.
Pin 4 is used here for consistency with this Arduino code

Step 4: Connect the Arduino and upload the code

Arduino Audio

The code is as follows:

#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(6);
tmrpcm.play("rain.wav");
}

void loop(){  }

Plug in the power and you have your Arduino Audio Player successfully playing Rain Over Me. Feel the happiness with the energetic starting lines, “Girl my party don’t lie”!!!!! :)

You can check the working demo below :


Thank You.

Sarvagnya Purohit-min

Sarvagnya Purohit – STAR Maker at DIY Hacking

About the maker :

Sarvagnya Purohit is a 13 year old passionate maker from Thane, Mumbai with much experience in Arduino and Raspberry Pi. He is a tech enthusiast and has interests in robotics, IOT, 3d printing, coding and Rubik’s cube solving.

Recommended Posts
Showing 30 comments
  • Jake Lachi
    Reply

    Hello. I’m doing a final project and we are using an SD card to play drum sounds. We are also using push buttons to play only one type of sound. I need some help with the code so that each push button plays a different drum sound from the SD card

  • Akhilan
    Reply

    can we play different types of audio files in different situations in program?
    example: when obstacle is detected at right side of IR sensor 1.wav as output..?
    when obstacle is at leftside 2.wav as output…?

    • Frit
      Reply

      It does not generate any music at all from sd card.NOTHING HAPPEN!!!

    • Ahmed
      Reply

      i have the same the question

  • debra
    Reply

    does it works with piezzo?

  • Marco
    Reply

    Nice post. Thanks a lot!

    • Sarvagnya Purohit
      Reply

      Welcome

  • W
    Reply

    Hi, I’m currently doing my final year project and my project’s objectives is to control the volume of the music by using ultrasonic sensor & paused the music when the distance of ultrasonic sensor is 0cm to 5cm. May I know how to code it? I used the same coding as yours to play my music but when I add my volume control statement into the code in the loop, the music won’t play anymore.

    • Sarvagnya Purohit
      Reply

      can you pm me?? i can give u the code

  • sai
    Reply

    hi,how to give my wave file in the place of rain.wave.

  • arbit
    Reply

    Thanks for this simple solution to play audio.
    One question: I am trying to play audio in a sketch with blinking leds.
    Can you recommend a sketch where the “Play rain.wav” command is part of the Loop, rather than the setup?
    I want to play different files as part of my loop.
    For example, if red light blinks, I want to play file 1, if green light blinks, I want to play file 2, etc.
    To do that, I need the play command in the loop, not the set up.
    Thanks again, and keep Making.

  • adibah
    Reply

    hi, i’m using arduino uno …i’m using ur code there but i have some problem with it…when i’m compiling the code…it turned like this..

    compilation terminated.for #include “TMRpcm.h”

    • Sarvagnya Purohit
      Reply

      You have to download that library

      • Jonii
        Reply

        how to download this library i tried many time but i couldn’t.kiindly help me i really need that

  • aa
    Reply

    bullshit, that’s not working

    • Sarvagnya Purohit
      Reply

      Try again. Believe me, it works …

  • Tejasurya Mouli
    Reply

    I’m getting static buzzing sound when i connect the speaker, what can be the problem here? pls help

    • Sarvagnya Purohit
      Reply

      Sorry btw for the late reply as I was busy in my 9th standard exams. You may have a problem in your files. Try step 1 properly again. The same happened with me first. Then I tried again. It will work :)

      • J
        Reply

        Hi, may I know why my SD keep failing? I have my connection all correct.

    • Sarvagnya Purohit
      Reply

      the white wire connected to pin 8 should be connected to pin 9 shown in the diagram ;) it was my mistake.

  • Derek States
    Reply

    I’m new to Arduino, but you say that the circuit will output audio through a speaker connected to digital pin 9, but, in the diagram, you don’t have anything connected to digital pin 9. Is there another step that I am unaware of?

    • Sarvagnya Purohit
      Reply

      Sorry btw for the late reply as I was busy in my 9th standard exams. the white wire connected to pin 8 should be connected to pin 9 shown in the diagram ;) it was my mistake. Thanx for pointing it out.

  • Kyle
    Reply

    I’m still a bit new to Arduino. Would an Arduino Nano be able to do this as well? And do you know of any way to play multiple audio files? Like, have a short audio file that plays at the press of one button and a different file that plays when you press another.

  • saad
    Reply

    hi .. can u please upload a clear crcuit diagram as i am new to arduino and cannnot understand the wiring . thank you

  • Chris
    Reply

    BC 546b NPN transistor? What is that? I cannot find it on Amazon.com. Is there some other name for this part? Thanks.

    • Sarvagnya Purohit
      Reply

      Sorry btw for the late reply as I was busy in my 9th standard exams. You can buy these specific value transistors in a local electronic, electrical, hardware, hobby, etc. shop but not on Amazon. You can find them on internet or Sparkfun as Amplifier Transistors. You can buy it at Mouser on the following link : http://www.mouser.com/Search/Refine.aspx?Keyword=512-BC546B
      Thank you for watching.

  • srp
    Reply

    Is there any way to play mp4 format in audrino

    • Sarvagnya Purohit
      Reply

      You mean “mp4”? Means even videos? You can rather do that by using a TVOUT library of Arduino through which you can get the video output and the above tutorial is enough for the audio part! Sorry btw for the late reply as I was busy in my 9th standard exams.

  • Ken L
    Reply
    • Sarvagnya Purohit
      Reply

      Thanx for that site.

Leave a Comment

32 + = 39

Start typing and press Enter to search