Using an Arduino to Play Custom Audio

Make it to the Right and Larger Audience


Using an Arduino to Play Custom Audio

The most basic audio waveforms can be generated using the “digitalWrite()” on an Arduino to create a square wave. Volume control of a square wave can be created by using analogWrite();. Frequency modulation of the square wave can be done using either interrupt based timers or a non-interrupt delay function. In order to create custom audio waveform to store and produce unique sounds such as vocals or music, we must be able to continuously vary frequency and amplitude. For this we will use a digital to analog converter (MCP4725) from Adafruit (


The MCP4725 includes a C++ library that can be easily integrated with an Arduino. This demo was done on an Arduino mega, but can be done on any arduino with I2C functionality. The MCP4725 is a 12 bit digital to analog converter which means it can resolve an amplitude range of 0 to 5 volts into 4096 levels, or bins. The sampling rate is dictated by two components – the speed of the Arduino and the actual sampling rate when the audio was recorded. The maximum speed at which the Arduino can sample and output sound is dependant on the amount of processes programmed on the microcontroller. It is also limited by the transfer rate of the I2C digital to analog converter. Through testing, it was determined that the audio should be sampled at a minimum of 8000 samples per second in order to maintain voice clarity.


The audio sample used for this demonstration was a series of voice recordings using Audacity. After opening Audacity, you can set the sample rate to 8000 Hz in the bottom left of the screen as shown below.

sample rate

Once the sample rate is set, ensure a microphone is available for recording. Press the record button in the top left of the screen as shown below.

record button

Press the stop button once you have completed your recording. You are now ready to export your audio file. Go to file -> Export Audio… within Audacity as shown below.

export audio

Export the file with a .wav format. Now we can use MatLab to parse the data as an amplitude value over time. The audio file is read by MatLab using the audioread() command. The amplitude vs time values are scaled between 0 and 4096 (212) because we are working with a 12 bit digital to analog converter. In the example code provided, we manually parsed the sample range for each word. Writedata.m was created to output the audio samples into a text file to easily add the audio samples to the Arduino board.


Start by loading your audio file (exported from audacity) into the MatLab working directory. Change the first line of code to include the name of your file from ‘testwords.wav’ to ‘YourFileName.wav’.


Run “ParseSound.m” to create the text file containing the audio samples. Copy and paste the text into another file named “soundData.h”. This header file is used to hold the audio information outside of the main script simply to reduce clutter. The Arduino code provided references this by stating “#include “soundData.h” at the top of the script.


The Arduino code may need to be tuned depending on the hardware setup of the MCP4725 digital to analog converter.  This DAC provides one hardware addressing pin. Depending on if this pin is brought high or low through a hardware connection. If this bit is set low, keep DAC.begin(0x62). If the hardware addressing bit is set high, you will need to change that line of code to read “DAC.begin(0x63)”. For this example, an Arduino Mega was used to communicate with the Adafruit DAC. A photo of the setup with A0 grounded is shown below.


File_000 (2)


A 5V and ground wire is being drawn from the Arduin. SCL and SDA are  connected to pins 20 and 21 on the Arduino mega. The loose wire shown above is connected to the output of the DAC. This can be used to connect to an amplifier/speaker or – as in this case – an oscilloscope. The connection to an oscilloscope is shown below.


File_001 (2)


After uploading the code, the output should automatically start displaying on the oscilloscope. A screenshot of the audio output is shown below. Yours may look different depending on the audio you choose to record.



If any issues arise when running this code, Adafruit provides example code for generating a sine wave. The example does not use interrupts, so the frequency is dependant on the processor frequency of the Arduino. The following oscilloscope screenshot shows the output of the Adafruit example for creating a sinusoid.




Author brief is empty


Contact Us

Thanks for helping us better serve the community. You can make a suggestion, report a bug, a misconduct, or any other issue. We'll get back to you using your private message ASAP.



Forgot your details?