Arduino projects_ digital audio recorder – apc

Being able to capture sound, store it and play it over and over again never fails to leave me in awe of its pioneers, from Thomas Edison to Alan Blumlein, the British electrical engineer who, in 1931, invented ‘binaural recording’ – what we now call ‘stereo’. Transfer function of high pass filter (Never heard of him? Blumlein amassed 128 patents in audio, radar and television that are still in use today, but tragically, was killed in a plane crash during World War II while testing airborne radar. High pass filter frequency His loss was considered so great, news of his death was kept secret until after the war).

So far in this series, we’ve turned an Arduino into a number of audio-related projects from a digital audio player to, most recently, an audio spectrum analyser. High pass filter calculator op amp This month, we see just how far we can push the popular microcontroller as we begin from scratch turning it into a basic but working digital audio recorder.

High pass filter eq How it works

No matter what they look like, all digital audio recording devices have to perform the same basic functions – they have to capture an analog audio signal at regular intervals while at the same time, saving the digital data to storage. High pass filter uses That might be easy for a PC or smartphone, but we’ll need to introduce some advanced programming techniques plus tap into some hidden features to get this working on an Arduino Uno.

For the record, our Digital Audio Recorder will capture a single (mono) analog audio channel with a sample rate of 22.05kHz, 8-bit sample depth and store it as a Windows WAV file with up to 4GB filesize on a microSD flash card. High pass filter dc offset Now before you yawn in excitement at those specs, remember, we’re doing this with a 16MHz processor, just 2KB of RAM and 32KB of programming space. Difference between high pass filter and low pass filter If only a Windows PC could be so efficient!

To help make the project (and source code) as easy to understand as possible, our recorder has just two buttons – record and stop. High pass filter photoshop It doesn’t play audio and only records to a single fixed file in the root folder of the flash card called ‘REC00000.WAV’. High pass filter phase An existing file with the same name will be overwritten. High pass filter 2nd order For playback, just take the flash card, load it into your PC, phone or tablet and play the file in any standard WAV file-ready media player or editor.

We all know digital audio – we all listen to music and we’ve all no doubt ripped a few CDs in our time. High pass filter transfer function magnitude But how do we capture an analog signal and turn it into digital audio?

This is where the work of another electrical engineer, Harry Nyquist, helps us out. Speaker high pass filter He figured out that in order to digitally capture an analog signal, we need to capture or ‘sample’ it at regular intervals, a rate which needs to be at least twice the highest audio frequency we want to capture. How high pass filter works That means if we want a 5kHz audio bandwidth for example, we need a minimum 10kHz sample rate.

The way we capture those samples is with a circuit device called an analog-to-digital converter (ADC) and the Arduino Uno’s ATMEGA328P microcontroller chip has one on-board. High pass filter with inductor But by default, it has a 9.6kHz sample rate and 10-bit sample depth, so we already have work to do to knock it into shape. Rc filter high pass For starters, the sample rate is too low (we’d only get a 4.8kHz audio bandwidth, which is AM radio quality at best) and the bit depth is the wrong size. High pass filter hpf (Bit depth is the sample precision, which is normally 16-bit in CD audio, but the Arduino’s ADC only has 10-bits to start with).

Still, the ATMEGA328P has a few tricks up its sleeve. High pass rc filter One of those we used in the Audio Spectrum Analyser project is an adjustable ADC clock prescaler. High pass filter applications Just like any CPU, the ADC requires a clock signal to synchronise its function and here, this is set by a programmable divider or ‘prescaler’, dividing the 16MHz Arduino master clock by a default factor of 128 to create a 125kHz ADC clock rate.

Because the ADC uses the ‘successive approximation’ sampling method (we looked at this in detail a few months ago), each sample takes 13 clock cycles, giving us a sample rate of 125kHz/13 or approximately 9.6kHz. What is a high pass filter But if we reduce the prescale factor, we can increase the ADC clock rate – a prescaler factor of 16 immediately cranks up the sample rate to nearly 77kHz, or a sample every 13microseconds.

But there is a downside – the ADC loses sample accuracy with increased clock speed, however, even at this higher rate, the accuracy is still close to 8-bit, which is all we need. Transfer function of a high pass filter This ‘overclocking’ technique works extremely well, but there is one other major limitation – the limited number of prescaler settings leaves us with audio-unfriendly samples rates of 9.6, 19.2, 38.4 and 76.8kHz, none of which are WAV format-standard.

Read through the ATMEGA328P datasheet and you’ll find that in addition to the normal ‘free-running’ sample mode we’ve been talking about, the ADC also has a ‘single conversion’ mode, whereby the ADC is enabled by setting the sampling register bit or ‘flag’, it grabs that sample and immediately resets the flag when the sample is ready for processing.

