Clap detector circuit / AirSpell typing system

This circuit combines a simple audio amplifier (based on an LM358 opamp) with an Arduino Nano to facilitate the detection of clapping sounds or blowing on the microphone.

This is my breadboard circuit:

I actually used a loudspeaker for my microphone, as shown here:

This Arduino program switches an LED on/off on pin D2 when a clap is detected:

//
// Clap on/off - written by Ted Burke
// Last updated 3/4/2019
//

void setup()
{
  pinMode(2, OUTPUT); // LED output
}

float v, avg = 1.66;
int lamp = 0; // on-off state of LED

void loop()
{
  // Sample analog voltage on A7 and convert to volts
  v = analogRead(7) * 5.0 / 1023.0;

  // Update moving average (really an IIR low-pass filter)
  avg = 0.99 * avg + 0.01 * v;

  // Detect sudden changes above a certain magnitude
  if (abs(v - avg) > 0.2)
  {
    // Toggle lamp (LED on D2)
    lamp = 1 - lamp;
    digitalWrite(2, lamp);
    delay(100);
  }
}

This Arduino program prints a single character over the serial connection whenever a clap is detected:

//
// Clap click - written by Ted Burke - 3/4/2019
//
 
void setup()
{
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}
 
float v, avg = 1.66;
 
void loop()
{
  // Sample analog voltage on A7 and convert to volts
  v = analogRead(7) * 5.0 / 1023.0;
 
  // Update moving average (really an IIR low-pass filter)
  avg = 0.99 * avg + 0.01 * v;
 
  // Detect sudden changes above a certain magnitude
  if (abs(v - avg) > 0.2)
  {
    // Send a character via serial link
    Serial.print("c");
 
    // Flash the LED
    digitalWrite(2, HIGH);
    delay(100);
    digitalWrite(2, LOW);
  }  
}

Finally, the following bash script generates a mouse click whenever a character is received from a device connected on /dev/ttyUSB0 (i.e. the Arduino). In combination with onboard (a free Linux onscreen keyboard) this allows text to be spelled out by blowing on the microphone in short bursts (or clapping. Onboard needs to be configured in scanning mode. This may perform better when /dev/ttyUSB0 is in configured in “raw” mode (e.g. by running sudo stty -F /dev/ttyUSB0 raw) because the read command may finish faster facilitating faster repetition.

#!/bin/bash

while true; do
read -n 1 </dev/ttyUSB0
xdotool click 1
done

Save the above bash script to a file called “clicky” and then run the following commands in the same directory:

chmod 755 clicky
sudo stty -F /dev/ttyUSB0 raw
sudo ./clicky
This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Clap detector circuit / AirSpell typing system

  1. Maram Alanhhas says:

    Hey! Thank you for sharing. I am interested in an electrical engineering student and I am trying to learn more about circuit design. I was wondering if you could explain how you built the circuit. Why did you choose these certain values for the resistors and capacitors? thnx 🙂

Leave a comment