Simple Music Maker

Enter Your Electronics & Design Project for Your Chance to Win a Fog Machine and a $100 Shopping Cart!

Back to The Project14 homepage

Project14 Home
Monthly Themes
Monthly Theme Poll




Happy Birthday Arduino and Happy Birthday Project14.



My entry to the Open Arduino competition was going to be a music box. I'm too late for that, having missed the deadline, so I'll post the blog in Simple Music Maker instead (and keep my fingers crossed that I don't win a fog machine). As you can hear, it's far from perfect, but it was fun to put together and play with.


The Uno is going to synthesize the sounds digitally. There's a small amount of analogue circuitry to convert the digital values to an analogue signal and drive a small loudspeaker with a very simple amplifier.


Circuit description


The waveform is generated by a R-2R network working as a DAC (digital-to-analog converter) directly from the Arduino port pins.


See this series of blogs for more on the R-2R network used as a DAC: Arduino: R-2R Experiment



By its nature, because the digital I/O pins swing between the rails, the output of the DAC is 0 to 5V fullscale. That's too high for what I want here, so I need to reduce the swing to something more manageable. I do that with the first stage (U1) which is an inverting amplifier with a gain of one tenth fifth - that then gets me a maximum swing of half a volt (I changed it after writing this because the original was a bit quiet and then forgot to come back and edit the text). The gain is simply R1/R8. The idea behind the 150pF capacitor is to slow the edges of the DAC output which are very fast and cause the op-amp to ring - it helps, but could be done better. The LM324 is an old bipolar op-amp that can't work up all the way up to the supply rail, and it's probably best to keep everything well clear of ground too, so the first stage also places the signal around 2V. It doesn't matter what the actual, precise level is as long as the signals are in a range where the op-amp can function properly - the audio is ac and at the end of the chain will pass through a capacitor, after which it will be referenced to ground again, so we don't care about arbitrary dc offsets prior to that.


Following the attenuator, there's a low-pass filter which comprises op-amp U4 and the passive components around it. It's a third-order Sallen-Key filter. This smoothes the waveform and removes the steps at the sampling rate. I didn't design this from scratch, instead I copied an existing design and scaled the resistor values to modify the cut-off frequency. The op-amp is configured as a follower and there's a dc path from the previous stage to the non-inverting input, so the output will still be centred about 2V.


The final stage, comprising U2 and the transistors, is a simple (and somewhat crude) power amplifier. The op-amp can't manage enough current to directly drive an 8 ohm loudspeaker, so the transistors lend a hand. The transistors are configured as emitter followers in a push-pull arrangement. To increase the output voltage the op-amp lifts the base of T1, T2 will naturally be off in this situation, and to decrease the output voltage, the op-amp lowers the base voltage of T2, where T1 will be off. The two diodes drop slightly less than the Vbe drops of the transistors and reduce the distance that the op-amp output has to swing when it changes over from one transistor to the other - that reduces the distortion that would otherwise occur because of the slew rate of the op-amp limiting the rate at which its output can change. The whole output stage is within the feedback loop of the op-amp and because it's wired as a follower the voltage at the loudspeaker should be the same as the filter output. There's still a little crossover distortion - a designer of hi-fi power amplifiers would die of shame if their amplifier has these kind of specs - but for a simple Arduino project it's fine.


This is what the simulator shows for the overall frequency response:



The output capacitor is there to give a signal at the loudspeaker that swings about ground and gives an avarage of zero - it's not good to have a permanent bias on a loudspeaker. The capacitor forms a high-pass filter with the load resistance (8 ohms) and for the cut-off frequency to be reasonable it needs to be a fairly high value. The 470u here gives a cut-off of about 100Hz. Not Hi-Fi quality, but I don't suppose anyone will worry about that - this isn't intended as a high-fidelity music box.




As with the R-2R experiments, I built this one on a piece of tri-pad board.


Here's the board as I built it:







The starting point for the testing was to simply use the sketches I'd done previously for the audio generator experiments.


Here are some traces from the board.


This shows a sweep from 20Hz to 20kHz and you can see the way the filter slopes off from about 10kHz upwards:



This is the waveform at the loudspeaker terminals (440Hz) - it's kind of sine-like. The little steps on the slopes of the sinewave are the points where the op-amp is frantically trying to get its output to the right place to effect the changeover between the two transistors.



Music Box Sketch


Finally, here's the sketch to play the tune (the include at the start is of arduino.h):


/* Arduino Music Box */
unsigned int tableOffset = 0;          // wave table index
unsigned int noteTime = 0xc35;         // note duration count
volatile unsigned int tableStep = 0;
int i=0;
unsigned int tuneNotes[] = {
//  --- Sine table - generated by sineTable.exe 
unsigned int sineTable[512] = {
void setup() {
  // set the digital pins as outputs:
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  // timer 2 set up
  cli();                  // disable interrupts
  TCCR2A = 0;             // control register all 0
  TCCR2B = 0;             // control register all 0
  TCNT2 = 0;              // set count to 0
  OCR2A = 159;            // period = 160 x 1/16MHz = 10uS
  TCCR2A |= (1 << WGM21); // mode is clear on match
  TCCR2B |= (1 << CS20);  // no prescaler   
  TIMSK2 |= (1 << OCIE2A);  // enable interrupt on match
  sei();                  // enable interrupts
  if(tableStep == 0) {
    PORTD = 0x80;
    noteTime = noteTime - 1;
    if(noteTime==0) {
      tableStep = tuneNotes[i++];
      noteTime = tuneNotes[i++];
      tableOffset = 0;
  else {
    tableOffset = tableOffset + tableStep;
    PORTD = (sineTable[tableOffset >> 7]) >> 8;
    noteTime = noteTime - 1;
    if(noteTime==0) {
      tableOffset = 0;
      tableStep = 0;
      noteTime = 0xc35;
      if (tuneNotes[i]==0)
void loop() {


The code is a bit clunky but it's a starting point for experimenting with. This is a rather fast version of 'Twinkle, Twinkle, Little Star' (I know it doesn't sound much like it, but really it is). This is with the waveform table containing a sine wave, so it has an electronic quality to it, and there's no envelope control, so the notes just slam in and out. The note table is arranged in pairs, first the note and then the duration. The note is the step value for the waveform table, the duration is the number of 10uS intervals. Having the duration in terms of the steps might seem awkward (and it is) but when I first tried it I got a lot of nasty clicking from having abrupt terminations part way through a sine wave cycle, so these values have been arranged to finish close to the point where it would cross zero.


For an actual music box, it would probably want to play through once on being powered up and then turn itself off - at the moment it repeats endlessly.


There's a follow-on blog here: Simple Arduino Music Box: Voices