That mightn’t sound like cause for celebration, but when we combine it with another of the ATMEGA328P’s hidden talents called ‘timer interrupts’, we now have a mechanism for setting a much more precise sample rate. High pass filter and low pass filter Timer interrupts

In computer architecture, an ‘interrupt’ is a trigger to tell the processor to immediately divert from or ‘interrupt’ the current process and run a specific task associated with that interrupt. High pass filter low pass filter Once the new task is completed, the processor returns to the original process and picks up where it left off.

Now, the ATMEGA328P has all sorts of interrupt triggers to play with – you can trigger an interrupt externally by pulling an interrupt pin high or low as appropriate, but the chip also has a number of software-controlled options, one set in particular called ‘timer interrupts’.

In any CPU or microcontroller, a timer is just a hardware variable or ‘register’ that counts up to its maximum count (for example, 256 for an 8-bit timer), instantly drops back to zero and starts again. Transfer function for high pass filter Because timers run off the master clock and each count takes a fixed number of clock cycles, we can programmatically figure out how long it will take to reach the top count, hence the ‘timer’ name.

The ATMEGA328P has three of them – one 16-bit and two 8-bit timers – along with different ways you can use them. High pass filter cutoff frequency One simple way is once the timer reaches its maximum count, it can set an ‘overflow’ flag, which can be used to trigger an interrupt. High pass filter ic Like the ADC, timers also have a programmable prescaler for the input clock, so we can adjust how long it takes to reach that overflow condition.

But another far more useful option is a special mode called ‘Clear Timer on Compare Match’ or CTC. Rangkaian high pass filter Instead of waiting for the timer to reach its overflow point, we can choose our own – for example, rather than wait for an 8-bit timer to count to 256, we can load any number between 1 and 255 into a special ‘compare’ register and once the timer reaches that number, it triggers an interrupt, the timer instantly reverts to zero and counts again. High pass filter logic Using this technique to drive the ADC sampling, we can set the sample rate with far greater precision.

In our project, we use the chip’s ‘Timer2’ timer, switch it to this CTC mode and load an 8-bit register called ‘OCR2A’ with our ‘compare’ number to give us an interrupt every 45microseconds. High pass filter image That gives us a sample rate of approximately 22.19kHz – not perfect, but closer than anything else. 40 hz high pass filter Storing the samples

However, now we have these 10-bit samples turning up every 45 microseconds, we’ve got to do something with them. High pass filter youtube The first thing is to drop the two least significant bits (LSBs) and turn them into 8-bit samples – that’s relatively easy since each 10-bit sample is split and stored in two 8-bit registers ADCL and ADCH. Difference between low pass filter and high pass filter We program the ADC control register ADCSRA to give us just the top eight bits in the ADCH register.

But with only 2KB of RAM on-board (closer to 1KB by the time we run our code), the ATMEGA328P will still run out of space in a heart-beat – that’s where the microSD card module comes in.

We all know SecureDigital (SD) flash cards – they’re in everything from cameras to phones and tablets. High pass filter gain We load them into our PCs and they appear as yet another storage drive. High pass filter c code But when it comes to low-level hardware design like this, we need to understand a lot more about these tiny little storage devices.

All SD cards normally use a four-bit wide parallel interface to transfer data and achieve average write speeds beyond 10MB/second. Sallen key high pass filter calculator But the key word is ‘average’ – the actual write speed can vary considerably, depending on the latency or delay in writing data to the card.

Normally, your PC or device has sufficient RAM to buffer the data and smooth out the writing process so there’s no perceived loss in write speed. High pass filter garageband But the ATMEGA328P has two things working against it – it only has 2KB of RAM, but more importantly, it doesn’t have a 4-bit data interface. Guitar high pass filter Instead, we have to use the one-bit Serial Peripheral Interface (SPI) bus. High pass filter circuit design It’s the fastest interface the Arduino Uno has, but even running at 8MHz, it leaves us with an average write speed of only 150KB/second.

Since we only want to store 22.05 (22.2) KB per second, it sounds like we’re fine, but again, this SPI-bus write speed is an average. Digital high pass filter SD card storage is divided into blocks, each 512 bytes wide, so we have to write the data in 512-byte chunks – but that write process can’t interfere with the ADC sampling every 45microseconds. High pass filter design in matlab The problem is, average SD card latency in SPI mode is between 700-900 microseconds.

The simplest write method we could try is to just open up a file on the card, start grabbing samples, count them up as we go and when we hit 512, dump that block to the card using the sample interrupt routine. High pass filter for speakers But if we do that, we’ll still be writing the block when the next sample interrupt request arrives – and that’ll result in either new samples or the entire block being lost. Corner frequency high pass filter Twin buffers

