Saturday, January 12, 2008

Arduino sound generation: sawtooth wave

This is a basic, initial attempt at making some sounds directly using the Arduino board. I know that it is pretty rudimentary at the moment, but you have to start somewhere.

The aim for this exercise is to generate an upwards-ramping sawtooth wave that can be tuned to (roughly) musical pitches. The amplitude of the wave is constant. The resulting generator has 64 different pitches selected by the first 6 bits of a DIP switch. 0000 is roughly equal to MIDI note C0 and %111111 is roughly equal to MIDI note D#5.

The waveform is made up of 32 steps. Each step is output from the Arduino board via digital outputs 2 to 7 and then into a R2R ladder. Output 2 is the lsb whilst output 7 is the msb.

The amount of delay to hold each step has been calculated as follows. A midi note from between 24 and 87 is converted into frequency, which is then converted into microseconds. This value is divided by 32 (for 32 steps) and then rounded up or down to the nearest microsecond. These 64 values are held in an array.

The command "delayMicroseconds" is then used in the Arduino program loop to actualise this delay in every step of the waveform. The DIP switch is connected to digital pins 8 to 13 and selects as a binary value the pitch MIDI number. The corresponding delay time is looked up in an array of 64 integers.

You can view an image of the waveform below.


Intosia said...

I love sawtooths :D Anyway you could share the code?

Sebastian Tomczak said...

yep sure, but you will have to wait for a few days because my computer is broken. :(