Sunday, August 19, 2012

Adding Audio to Arduino Projects

Sometimes a project just needs to be louder, whether its a synthesizer, alarm clock, autonomous robot or the RC Arduino lap timer.

In the case of the lap timer, I want people in the club house to know when a lap record has been broken, it all adds to the pressure and the fun of racing.

One incredibly simple solution to getting more sound from a micro controller is the LM386 series of amplifier chips. These can give project quality audio for less than a dollar.  They even make a passable one dollar MP3 docking station and are the basis of the 'little gem' guitar amplifier.


LM386-N4 Big Audio In A Tiny Package - 

Parts List - 
1 x LM386-N4
2 x 100uf Electrolytic Capacitors
1 x 0.1uf Capacitor
1 x 100Ohm Resistor
1 x 10K Potentiometer

Circuit pictured is used to drive a PC Speaker in the RCArduino Lap Timer project - See the video.


The LM386 Minimal Components Circuit

The datasheet for the LM386-N4 which I am using provides some example circuits but these require non standard capacitors - by non standard I really mean that most of us keep 'decade' capacitors meaning the tens - 0.01uf , 0.1uf, 1uf, 10uf, 100uf. The sample circuits require 250uf and 0.05 uf capacitors.

As I didn't have these values, I built the sample circuits with 100's in series and 0.1's in parallel to get 200uf and 0.05uf. After playing with the circuit for a while I removed the series and parallel capacitors leaving just a single 100uf and one 0.01 uf capacitor. This variation of the recommended circuit works perfectly well, and is now included in the built version of the RCArduino Lap Timer.

So for a super simple chip to add quality sound and volume to a project order a few LM386N4's its amazing how many projects can benefit from bigger sound when its as easy as this.


Simplified LM386N4 Circuit To Drive PC Speakers From A Micro controller

Caution : The new generation of 32 Bit ARM chips used by the Arduino Due are less able to sink and source current than the AVR Chips used in the 8-bit Arduinos. A number of users have reported burnt out DAC (Digital To Analog Converter) outputs while using the Arduino Due. It is suggested that a series current limiting resistor should be used between the Arduino Due DAC Outputs and external circuitry.

There are currently a number of topics covering the Arduino DAC outputs and pin protection in general on the Arduino Due forum - http://arduino.cc/forum/index.php/board,87.0.html

The circuit below shows a suitable circuit for 8-bit AVR Arduinos - UNO, Mega, Leonardo etc.



 Data sheet with alternative circuits and full application details -

http://www.ti.com/lit/ds/symlink/lm386.pdf

The RCArduino Five Dollar Synthesizer, Arduino audio played through the LM386 amplifier driving a PC Speaker

Project details here -
http://rcarduino.blogspot.com/2012/10/five-dollar-synthesiser.html

 


The Auduino
The five dollar synth is a great project if your up to re purposing an existing keyboard, if not, you really have to build an Auduino.

The Auduino is one of the best sounding Arduino Audio projects, it is also the easiest to build requiring only five potentiometers.
 
Update: The first video is my own Auduino, any others I post are enhanced Auduino's which for one reason of another are nicer than mine, so skip mine and have a look at what everyone else is able to get from this simple sketch through the use of clever additions -

My Own Audiuno - 
Totally standard Audiuno code with output direct to a PC Speaker using the LM386 Amplifier circuit shown in this post.


Auduino By DenkiTribe

A very musical Audiuno based jam -

The project enclosure seems to be closely related to a pizza box but we can forgive that for the very musical session. Modifications are obviously the stylus based pitch control and the addition of external echo.



Auduino By 'TheHangMansAxe'

As far as I can tell, thehangmansaxe has enhanced his auduino with two additions

1) An LED and LDR that 'gates' the output, for those of us with no audio background, gating is essentially connecting and disconnecting a signal from the output.

In DIY Projects LDRs are often used for this as they have an output which is a rough approximation to many stringed instrument where the initial note is loud but then decays away over time. LDRs initially react to light very quickly, but when the light is removed, they settle more slowly allowing the not to linger slightly.

At around the 1 minute mark the user shows the LED switching on and off through the side of the instrument.


