Arduino – making a basic drum machine
Had a quick look round at turning a piezoelectric speaker in to a sensor that will detect a tap or knock. I also then had a search around for setting the output of a speaker to a different note. Combining this has given me a small basic Arduino drum machine and a headache to my girlfriend.
First of all a piezoelectric speaker works kind of like a guitar string in that it vibrates to generate sound. Typically we pass a current through the 2 pins and this then vibrates the piezo element accordingly, using Pulse Width Modulation allows us to alter the volume and the frequency of the sound wave by altering how frequent we vibrate the piezo material.
So if we reverse this we get a knock sensor which means we vibrate the piezo element by hitting it this then creates a signal that we detect as an input. By sensing the vibrations we can crudely detect how hard it was hit. But because we constantly read the input and the vibrations will reach a peak we need to take the peak value to get a decent result rather than the first or last reading. To do this we write a small chunk of code to take the highest reading.
Next we have a normal speaker as our output, by altering the frequency we can alter the note that it produces, much like creating an Infrared signal in my tutorial here. The frequency is basically created by the microsecond time length of the soundwave and we rapidly pulse the speaker to this length so half the length is on, the other is off. Different notes and octaves have different wave lengths on the chromatic scale. A basic 12 note scale is shown below with the chromatic notes and their frequency along with the timings:
|Chromatic Note||Frequency Hz||Time of soundwave (micro seconds) ?s||Pulse On/Off (divide time by 2) ?s|
|C||261 Hz||3830 ?s||1915 ?s|
|C#||277 Hz||3610 ?s||1805 ?s|
|D||294 Hz||3400 ?s||1700 ?s|
|D#||311 Hz||3216 ?s||1608 ?s|
|E||329 Hz||3038 ?s||1519 ?s|
|F||349 Hz||2864 ?s||1432 ?s|
|F#||370 Hz||2702 ?s||1351 ?s|
|G||392 Hz||2550 ?s||1275 ?s|
|G#||415 Hz||2410 ?s||1205 ?s|
|A||440 Hz||2272 ?s||1136 ?s|
|A#||466 Hz||2146 ?s||1073 ?s|
|B||493 Hz||2028 ?s||1014 ?s|
To calculate the time of a wave we can use the following calculation:
Time (T) = 1 / frequency (Hz)
This will give us the value in seconds. To obtain microseconds multiply this value by 1,000,000 (There are a million microseconds to 1 second). Conversely we can obtain the frequency by dividing 1 by the Time (in seconds).
I’m going to have 2 piezo speakers as knock sensors which will light up a corresponding LED and play a different note, in this case a C or a D.
Arduino Drum Machine Parts
2x 220 Ohm resistor (Red, Red, Brown, Gold)
2x 1Mega Ohm resistor (Brown, Black, Green, Gold) – that’s 1,000,000 Ohms
2x piezoelectric speakers
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
Optional 9V DC power supply or use the USB power for the Arduino
More detail: Arduino – making a basic drum machine
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days