How to Make an Audio Player with Speaker Using the Arduino Uno!

By Sarvagnya Purohit

Arduino Audio

 

This Arduino project is a simple and fun project you can build in 10-15 minutes. The result of this make will be an Arduino audio player that plays “.wav” files. It consists of a speaker, a simple transistor acting as an amplifier, and a micro-SD card adapter with a micro-SD card in it where the .wav files are loaded and played.

Required Materials

Hardware:

  1. Arduino Uno (Any other Arduino except Duo will work)
  2. Micro-SD card adapter
  3. 8-ohm 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 circuit shown below 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 ‘Rain Over Me”, by Pitbull and Marc Anthony.

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

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

Converting files to .wav file Format for Your Arduino Audio Player

You don’t need to install any software in order to convert the songs to .wav files. There’s an online music converter to do the work.

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 audio 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. Set the PCM format as PCM unsigned 8-bit.
  7. Click on “Convert” and the files are converted!

 

Arduino Audio

Formatting Your Micro-SD Card

  1. Download the SD Formatter tool.
  2. Run the tool as administrator.
  3. Select the proper drive to format. Give the card your favorite name by writing it on Volume label and click the “Format” button. It will prompt you with 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

Preparing Your 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

Connecting the Arduino and Uploading 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 play Rain Over Me. Feel the happiness with the energetic starting lines, “Girl my party don’t lie”!!!!!

You can see the working demo in the video below.

 

 

 

 

 

Recommended Posts
Showing 53 comments
  • Balakrishna
    Reply

    We tried converting the audio to .wav but we couldn’t get the sounds right.. Should we compulsarily convert it to PCM unsigned 8-bit format? we tried doing that in the website mentioned in the link but that option was not available anymore. could u suggest a different option for that? Thanks in advance

  • Kritkorn Supyen
    Reply

    Please help me.I done every thing you told but why it always said “SD fail”.

  • Dharma Teja
    Reply

    Hi I have done this. Everything is fine but I am getting a low sound out from speaker . So when I measured the voltage passed for speaker it is very low in my case 0.5v. Please help me I want loud sound.

    • Sarvagnya Purohit
      Reply

      Try using MP3 shields for Arduino. They work with good quality of output.

  • Arthur
    Reply

    Is it important to have this exact transistor model? I’ve NPN transistor in my kit I hope it’ll work!

    • Sarvagnya Purohit
      Reply

      Its not important unless and until you have a NPN transistor.

  • pranitha
    Reply

    hi i tried to do all the process you described but im getting error as follows when compiled can you help me with this

    C:\Users\user\Documents\Arduino\sketch_mar28a\sketch_mar28a.ino: In function ‘void setup()’:

    C:\Users\user\Documents\Arduino\sketch_mar28a\sketch_mar28a.ino:17:21: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    tmrpcm.play(“81.wav”);

    ^

  • Rebecca
    Reply

    Hi we’re following your instructions for a school project. Is there a way to make the code loop? We tried moving the 2 lines of code ” tmrpcm.setVolume(10);
    tmrpcm.play(“1.wav”);

    into the loop, but it’s not working. Any advice?

    • Sarvagnya Purohit
      Reply

      tmrpcm.setVolume(10); is absolutely WRONG. The highest volume you can set in tmrpcm library is 7 and its impossible you can get compiled. I don’t think you got it compiled rightly. Try setting it tmrpcm.setVolume(10); to tmrpcm.setVolume(7); . It’ll work.

  • John T
    Reply

    Serial monitor shows “SD fail”. I’ve followed everything exactly. Code compiles but with the warning “deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]” and there is no sound. Would appreciate some help.

    • Sarvagnya Purohit
      Reply

      Hey John, I had even faced the same difficulties when I had started. You can email me([email protected]) or Pm me of fb so that I can solve ur problems. Its a quite difficult step.

      • Akshay
        Reply

        same problem here please help …. emailed u already

    • Sara
      Reply

      I have the exact same problem. How did you guys solve it?

    • NITHYAGANESHAN THAMOTHARAN
      Reply

      i have the same prob how do i solve it pls

  • Robert
    Reply

    Hi Sarvagnya,
    I was wondering if you could help me. I am building an r2d2 themed electric skateboard. I have a battery pack that would provide power to the motor, and it has a usb port. I would like to use that usb port to connect a music system (ideally a soundboard with an sd card and a small speaker /with bluetooth connectivity) so that when I turn on the skateboard it makes a welcoming r2d2 sound. I have no experience in coding or doing any of these things, hence why i’m here asking for your help :) I was thinking whether to purchase this small bluetooth speaker with sd card capability https://www.aliexpress.com/item/X3-Wireless-Mini-Bluetooth-V2-1-Speaker-Portable-Speaker-for-Mobile-phone-and-TF-Card-with/32655452896.html?spm=2114.30010308.0.0.KQFdJc , remove the plastic , revealing the raw electrics and connecting this to the skateboard battery. I understand that I would somehow need to program the speaker so that when it turns on it plays the r2d2 sounds from the sd card and once it finishes playing it turns off the speaker. Could you tell me whether its possible to do this with the speaker i sent in the link, or do you advise buying everything seperately and building it. If you advise building it could you recommend the stuff that I would need to build it?

    Many Thanks

    Rob

    • Sarvagnya Purohit
      Reply

      Hey Robert,
      Your project is quite interesting. I think it wouldn’t be the perfect place here for us to talk about it. You can email me([email protected]) or contact me on facebook.

  • Ilan
    Reply

    Hi Sarvagnya,

    Is the music quality mainly dependent on the speaker, or is there no way of getting a better sound quality with an Arduino as audio
    player? thanks

    • Sarvagnya Purohit
      Reply

      Actually we used a transistor as an amplifier. You can use MP3 shields for better quality. for example those adafruit mp3 shields.

  • corado
    Reply

    how can i use two speakers to play different types of audio files??

  • Chaitanya
    Reply

    Good Evening Sarvagnya,
    I was referring your material for my project work, i am able to dump the code on controller the code is running successfully but i am unable to get audio output.

    • Sarvagnya Purohit
      Reply

      Did you convert those .wav files according to the instructions? If yes, then try connecting that white wire in the circuit diagram to pin 9 rather than pin 8.

  • 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

− 1 = 1

Start typing and press Enter to search