We solve this problem using a technique commonly found in digital audio design called ‘double buffering’ – we set up two 512-byte blocks of RAM called ‘buffers’, labelled ‘buf00’ and ‘buf01’ in our code. High pass filter experiment As we start recording samples, we store them in the first buffer. High pass filter calculator passive When that buffer is full, we switch over to the second buffer; meanwhile, the first buffer is now saved to the flash card. High pass filter in photoshop The 512-byte buffer size gives us roughly 22 milliseconds to get that first buffer written to the flash card.

The 900-microsecond average latency for SD card writes via SPI is way too long to handle inside the 45-microsecond sample interrupt process, but with 22 milliseconds per buffer to play with, we can get the ATMEGA328P to multitask and write the block in between ADC samples.

Once the second buffer fills up, we switch back to loading up the first buffer again that has now been saved and meanwhile, start saving the contents of that second buffer in the next 22 milliseconds. Second order high pass filter What we end up with is this constant swapping – we’re storing samples in one buffer while writing the other buffer to the flash card. Low pass filter and high pass filter pdf But on the card itself, we end up with a seamless stream of audio data, all done with just 1KB of RAM. Simple high pass filter (In reality, the Arduino’s SD library grabs its own 512-byte buffer, but without our double-buffering, this project wouldn’t work). High pass digital filter Listen to history in the making

We’ve been recording sound since Thomas Edison invented the tinfoil system in 1877. High pass filter calculator speaker Check out cylinder recordings. High pass filter matlab image processing Alan Blumlein’s stereo test recordings from 1933 have also been preserved and restored by the British Library. High pass filter graph Listen online here.

So if you comment out that line it means it’s no longer storing, am I reading that right? Buzzing can come from a number of sources, so here’s what I’d do. Rc high pass filter derivation First, make sure your wiring is solid – check that all of your ground lines are grounded.

If the buzz goes by commenting out that line, it suggests you have your analog ground line on the wrong side of the SDcard ground, by which, you probably have this arrangement:

Basically, it sounds like a ground-loop problem whereby the return power path of the SDcard is entering the ground path of the analog audio input before heading back to USB GND.

Physically follow the ground plane path from your analog input ground to the USB connector and make sure you’ve got the order right. High pass filter schematic (Mind you, it is only 8-bit so there will be some minor hiss as a result).

Thanks very much for your help. High pass filter formula i switched power to usb from my android phone charger and the buzz is gone. High pass filter fl studio There is only the expected hissing you mentioned. High pass filter pioneer It must have been the ground loop problem you described. High pass rc filter calculator That would make sense given that both the power and analog input were from the same computer.

This is a very cool project and has been a great learning experience for me. Gimp high pass filter One thing i’m wondering about is if you think the wear leveling algorithm in the sdcard could slow down some of the 512 byte block writes enough to cause a timing issue in the sketch code? I did some tests where i sampled the timing of several hundred 512 byte writes. High pass filter 80hz Most were in the 768uS range, but there were also a few in the 6-9mS range. High pass filter excel Any ideas about what might cause such a bump in the time? Used the same initialization code from your project for those tests.

No, what tends to occur is that there is a seek latency, but actual writing is comparatively quick. Butterworth high pass filter design The microSD card module here runs via SPI (serial peripheral interface) and from memory (I mean ‘my memory’), I’ve set it in code to run at 8MHz, or basically as fast as it can go. Design a high pass filter Yes, 8-bit SDIO would’ve been ideal, but SPI is the best an Arduino Uno can do.

However, that said, my research in this project was that there are occasional latencies that exceed 22ms, which does result in lost samples, but surprisingly enough, those losses are quite rare. High pass filter rc circuit Obviously, the higher the audio sample rate, the more of a problem this is. 80hz high pass filter I start with a clean formatted card and the results are pretty good. Design high pass filter It tends to come down to how well internally the flash card reclaims dumped blocks as to when and how often those extended latencies seem to occur.

There is a later version of this project using an Arduino MEGA – the larger 8KB cache of RAM on the ATMEGA2560 overcomes this issue to a large extent. How to use high pass filter But as I said before, the sample losses were only occasional.

Still, if you’re not dealing with audio recording, I’d imagine you’d be able to slow the sample rate down, in which case, this becomes less of an issue. High pass filter using op amp In testing, I saw occasional seek latencies of up to 70-80ms, but they seemed quite random. High pass filter algorithm If you’re happy to use the default ADC rate of around 8.9kHz, I can’t see it being a problem.

Meanwhile, I’ve begun looking into 32-bit ARM Cortex M3 boards featuring the STM32F103C8T6 in particular – these have 20KB of RAM, which makes things easier to deal with. High pass filter diagram I did say somewhere that audio recording pushes the Arduino to its limit – and mostly that limit revolves around the squinchy 2KB of RAM.