2) Thermin style note sensing. The user does not provide any details, but I assume that the note is being controlled by infra red bouncing off the users hand. The original Audiuno design provided on tinkerkit uses five analogue inputs to control the sound, generally these are connected to puts but can also be replaced by any device capable of providing and analogue output.

UPDATE - 25/10/2012 - Added this section to explain how the Auduino works. It needs diagrams and rewritting for reduced length and increased clarity.


How does the Auduino work ?
While I am a big fan of the Auduino, its not that well documented. It is described as a 'granular synthesiser', I spent a lot of time reading up on granular synthesis and reverse engineering the code before I was able to understand exactly how the Auduino generates its particular sound.

A granular synthesizer is usually described as generating sound by rapidly repeating a small 'grain' of sound and if you read through the Auduino code you will certainly find repeated reference to grains however what and where are the grains ?

Outside of the Auduino project, the term grain is most often used to describe grains of sound which are sampled from real life speech, instruments or environmental sounds - a grain is a very short sample in the order of 1/10 to 1/10,000th of a second as opposed to the sampled vocal and drum tracks that you might be familiar with.

Pereshaped points out in the comments below that the synthesis technique used by the Auduino is closer to 'Vosim' that grain synthesis.
 
The Auduino code is actually very clever and efficient, instead of storing a grain the Auduino generates the grain in realtime using a simple counter.
The variables grainPhaseAcc and grainPhaseAcc2 are basically just counters. They count up, then down at a rate determined by grainPhaseInc and grainPhaseInc2. If you were to plot the value of these variables over time each one would give you a triangle waveform.

  // Increment the phase of the grain oscillators
  grainPhaseAcc += grainPhaseInc;
  grain2PhaseAcc += grain2PhaseInc;

The two variables grainPhaseInc and grainPhaseInc2 are directly controlled by two of the Auduino inputs. Adjust the potentiometers up and down and you will hear a frequency component of the output rise and fall in pitch as the relevant triangle wave increases and decreases in frequency.

These are not our main pitch control though, vary them up and down and while the quality of the note will change dramatically, the pitch of the note will stay the same.

The note pitch is controlled by the variable synchPhaseInc. This has an interesting job to do, it controls the rate at which yet another counter - synchPhaseAcc overflows. Whenever this counter overflows, it resets the the two triangle waveforms to an initial synchronized position. This periodic resetting of the two waveforms is what causes repetition of a repeatable 'grain' of sound. The rate of repetition gives the output its pitch.


Its actually a lot more interesting than that, what makes the Auduino sound so engaging is that as you increase or decrease the output frequency so you adjust the amount of the grain that is repeated adding additional layers of colour to the output tones.

The final stroke of genius in the Auduino design (its not my design so I am allowed to say this) is the use of the pentatonic scale. Instead of allowing you to choose any frequency you like the main pitch control is mapped to the musical scale know as the pentatonic scale. This is what gives the Auduino a kind of bluesy sound and ensures that you will never hit a duff note. For more on the pentatonic scale check out the wikipedia article.


A picture speaks a thousand works and Miro2424 has kindly posted this video of an Auduino in action on youtube. In the video you will see the two triangle waves superimposed on each other, you will see and hear how they are used to create both the pitch and the tone of the sound.


Auduino By Miro2424
I have been trying to learn how an 'addative', 'grain' or 'frequency on frequency' synthesizer like the Auduino works. This clip from Miro2424 shows the Auduino output visualised through what I am guessing is a high end PC Sound card. In the clip you can see the two triangular grains super imposed on each other and how they are used to create and vary the sound. Very happy to have found this, it makes it all easier to understand.




Everyone should have at least one Auduino, if you have a spare Arduino and 5 potentionmeters you can build one right now - http://code.google.com/p/tinkerit/wiki/Auduino

UPDATE - 01/02/2012
Here is another great variation on the Auduino by Moshang, this one also has the coolest name 'The Groovesizer' and also the best looking case of any I have seen so far.

The groovesizer extends the Auduino with a built in sixteen step sequencer.

Full details here - http://moshang.net/soundjeweler_blog/technique/groovesizer-diy-16-step-sequencer-and-synth/




