SparkFun AS3935 Lightning Detector Hookup Guide (v20)

Pages
Contributors: Elias The Sparkiest
Favorited Favorite 2

Example 1 - Basic Lightning SPI

The example below will start with the basic functionality in Example1_BasicLightning_SPI.ino. Open the example up to follow along.

Example 1 - Basic Lightning SPI

At the very top, we have a few defines that will help us to distinguish what sort of event the SparkFun Lightning Detector has sensed. There are three possible "events". The first of course is lightning, but we may also get a false lightning event called a disturber, and finally we may hear noise. Also of note are the noise and disturber variables. We'll talk about those farther down in the main loop.

language:c
#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"

#define INDOOR 0x12 
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

SparkFun_AS3935 lightning;

// Interrupt pin for lightning detection 
const int lightningInt = 4; 
int spiCS = 10; //SPI chip select pin

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector. 
int intVal = 0;
int noise = 2; // Value between 1-7 
int disturber = 2; // Value between 1-10

By default the SparkFun Lightning Detector is set to be run inside because I'm assuming you're at your computer running this code. If you're outside change the parameter to OUTDOOR.

language:c
void setup()
{
  // When lightning is detected the interrupt pin goes HIGH.
  pinMode(lightningInt, INPUT); 

  Serial.begin(115200); 
  Serial.println("AS3935 Franklin Lightning Detector"); 

  SPI.begin(); 

  if( !lightning.beginSPI(spiCS, 2000000) ){ 
    Serial.println ("Lightning Detector did not start up, freezing!"); 
    while(1); 
  }
  else
    Serial.println("Schmow-ZoW, Lightning Detector Ready!");

  // The lightning detector defaults to an indoor setting at 
  // the cost of less sensitivity, if you plan on using this outdoors 
  // uncomment the following line:
  //lightning.setIndoorOutdoor(OUTDOOR); 
}

In the main loop the interrupt pin is monitored for a lightning event from the Lightning Detector. If the interrupt pin goes HIGH we check whether this event is lightning, a disturber, or noise with the lightning.readInterruptReg() function call. Each of these events are printed out in the serial window at 115200 baud. If lightning is detected, then we check the distance to the storm with lightning.distanceToStorm() and print that out to the window. There are two other possibilities: noise and disturber. If you see a lot of noise or disturbers check the section False Positives and Noise above to help guide you to reduce the noise in your environment. We can also increase the SparkFun Lightning Detector's robustness to noise with the lightning.setNoiseLevel() function call. Likewise we can increase the SparkFun Lightning Detector's resistance to false lightning events with lightning.watchdogThreshold().

language:c
void loop()
{
   // Hardware has alerted us to an event, now we read the interrupt register
  if(digitalRead(lightningInt) == HIGH){
    intVal = lightning.readInterruptReg();
    if(intVal == NOISE_INT){
      Serial.println("Noise."); 
      // Too much noise? Uncomment the code below, a higher number means better
      // noise rejection.
      //lightning.setNoiseLevel(noise); 
    }
    else if(intVal == DISTURBER_INT){
      Serial.println("Disturber."); 
      // Too many disturbers? Uncomment the code below, a higher number means better
      // disturber rejection.
      //lightning.watchdogThreshold(disturber);  
    }
    else if(intVal == LIGHTNING_INT){
      Serial.println("Lightning Strike Detected!"); 
      // Lightning! Now how far away is it? Distance estimation takes into
      // account any previously seen events in the last 15 seconds. 
      byte distance = lightning.distanceToStorm(); 
      Serial.print("Approximately: "); 
      Serial.print(distance); 
      Serial.println("km away!"); 
    }
  }
  delay(100); // Slow it down.
}

When lightning is sensed you'll see the following in your Arduino serial window.

This image shows a picture of the Arduino IDE's Serial Monitor with lightning being detected printed out.