Output Devices

This week I finished the Capsense Piano project that I began in Input Devices Week and added a Speaker to it that I salvaged from an old guitar amplifier.


After changing the footprint of the ATMega328p I was able to mill it with the 1/64th endmill. I added a single N-Channel MOSFET with the source to ground and a header to connect to the speaker. I used the single low side FET on the board to drive it. I was able to get a lot of volume using just the 5V supplied by the FTDI. To generate sound I sent a short pulse at a frequency defined by the note being played. The main loop continuously plays a sound if a note is held down. A timer interrupt then does a sweep over the capsense sensors. To calculate the frequencies for the tones, I just used a table of note frequencies I found online and put a delay of the appropriate number of microseconds between pulses, which worked surprisingly well. There is some hiccup in the sound, which is caused by the pause in audio playback while the interrupt is being handled. Here is the code to set up and handle an interrupt with timer 1 on the ATMega328p.

TCCR1A = 0; // clear registers
TCCR1B = 0;

OCR1A = 1000; // choose counter value

TCCR1B |= (1 << WGM12); // turn on CTC mode

TCCR1B |= (1 << CS10); // Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12);

TIMSK1 |= (1 << OCIE1A); // enable interrupt
ISR(TIMER1_COMPA_vect)
{
    // handle interrupt
}