0

I have a sound sensor that was working perfectly fine as I was using it as a clap sensor to turn lights on and off. However, I tried to change the code to make it work a little smoother and for whatever reason it stopped working. I then tried to write a very simple script to allow it to turn on and off using just one clap but now it treats the sound sensor as if it's always inputting HIGH. Any ideas why this might be happening?

note: I've turned up the sensitivity and can see that the sound sensor isn't always HIGH. Even when it's too high to hear me clap it still turns the LED on and off as if the if statement below was always true.

int SENSOR = 2;
int LED = 13;


void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  pinMode(SENSOR, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  if(digitalRead(SENSOR) == HIGH){
    digitalWrite(LED, (digitalRead(LED) == HIGH ? LOW : HIGH));
    delay(100);
  }
}

This is the sound sensor module I am using: Robojax Sound Detection Microphone Module

  • sensors don't output HIGH or LOW, their output is numbers... – ElectronSurf Jul 27 '19 at 19:51
  • 1
    revert your code to where it worked and start rewriting the code again – jsotola Jul 27 '19 at 19:54
  • The issue is that I can't revert back to my old code so I need to rewrite. – JeffBrin Jul 27 '19 at 19:56
  • 1
    Im sorry about all the confusion, I just read up on how it works and apparently it outputs low when it senses a sound, weird because it was working before when I was checking for a HIGH input. Anyways my problem's solved thank you for the suggestions. – JeffBrin Jul 27 '19 at 20:05
  • 1
    @jsotola Not really. It toggles the LED when the sensor is reads HIGH, with a delay of 100 ms between each toggle. When the sensor does not read HIGH it doesn't toggle the LED. Thus it does something different when the sensor is HIGH compared to LOW. – Nick Gammon Jul 27 '19 at 21:52
2

The robojax microphone switch I was using outputs LOW when it senses a sound rather than outputting HIGH as I assumed. The solution was just to replace all the if statements testing for a HIGH input with LOW.

note: sorry about not looking into the microphone earlier.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.