Sound Sensor Modules LM386, HW-484, and KY-038

11/15/18

Categories: Electronics Tags: Arduino

~/contents

HW-484/KY-038 Sound Detector

HW-484 (KY-038) is a sound detector with LM393 and an electret mic, as well as a trimpot for adjustments (clockwise increases sensitivity, counter-clockwise decreases sensitivity). Operating voltage is 4 to 6V. This is a 4-pin sensor with both AO and DO. The analog output is the voltage signal of the microphone, the digital ouptut outputs high/low if the sound reaches a certain threshold based on how the pentometer is set.

I purchased this to make a clap sensor. By testing and adjusting the potentiometer I was able to get a high signal when clapping that is not triggered by ambient noise. For switches, it’s useful to require 2 claps. This code probably came from one of my resource links, I don’t remember which one.

int soundSensor = 10;
int relay = 4;
int claps = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
boolean lightState = false;

void setup() {
  pinMode(soundSensor, INPUT);
  pinMode(relay, OUTPUT);
  Serial.begin (9600);
}

void loop() {
  int sensorState = digitalRead(soundSensor);
  if (sensorState){
    if (claps == 0){
      detectionSpanInitial = detectionSpan = millis();
      claps++;
      Serial.println("Clap");
    }
    else if (claps > 0 && millis()-detectionSpan >= 50){
      detectionSpan = millis();
      claps++;
      Serial.println("Clap 2");
    }
  }

  if (millis()-detectionSpanInitial >= 400){
    if (claps == 2){
      if (!lightState){
          lightState = true;
          digitalWrite(relay, HIGH);
        }
        else if (lightState){
          lightState = false;
          digitalWrite(relay, LOW);
        }
    }
    claps = 0;
  }
}

Additional Resources:

LM386 Sound Sensor

This LM386-driven “intelligent” sound sensor is marked Sound Sensor V2. Operating voltage is 3.3V to 5.3V, and this sensor provides both analog and digital out. This makes it suitable for volume detection, whereas the previous sensor can merely detect the presence of sound. I purchased this to create an audio visualizer. According to the seller it can detect gain up to 200.

In my case, I only have one string of LEDs, so that simplifies matters significantly. I simply wanted to have the number of LEDs correspond to decible changes. Color would not be affected. Basic testing shows that “normal” ambient noise is around 512. A basic analog reading was good enough.

#include <FastLED.h>
#define NUM_LEDS 13
#define DATA_PIN 6
#define BRIGHTNESS  30
#define LED_TYPE    WS2812B
CRGB leds[NUM_LEDS];

byte micPin = A6;
int BASELINE = 300;
int VOLMAX = 800;

void setup() {
  pinMode(micPin, INPUT);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin (9600);
}

void loop() {
  int volume = analogRead(micPin);
  int numLedsToLight = map(volume, BASELINE, VOLMAX, NUM_LEDS, 0);
  Serial.print("VOLUME: ");
  Serial.println(volume);
  Serial.print("NUMBER LEDS: ");
  Serial.println(numLedsToLight);
  FastLED.clear();
  for(int led = 0; led < numLedsToLight; led++) {
    leds[led] = CRGB::Blue;
    }
  FastLED.show();
  delay(100);
}

Resources: