(Probably) Simplest Arduino based Sound/Lightning Trigger

I have just finished a small project -- sound trigger. It is very stable yet very sensitive -- it can detect a moderate finger snap 3-4 ft away.

Schematics and construction:

Code:

//  
//    This is a sound trigger for Arduino using simple Piezo 
//    as sound sensor, it is quick and dirty, but highly sensitive
//    device that can detect a moderate finger snap in 3 ft
//
//    Copyright (c) 2013 Peter Y Lin (a.k.a MJKZZ)
// 
//    Permission is hereby granted, free of charge, to any person
//    obtaining a copy of this software and associated documentation
//    files (the "Software"), to deal in the Software without
//    restriction, including without limitation the rights to use,
//    copy, modify, merge, publish, distribute, sublicense, and/or sell
//    copies of the Software, and to permit persons to whom the
//    Software is furnished to do so, subject to the following
//    conditions:
// 
//    This permission notice shall be included in all copies or 
//    substantial portions of the Software.
// 
//    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//    OTHER DEALINGS IN THE SOFTWARE.
//
#include <avr/io.h>
#include <avr/interrupt.h>

#define    FLASH_PIN      7
#define    SOUND_PIN      A0

#define    TIMEOUT        100  // 100 milliseconds
#define    THRESHHOLD     2  

void setup()
{
  Serial.begin(115200);
  
  // make flash trigger pin output
  pinMode(FLASH_PIN, OUTPUT);
  // keep flash trigger pin low
  digitalWrite(FLASH_PIN, LOW);
}

volatile int v1 = 0;
volatile int v2 = 0;
volatile int to = 0;

void loop()
{
  // start analog read
  v1 = analogRead(SOUND_PIN);
  while(1)
  {
    // acquire again
    v2 = analogRead(SOUND_PIN); 
    
    // if difference between two analog read exceeds threshhold, 
    // and it has been timed out since last trigger,
    // then trigger flash
    if (abs(v1 - v2) > THRESHHOLD && to == 0)
    {
      // debug only, you can remove it 
      Serial.println(abs(v1 - v2), DEC);
      
      // *** edit your code to insert time delay here ***
      
      // trigger flash
      digitalWrite(FLASH_PIN, HIGH);
      // keep flash trigger high for 1ms
      delay(1);   
      // done triggering, reset back   
      digitalWrite(FLASH_PIN, LOW);

      // set timeout value to prevent multiple triggering
      // unit is number of milliseconds
      to = TIMEOUT;
    }
    v1 = v2;
    
    // decrease timeout value 
    if (to)
    {
      to--;
      delay(1);
    }
  }
}

Very cool! What sort of microphone/speaker was that? I was trying to detect the tick of a grandfather clock with only partial success.

You could use the analog comparator instead of analogRead, that might give slightly faster response (not that it probably matters):

Oh, it is a piezo type of buzzer which can also be used as sound (pressure) sensor -- it generates voltage under pressure or sound.

Very cool project you have there, too. I am new to Arduino world, but I'd love to use the comparator mode for this. Or even do a lightning/thunder trigger. Thanks for the tip.

Actually, I put in a photo diode in with positive terminal going into A0 and negative terminal going into Gnd on the Arduino, it can detect a Yongnuo 560 at 1/128 power level 15 ft away -- not pointing at the flash, but pointing at the ceiling. I am sure if pointing to the light, it can detect it at even further distance.

The photodiode is BPW21R

So, it should be good to detect lightning.

That is the most elegant solution to this type of triggering I have ever seen. Well done. I am totally copying it for future reference.

I am having a problem with this circuit. I can detect noise and see the serial monitor respond, however the flash will only fire when I remove a wire and from the circuit. Does this possibly have with the fact that I am using a disposable camera flash unit. Any help is appreciated.