CSC270 Sound Sensor

From CSclasswiki
Jump to: navigation, search


Soundtestimage.jpg

First, we put together a simple circuit to test and adjust the sensitivity of the sensor. The sensor provided in the kit includes two output pins - digital and analog. For our purposes we decided to use the digital pin that would output HIGH when the sound was louder than the threshold, and LOW when it was quieter. The signal received from the sensor would act as an input for the LED, making it turn on or off.

/* Sound Sensor Test
 * Joce Kofke and Stacy Falaleeva
 * 4/16/19
 * 
 * Should flash an LED when a clap is registered
 */

int led = 7;
int buttonpin = 5;
int val = 0;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(buttonpin, INPUT);

}

void loop() {
  val = digitalRead(buttonpin);
  if (val == HIGH){
    digitalWrite(led, HIGH);  //turn on the LED when reading HIGH from sensor
  }
  else{
    digitalWrite(led, LOW);   //otherwise LED should be off
  }
}


This way we were able to easily adjust the sensitivity of the sensor, so it would react to the clap, but not to the background noise. Adjusting the sensitivity is done by twisting the little knob on top of the sensor.
Sound sensor.jpg

Debouncing

The problem with the sound sensor we've discovered later was that the signal bounced when it registered a clap. This is very standard for switches to do that. Since the software runs fast, it registers multiple HIGH-LOW transitions and switches the state of our finite state machine at every bounce. The signal from the sensor looks something like this:

Bounce.png


There are multiple ways debounce the switch, including adding hardware or modifying software. You can read more information about debouncing here. We decided to add a simple short delay after the detection of the first LOW-HIGH transition, so that the program pauses for a bit before staring to check for transitions again, and it misses the bounces.

 if (clap == LOW && lastClap == HIGH){
   delay(250); // debouncing
   state = (state + 1) % 3; // changes state