This project uses an Arduino Uno and a potentiometer to create a variable-pitch tone.
- In Arduino Note Generator I update the project to play discrete notes.
These are the key Arduino tutorials for the main concepts used in this project:
If you are new to Arduino, see the Getting Started pages.
Parts list
- Arduino Uno
- 8 ohm speaker or old headphone speaker
- 1x 220 resistor
- 1x 10k potentiometer
- Breadboard and jumper wires
The Circuit

The potentiometer’s signal pin (usually the middle one) is wired up to the Arduino analogue A5 input directly, with the other two pins to +5V and GND respectively. This means that the signal will read between 0 and +5V as the potentiometer turns.
You can use a salvaged speaker from an old stereo or TV or salvage a smaller speaker out of an old pair of headphones. The speaker will have an impedance – typically 8 or 16 ohms for a larger speaker, but probably more like 30 ohms for some headphones. You should be able to use either in this circuit. Alternatively any speaker you get in one of those “Arduino kits” would also be fine. The speaker is connected via a 220Ω resistor to output pin 10 to limit the current flowing from the Arduino output pin, which must be no more than 40mA or it will damage the processor. A typical current of 20mA from the pin is ideal.
There is a crude, approximate calculation you can do using Ohm’s law to estimate the current flowing. It is an estimate as the speaker has an impedance, not a “direct current resistance” and we are using an alternating current which makes the real calculation more complicated. But this gives an idea within a certain margin of error.
For a 220Ω resistor and a 8Ω speaker, using Ohm’s law:
Current = Voltage / Total Resistance
So in this case we can guestimate from our 5V supply that:
Current = 5 / (220 + 8) = 0.0219… Amps
So that is around 22 mA which is about perfect (or at least gives a large margin of error to allow for our simple calculation). If using a headphone speaker of around 30Ω then the current is even lower at 20mA.
Note that this is a very inefficient circuit as most of the power of the signal is being lost within the resistor as heat rather than driving the speaker, but for our purposes that is fine.
The circuit can be powered using either the Arduino USB port or the Arduino “barrel jack” port.
The Code
The basic idea for the code is as follows:
- Read the value from the potentiometer, which will be between 0 (for 0V) up to 1023 (for the full 5V).
- Translate this into a pitch to output to the tone() generator – I use between 120Hz and 1500Hz, which us a range of around C3 up to A6.
- Instruct the tone() generator to play the tone at that pitch.
There is an extra step in that if the potentiometer reads 0, then I treat that as an instruction to turn the tone generator off completely.
One programming trick to learn, is the use of the map() function which will take a value from one range of values and return the equivalent value from a second range. So, if your first range was 1 to 100 and your second range was 1 to 2000, then giving it the value 50, which is halfway through the input range, will return the value that is half-way through the output range (1000) and so on. This is a simple way to map a range of sensor readings (in this case 0 to 1023) to something else (in this case a frequency between 120Hz and 1500Hz).
The tone() function generates a square wave at a certain frequency, which when fed through a speaker generates a simple tone at that frequency.
Closing Thoughts
This will generate a continuous sweep between the frequencies. One modification could be to create a table of specific frequencies representing individual notes and use the potentiometer input to choose which note to play.
In fact, I might try that one next.
Kevin
One thought on “Arduino Tone Generator”