Simple audio player

Play audio using an SD card

Introduction

This tutorial demonstrates how to use an Arduino board (Arduino Zero, MKRZero or MKR1000 WiFi) to play a wave file stored on an SD card using the AudioZero library and the 10 bit DAC.

Goals

  • How to play a wavefile stored on an SD card.

Hardware & Software Needed

Components to Build an External Audio amplifier:

  • LM386 (low power audio amplifier)

  • 10k Ω potentiometer

  • 10 Ω resistor

  • 2 x 10 µF capacitor

  • 0.05 µF or 0.1 µF capacitor

  • 250 µF capacitor

The Circuit

To connect a speaker to the board you have add an amplification circuit connected between the

DAC0
pin and the speaker. The amplification circuit will increase the volume of the speaker. There are many audio amplifiers available, one of the most common is the LM386. The following scheme shows how to build the circuit using the LM386 and a bunch of components.

You can supply the LM386 connecting the Vs pin with different voltages sources, like for example the +5 V present on the 5V pin of the Arduino Zero / MKRZero or an external 9V battery. The gain of the amplifier is given by the capacitor connected to pin 1 and 8 of the LM386. With the 10 µF capacitor the gain is set to 200, without the capacitor the gain is 50. With the potentiometer you can control the volume of the amplifier.

LM386 electronic schematic
LM386 electronic schematic

LM386 mounting on breadboard
LM386 mounting on breadboard

For Arduino Zero and MKR1000 you need to connect shield or module for an SD or microSD card with CS on pin 4.

The same circuit made with MKRZero and all the components on a single breadboard
The same circuit made with MKRZero and all the components on a single breadboard

For MKRZero, the microSD Slot is built in. A .wav file named "test.wav" is in the card's root directory. For a simple test you can attach a pair of headphones directly to ground and DAC0, respecting the polarity.

Warning: do not connect the speaker directly to the pins of the Arduino Zero or MKRZero.

Programming the Board

We will now get to the programming part of this tutorial.

1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. The AudioZero library can installed from Arduino IDE's library manager. To do this, open the Arduino IDE, go to Tools-> Manage Libraries. There you can search AudioZero and install the library shown. The 'more info' link will take you to the GitHub page which includes all the documentation for the library. For a more detailed explanation on installing and importing libraries see this tutorial.

3. Here are some of the core functions of the sketch:

  • AudioZero.begin()
    - Initializes the AudioZero library by specifying the target sample rate.
  • AudioZero.play()
    - Writes an audio signal read from the SD card to DAC0.

Audio File Format

The Audio file to store on the SD card must be in the .wav format with 88200 Hz, 8-bit unsigned PCM mono quality. This type of file can easily be created by software such as audacity.

Code

1/*
2
3 Simple Audio Player for Arduino Zero
4
5 Demonstrates the use of the Audio library for the Arduino Zero
6
7 Hardware required :
8
9 * Arduino shield with a SD card on CS4
10
11 * A sound file named "test.wav" in the root directory of the SD card
12
13 * An audio amplifier to connect to the DAC0 and ground
14
15 * A speaker to connect to the audio amplifier
16
17
18
19 Arturo Guadalupi <a.guadalupi@arduino.cc>
20
21 Angelo Scialabba <a.scialabba@arduino.cc>
22
23 Claudio Indellicati <c.indellicati@arduino.cc>
24
25 This example code is in the public domain
26
27 http://arduino.cc/en/Tutorial/SimpleAudioPlayerZero
28
29*/
30
31#include <SD.h>
32#include <SPI.h>
33#include <AudioZero.h>
34
35void setup()
36{
37
38 // debug output at 115200 baud
39
40 Serial.begin(115200);
41
42 // setup SD-card
43
44 Serial.print("Initializing SD card...");
45
46 if (!SD.begin(4)) {
47
48 Serial.println(" failed!");
49
50 while(true);
51
52 }
53
54 Serial.println(" done.");
55
56 // 44100kHz stereo => 88200 sample rate
57
58 AudioZero.begin(2*44100);
59}
60
61void loop()
62{
63
64 int count = 0;
65
66 // open wave file from sdcard
67
68 File myFile = SD.open("test.wav");
69
70 if (!myFile) {
71
72 // if the file didn't open, print an error and stop
73
74 Serial.println("error opening test.wav");
75
76 while (true);
77
78 }
79
80 Serial.print("Playing");
81
82
83
84 // until the file is not finished
85
86 AudioZero.play(myFile);
87
88 Serial.println("End of file. Thank you for listening!");
89
90 while (true) ;
91}

Testing It Out

After you have uploaded the code, your audio file should start playing, the audio file is defined in the code as

File myFile = SD.open("test.wav");
. To get additional information about the playback, open the serial monitor in the IDE by going to Tools > Serial Monitor. Remember that you can control the volume of the amplifier using the potentiometer.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • The wiring is incorrect.
  • Make sure there are no missing curly brackets {}.
  • You have not installed the AudioZero library.
  • You have not converted your audio file correctly.

Conclusion

In this example, we have learned how to create a simple audio player using the AudioZero library! If you want to extend this project, take a look at the Weather Audio Notifier tutorial to create a project that notifies you of weather changes.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.