Turn on an LED by clapping using a microphone

xml74 Posted messages 186 Status Member -  
xml74 Posted messages 186 Status Member -
Hello, as a beginner, I'm asking for your help. My goal is to be able to turn on an LED using a microphone by clapping my hands, and then turn it off by clapping my hands again. How is it possible to do this?
Thank you in advance.

3 answers

  1. ElementW Posted messages 5690 Status Contributor 1 293
     
    Hey, it all depends on the platform you're using and the equipment you have available...

    By the way, hello again! It's been a while ;)
    --
    from human import idiocy
    del idiocy
    1
    1. xml74 Posted messages 186 Status Member 1
       
      Hello again :D
      Yes, I only have an Arduino UNO.... But I have a microphone (a must..)
      0
  2. ElementW Posted messages 5690 Status Contributor 1 293
     
    Well, first you put your LED on the Arduino (obviously), connected to a pin (e.g. 13) and to GND -- with an appropriate resistor so as not to blow the diode.
    For your microphone, it needs to be connected to an ANALOG IN pin as well as GND;
    However, we have no indication of the voltage amplitude it can provide, and it will likely need to be amplified.
    On the code side, you will need to:
    - set up the ADC so that we can read the audio data, ideally at 22kHz (or higher)
    - have a variable to know whether to turn the LED on or off
    - in a loop, read the analog data, and if a spike is detected, toggle the state of the LED
    For the state variable, it's trivial:
    bool ledAllumee = false;

    For ADC initialization, it’s less straightforward... because analogRead allows for a maximum of 10,000 reads per second.
    You will also need to determine what the voltage is when a clap occurs, which should be much higher than the ambient noise.
    --
    from human import idiocy
    del idiocy
    1
  3. xml74 Posted messages 186 Status Member 1
     
    Okay, so for now, I've written this code:
    const int m1 = 7; void setup() { //initialization content pinMode(m1, OUTPUT); } void loop() { digitalWrite(m1, HIGH);}

    It's not lighting up an LED but a relay (For a home automation project).
    What I think I need to do first:
     const int m1 = 7; const int mic =8; void setup() { pinMode(mic, INPUT); pinMode(m1, OUTPUT); } void loop() { digitalWrite(m1, HIGH);}

    And add conditions:
    if
    &
    else

    But I don't know how to go about it.. After that, I have to know that the code runs in a loop, so it refreshes automatically, and has to detect exactly when the microphone sends something.
    0