Tone Generator

In this section we are going to build a tone generator using the Arduino Due, most of this comes directly from the Arduino site. We are also going to cover some basic circuit theory, and how to build a volume control circuit.

We will hook up a speaker to the Arduino Due, and make some noise using one of the DAC ports. The sketch we are using here can be found on the official Arduino site. It this just a way of making a simple function generator using the Arduino Due, you can choose between three wave types, a sine wave, a square wave or a triangle wave. I have not modified the sketch for this example, but we do need to modify the circuit. The DAC ports on the Arduino Due are not capable of driving a speaker, in fact you will most likely destroy the DAC if you try it. I recommend reading this forum post about damaging the Due. If you have an oscilloscope, you could test it with that, but I’m assuming you don’t.

We will need to hook up an amplifier circuit to drive the speaker. Later we can use this amplifier to build the theremin. This is also the best place to cover volume control. Volume control is a key part of a theremin, and its not as simple as it first sounds. Lets build the circuit and play with it a little, then we can discuss how it all works. First hook up a potentiometer ( something like 10k is good), hook up one leg to 3.3V, the other leg to GND, and the wiper to Analog Input 0. Then add a push-button as we did before when adding hardware interrupts in the LCD tutorial. Connect the button to port 4.

Here is the circuit diagram for the amplifier section, it is exactly the same as the Arduino page. I’ll explain a little more about the amplifier when we get to the section on volume control.

Next, hook up the 2nd potentiometer. Here I have connected PWR (pin 6) and GND (pin 4) for the LM386. Also I added the second potentiometer (10k), one leg is connected to GND, and the wiper is connected to the amplifier input (pin 3).

Next I added the 3 remaining capacitors. The 10 microfarad between pins 1 and 8 is what sets the gain to 200. 2 capacitors connect to the amplifier output (pin 5), one is a .047 microfarad ( I did not have a .05), this connects to ground through a 10 ohm resistor. The other capacitor is a 250 microfarad which will connect to the speaker.

Finally we need to connect DAC1 to the remaining leg of the potentiometer, and connect the speaker to the negative side of the 250 microfarad capacitor.

Now that the circuit is built, we need to upload the sketch, however we will need to create a library in order to use the “Waveforms.h” file. Actually you don’t need to do this, you can just copy the contents of “Waveforms.h” and paste them at the beginning of the sketch file if you want to. I have not yet covered how to make a library though and it is super easy. All you need to do is copy the contents of the “Waveforms.h” file and paste them into a simple text file (e.g. .txt), you don’t want to use the Arduino IDE for this, but rather something like Notepad or Wordpad ( if you are using windows). Then place your “Waveforms.h” file into a new folder named “Waveforms”, and place this folder in the libraries directory that is in your Arduino install. Mine is “C:\Program Files (x86)\Arduino\libraries”. Now paste the “Simple Waveform Generator” sketch into the Arduino IDE, then click on

Sketch->Import Library->Add Library

then navigate to the Waveforms folder, and click Open. Then click on

Sketch->Import Library->Waveforms.

You will see it add an include statement at the top of your file. ( you might have two includes for “Waveforms.h”, you can delete one of them). Here is the “Simple Waveform Generator” sketch

The main difference between this sketch and using an Arduino UNO or similar device to play a tone is that we are not using one of the digital outputs, but rather one of the analog outputs (DAC1, DAC2), which currently only the Arduino Due has. The way this example works is by using a hard-coded table to represent your waveform ( sine, square..). The tone is then played by running a loop which steps through the table, writing the value to the analog output, then pausing briefly.

When we get to building the theremin, we will also use a table of values to represent our waveform, but we will use a more sophisticated technique called DDS ( direct digital synthesis). I only used one of the outputs, this sketch can play two waves at once, and also allows you to change between four waveforms.

