Week 9: Input devices

Now, it is time to program a microcontroller to measure something (Week 5). For this week, I decided to make a switch, which would turn on and off by clapping. The idea is to use this device later for my final project and enable any connected device turn on and off remotely.

Step 1: Detect the sound

First step is to built a sound detector. For that, I ihnerited the schematics from the Neil's sound input device. I took an empty PB3 pin as an output pin, which would indicate whether the clapping event has occured.

Modifying the board design to account for the 2x2 connector (connected to PB3 and ground), I strived to have no vias, and winded up with the following layout in Eagle:

Circuit design

PCB design

As one can see, the gain pin on the micrphone is not connected to any resistor, which according to the datasheet of SPU0414HR5H-SB microphone, leads to the maximum gain of 20dB, which is exactly what I need. The 10uF capacitor, coupled with 2.44kOhm microphone impedance, leads to the high-pass filter with the cut-off frequency of 6.52 Hz, which seems sufficient for our audio clap detection purposes.

After the arrangement of components is done, I proceeded with milling and stuffing the board.


Sound detection is working

After a few struggles with starting up the board (see the issues tracker below), I have finally gotten it working, and uploaded the Neil's code and the board passed the sound detection test. Now, it is time to learn how to detect a sound with the LED.

Step 2: Detect a sound

Now, to detect a sound, I took the contents of higher bits, (corresponding to 8 bits precision) in the Neil's code and compared them with the experimentally found threshold of "0x8D". I ignored the contents of the lower bit register (corresponding to the extra 2 bits of precision)

Modified Neil's code for the sound detection

The resulting sound detection performed on the connected to my 2x2 pins looks as follows


Sound detection

Step 3: Distinguish a clap

I would like to intitialize the board by performing two quick claps, which, sounds, according to the Audacity software, looks as follows:

Set of claps visualized with Audacity

We can see that my clap sequence consists of two peaks separated by ~200 ms with ~50 ms decay time. After studying the datasheet for Attiny45, I wrote a set of if statements with a timer, which check all the requirements for two claps followed by ~800ms of silence.

Clapper main code

Finally, the sound detection looks as follows


Selective clap detection

Issues that I faced

Files