UPDATE - 16/11/2012 - The Auduino is the original work of Peter Knight, the project home page appears to be inactive. RCArduino has previously reported a bug fix to the project which has not been updated. On the basis that the project is no longer active, a full version of the Auduino code including the bug fix and an added echo effect can be found on RCArduino -
http://rcarduino.blogspot.com/2012/11/auduino-with-delay.html
These two RC Arduino projects can also be built using identical hardware, upload them to your Auduino for a change of scene, you can always re upload the Auduino when your finished.

http://rcarduino.blogspot.com/2012/10/arduino-modular-synthesizer-part-one.html



Duane B

11 comments:

  1. Hello.
    Thanks for sharing your knowledge.

    There's a thing I don't understand: LM386 datasheet says that input voltage is +/-0.4V. But in your diagram you put a pot connected directly to a digital output of Arduino board (which sources 5V). Won't the LM386 be fried?

    Thanks!!!

    ReplyDelete
  2. Hi,
    In this case the potentiometer is acting as a simple voltage divider. If I set the voltage divider at the low end, most of the output voltage is being dropped by the potentiometer with only a smaller amount being routed out to the LM386.

    In one of my current projects I use the pot as an overdrive control, if I turn the pot up to send too much voltage into the LM386 it clips the waveform giving a harsh distorted sound which works nicely as an optional effect in audio projects.

    Overdriving the chip might shorten its life over the long term, but they are pretty tough in the medium term and by using the pot as a voltage divider you have the option of turning it down so that the chip is never over driven - the key thing is that the pot gives you the option to tune this in or out as you need.

    Duane B.

    ReplyDelete
  3. Oh, thanks a lot for this so fast answer!!

    What you say it's full of sense, but there's only one more thing that annoyies me...

    If LM386 accepts so low values of input voltage (+/-0.4V) and has a (minimum) gain of 20, the ouput voltage will be 0.4x20= 5V, which is the output voltage of an Arduino pin. So, which usefulness would the LM386 have in this case?

    Many thanks!!

    ReplyDelete
  4. Volts are only half the story, the LM386-N4 is a one watt amplifier so at 5 volts it can deliver 200ma, thats five times more power than an Arduino pin.

    It also acts as a buffer, if you connected an Arduino pin directly to an 8ohm speaker it would try to deliver 625ma which as you know is 15 times more than the maximum a pin can safely deliver.

    The amplifier provides power for the speaker and protection for the Arduino.

    Duane B

    ReplyDelete
  5. Oh, thanks a lot for your explanation!! Thanks!

    ReplyDelete
  6. Just for info what you are describing in the project is more "particle" formant synthesis rather than granular. In fact it is quite similar to VoSim. It is still a microsund technique and very nice indeed. Great project and thanks for sharing.

    ReplyDelete
  7. Hi, thanks for the comments, the project was originally built by a gentleman by the name of Peter Knight who described it as a granular synth. I am very much a novice in these things but it does seem that there is overlap between the different techniques. I have had some interesting sounds from a monophonic synth with variable arpeggio rates, at very high rates the notes blend into a composite waveform, I will be updating this post with details of the project - http://rcarduino.blogspot.com/2012/10/five-dollar-synthesiser.html although the blending of notes is not the main focus.

    Duane B

    ReplyDelete
  8. Hey there Duane,

    Thanks again for the great info here. I built the LM386 output circuit according to the schematic above and got it working right away (actually, my local only had the JRC386D, but same thing apparently). Just a question regarding the "seperate power" bit. I cheated and took 5v for the amp off the arduino board and it seems to work fine, but I guess there's a reason why seperate power is advised. I want to add the amp to a hackduino based pcb I'm designing for myself to play around with. I'm feeding the hackduino with a 5v 7805-based power supply. Would it be OK to feed the 386 directly from the 9v power brick that feeds the 7805 power supply (the 386 should be OK with 9v), or would it be best to have a second 7805 on the board to feed the amp?

    ReplyDelete
  9. Hi,
    The more powerful LM386 variants can draw a lot of current, so its better not to have them sharing the 5v power rail with the Arduino. You can connect them directly to the 9v input and should get a little more volume as a result. If you find that you get noise in the amp circuit from the 9v circuit you can try adding more decoupling capacitors or as you suggest another 7805 which is possibly overkill but will provide an effective,low cost and simple buffer against power source noise - I have used them as buffers in some RC Car circuits where motor noise was a problem.

    Duane B

    ReplyDelete
  10. Got it - thanks for the reply!

    ReplyDelete