The other half of this circuit is the amplifier, we will use this same circuit for building the theremin. You can find this circuit several places on the web ( e.g. here). It uses the LM386 analog amplifier, which is a very inexpensive low power audio amplifier. The LM386 is ideally suited for small audio applications. The second potentiometer in this circuit controls the volume.

This volume control works by using the potentiometer as a voltage divider on the amplifier input. You hook one leg of the potentiometer up to the Arduino DAC output, and the other leg to GND, then hook the middle pin (a.k.a the wiper) to the amplifier input. Its exactly the same as when you hook up a potentiometer to one of the analog inputs to control something. This is easiest to explain with a small circuit that is equivalent to the potentiometer. As you turn the potentiometer. you are in effect changing the relative sizes of R1 and R2.

This is the perfect place to discuss how will we implement volume control for actually building the theremin, this is not as easy as it sounds, but first I want to discuss 3 general ways to control the volume with an amplifier circuit.

The first is the one we just did, place a voltage divider at the input of the amplifier, this allows you to control the amplitude of the amplifier input. A second way is to place a voltage divider at the amplifier output, so we can directly control the amplitude of the signal sent to the speaker. The third way is vary the gain of the amplifier circuit, the datasheet for the LM386 explains that you can vary the gain of the amplifier from 20 to 200 by placing a capacitor and resistor between pins 1 and 8. Which do we want? I decided not to use the third approach because the minimum gain is 20, so we won’t be able to reach zero volume just by use of the potentiometer. The first and second approach sound very similar, would there even be a difference? It turns out there is a pretty big difference, but its tricky to explain. I think its a great example though, since the concepts we need to understand are fundamental and you will use them over and over when tinkering with electronics.

First I need to mention a few disclaimers, we are working with a time varying audio signal, but all these calculations are for a DC signal, so technically these calculations are not correct, but I’m trying to convey some concepts, not exact numbers. Also when calculating current or power for a circuit, its good if your calculations are on the conservative side (i.e. overestimate the power you are actually supplying, and then building a circuit that can handle it). Having the DAC output a DC signal would be more power than you would ever get with an audio signal of the same amplitude.

First a little overview of current calculations. If you have two resistances in parallel like this

what is the equivalent resistance? A handy shortcut is “product over sum” so the equivalent resistance is

(10*15)/(10+15) = 150/25 = 8

in general

What if you want to know the current through the two resistors in parallel? If you know the voltage, then its just ohms law I = V/R. What if you only know the total current? Then its we need the following formula, this is commonly referred to a “current divider”.

Ok, now that we have a few basic tools lets take a closer look at manipulating the amplifier input vs manipulating the amplifier output. Imagine that you are really small and you are standing on top of the wiper of the potentiometer on the input of the amplifier.

pretend you can see the current flowing by. Say you look at R1 and see something like 4K, then you look at R2 see something like 6K, what kind of resistance do you see if you look towards the amplifier? It depends on the type of amplifier, and it can vary quite a bit from chip to chip, but it will be quite large. We say that the amplifier has a very large input impedance, this is why the rather feeble DAC output is able to drive the amp (i.e. a device with a large input impedance is easy to “drive”). The output of the DAC can not get higher than about 2.25 volts, and the datasheet says a typical input resistance for the LM386 will be 50k, so if we have the wiper all the way to one side ( assume a 10k potentiometer) so that R1 has zero volts and R2 has all 2.25 volts,then we have 50k in parallel with 10k, which represents an equivalent resistance of

(50k*10k) / (50k+10k) = 8.33 k

with 2.25 volts output, using ohms law we’ll have a total current of

2.25 / 8333 = .000270 A = 270 micro amps (uA)

now using the current divider equation, the amplifier will get

270 uA * ( 10k / 10k + 50k) = 45 uA

What if we turn the potentiometer so that R1 = 5k and R2 = 5k?

Now we have 5k in parallel with 50k which gives an equivalent of

(50 * 5)/(50 + 5) = 4.5k

