Instructables
Picture of Arduino (Mega) Audio Recording
serial1.jpg
serial2.jpg

Record Audio to your Audino Mega SD card.

The audio file can be played back on a standard audio application or analysed byte by byte.

This Instructable will show you how audio input can be repeatedly added to a 512 byte buffer and then transferred to a SD card in real time. The period recorded can be altered.

The sample rate is 9.4 KHz and the wav file output 8 bit, mono. Whilst not hi-fidelity, the sound quality is perfectly adequate.

The recorded wav file can be saved as tabulated data. It can also be displayed as a simple scrolling graph on the monitor.

All files are time stamped using a unix time code sent from the serial monitor.

The inspiration for this article came from reading Amanda Ghassaei: http://www.instructables.com/id/Arduino-Audio-Inp...

.

My latest program update at the end of this instructable, increases the sample rate to 19 KHz with a significant improvement in audio quality.

.

You may also be interested in my instructables on:

A high speed Arduino Oscilloscope: http://www.instructables.com/id/Arduino-High-speed...

Playing wav files from the Arduino: http://www.instructables.com/id/Arduino-playing-wa...

.

Inevitably SD card technology has already improved. I have tested with a Sandisk Ultra 8GB sd card. This is significantly faster than the Kingston 4GB card I started with. Using my updated software I am able to record at 38.3KHz, with no degradation in quality. (4/8/2014)

 
Remove these adsRemove these ads by Signing Up

Step 1: Requirements

Picture of Requirements
53b71f91f92ea1d1c9000032.jpg
53b71edcf92ea1f268000094.jpg

Arduino Mega 2560

The following components work- alternatives may be viable (with program tweaking- I leave that to you!)

LCD Keypad Shield http://www.hobbytronics.co.uk/arduino-lcd-keypad-...

MicroSD Breakout Board Regulated with Logic Conversion V2

http://www.hobbytronics.co.uk/microsd-card-regula...

4GB Micro SD Memory Card http://www.hobbytronics.co.uk/microsd-card-regula...

Microphone pre-amplifier-

ac coupled with a potential divider to centre the voltage between Arduino 0-5 V rail

Amanda Ghassaei has published a circuit at http://www.instructables.com/id/Arduino-Audio-Inpu...

I designed my own with bass, treble and volume controls. However there are plenty of pre-amp designs on the web.

JohnL73 months ago

Thank you for posting this, this is a great project and you have clearly laid out all the steps.

In your comments on your latest version, it states that 32 is the fastest sampling rate, even though you left in the code for a prescaler of 16.

Have you been able to get any faster rates with lower prescalers?

DavidPatterson (author)  JohnL73 months ago

Hi,

The 32 prescalar works well with the latest high speed sd card, producing a sample rate of 38.3Khz on my arduino mega.

A prescalar of 16 should interrupt at 72Khz- this rate will not leave enough time for the buffer writes to complete. 512 bytes would be read in 512*1000/72000 mS:

7.1ms

This will almost certainly be less than the write time for the buffer at the current speed of software and sdcard.

So the interrupt will prevent the completion of the write- hence the degradation in observed frequency response. (As you observed)

I find the audio quality for 38.3Khz very acceptable-

Have you got it working?

.

Reducing the buffer size is not an option- according to best advice 512 bytes is the optimal block size.

A future development might be stereo- two preamps and a 512byte buffer comprising 256 left channel, right channel data pairs......

My follow up instructable detals how to play back the wav files from the sdcard- I have had no problem with files recorded at 44Khz. So 38.3Khz files will work quite happily.

JohnL7 JohnL73 months ago

The 16 prescaler actually slows down the sample rate compared to the 32 prescaler. With the 32 prescaler, i was showing ~38kHz sample rate. With the 16 prescaler, I was showing ~31.8kHz sample rate. Is this because of the interrupts?

thanks for posting this! a lot of great info here. Do you think it's possible to increase the sampling rate above 9.4kHz? is there a limit to how fast you can interface with the sd card?

DavidPatterson (author)  amandaghassaei4 months ago

I did a re-write this evening, utilizing double buffering.

I now have the frequency easily up to 19Khz, with very good clarity.

Further testing at higher frequencies is needed, but results are very promising.

DavidPatterson (author)  amandaghassaei4 months ago

I investigated sampling with pre-scalars of 16 (72Khz), 32,(36Khz), 64(18.6Khz) and 128 (9.4Khz). I have left the necessary code in the startad subroutine for those who wish to try.

Without doubt, the limiting factor is the 512 byte block transfer to the sd card.

I believe that it is unlikely that further optimization is possible within the block write section of the code.

Obviously, if it takes longer than the sample period to write a sd block, then the audio is missed (or the frequency perverted) and the sound quality collapses.

At present, 9.4Khz is the limit for my SDcard writer.

No doubt card writer technology will speed up.

I have read claims for faster (beta) versions for sd libraries. However I wanted to use the standard sd library.

Faster sampling is possible if memory is used as the buffer, rather than the sd card.

My best optimization for that system allowed for a 6000 byte buffer on a Mega.

At 38 Khz, only a fraction of a second can be read. This is no good for recording audio, but has applications as an oscilloscope / fast logger.

bergerab4 months ago

This is simply incredible. I love how you go over wav files. I always thought about this but I never thought it could actually be done with an arduino. Thanks for sharing

DavidPatterson (author)  bergerab4 months ago

Thanks for that.

Let us know if you you get it up and running.