This 4.5k (R2 in parallel with Ramp ) is in series with the other 5k (R1), so we get a total resistance of 9.5k.

Thus the total current is

2.25 / 9500 = 230 uA

and using the current divider equation, the current to the amplifier input is

230 * 5 / (50 + 5) = 20.9 uA

I hope that explanation was helpful, all I want to convince you of is that the current that is drawn by the amplifier is very small. Lets now consider the same scenario, but on the output side of the amp.

We have R1 and R2 ( assume a 10k potentiometer as before), but what do we see ( this is the key difference) when we look at the speaker? Most likely it will be 8 ohms, it could be 4 or maybe something else, but its going to be tiny compared to the amplifier’s input impedance. We do have a higher voltage since we have stepped up to a 5V supply (the amplifier is powered at 5V). We won’t actually get 5V, but like I said, its good to overestimate a little. What does our current divider look like now? Lets say the potentiometer is turned all the way to one side so that R1=0, R2=5k, and the speaker is 8 ohms. we can use the product over sum calculation from above and get the equivalent resistance.

(10000*8) / 10008 = 7.993 ohms

The voltage across the speaker will be the full 5V, and the total current is

5/7.993 = .625 A or 625 mA.

How much of this current goes to the speaker?

I = (.625) * 10000/10008 ~= .624 A, or more that 99% of the current.

That sounds ok as the Arduino Due can supply 800 mA through the 5V port. ( remember this is a DC calculation, the actual current will be much less).

Now lets turn the potentiometer a little more so R1 = 1K, R2 = 9K.

Using the product over sum formula, the equivalent resistance is 1k plus the parallel combination of 9k and 8 ohms.

72000 / 9008 = 1008 ohms + 1000 = 2008 ohms,

again the max voltage across the speaker will be on the 5V level ( its being amplified), and we have the following so the total current is

5/2008 = .0024 A or 2.4 mA.

We turned the potentiometer less than 1/10 of the way from full volume and the current has dropped by a factor of 260 . This is because our potentiometer is 10k, and the speaker is only 8 ohms.

One way around this is to use a much smaller potentiometer, I looked briefly and the smallest I could find was a 5 ohm potentiometer and it was over 10 times the cost of the small 10k ones that I already own. Even at 5 ohms this approach is not as good, at half volume, we’ll be dumping 40% of the amplifier’s output power into the potentiometer. There are some specialized applications where you would put the potentiometer on the output, ( e.g. very lower power device like some headphones) but it is far less common.

I said that the volume control for the theremin was not easy, but everything so far just amounts to putting a small potentiometer somewhere in the circuit, sounds easy! Its not so simple because we need to be able to run the instrument in “Training Mode”, and that means being able to control the volume with the Arduino, in other words make the Arduino turn the potentiometer.

Remember how we hacked the Servo so that it acts like a potentiometer, all we should have to do is take the wire we soldered to the servo’s potentiometer, and connect it to our amplifier circuit, then we can use the Arduino to write to the servo which will in effect move the potentiometer, and control the volume. In this approach the Arduino does not directly control the volume, but rather sets a servo position that is part of an external volume control circuit.

In theory this works just fine, but in reality we are using a hacked potentiometer, and don’t get full control of it, it only goes from .29V to 2.06V. So again we may not be able to reach zero volume with the potentiometer, and calibrating it might be difficult.

I know from experience that its hard to calculate exactly what the volume will be for a device that is cobbled together from various parts, it could end up being very faint or uncomfortably loud. I ended up taking a two part approach, the first part is to place a potentiometer in the input stage of the amplifier. The Arduino is completely unaware of this potentiometer, think of it as a “master volume” control. The second part is to use software to control the volume by varying the amplitude of the DAC output, this turned out to be very doable as the DAC has 12 bits of resolution or 4096 values, if we allow for 10 volume levels, that gives us 409.6 levels for the waveform. I’m getting ahead of myself though, we need to cover how DDS works before we go much further.