SparkFun Qwiic AS3935 Lightning Detector Hookup Guide

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

View the updated tutorial: SparkFun AS3935 Lightning Detector Hookup Guide (v20)

Pages
Contributors: Elias The Sparkiest
Favorited Favorite 1

Example Code

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

At the very top, we have a few defines that will help us to distinguish what sort of event the 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.

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

// 0x03 is default, but the address can also be 0x02, 0x01, or 0x00
// Adjust the address jumpers on the underside of the product. 
#define AS3935_ADDR 0x03 
#define INDOOR 0x12 
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

// If you using SPI, instantiate class without address: 
//SparkFun_AS3935 lightning;

// If you're using I-squared-C then keep the following line. Address is set to
// default. 
SparkFun_AS3935 lightning(AS3935_ADDR);

// Interrupt pin for lightning detection 
const int lightningInt = 4; 
int noiseFloor = 2;

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector. 
int intVal = 0;

In the setup, we set the IC 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() 
  Wire.begin(); // Begin Wire before lightning sensor. 

  if( !lightning.begin() ) { // Initialize the sensor. 
  //if( !lightning.beginSPI(9, 2000000){ // Uncomment for SPI.
    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); 
}

...and finally the meat. Here we're just monitoring the interrupt pin. If the pin reads high, then the IC has heard some sort of event. Afterwards we're going to read the interrupt register to see what the event is, whether it's lightning, a disturber, or noise. Each of these events are printed out in the serial window at 115200 baud. The lightning event however will also print out the estimated distance to the front of the storm. Keep in mind that this is not the distance to the lightning but rather the storm.

language:c
void loop()
{
  if(digitalRead(lightningInt) == HIGH){
    // Hardware has alerted us to an event, now we read the interrupt register
    // to see exactly what it is. 
    intVal = lightning.readInterruptReg();
    if(intVal == NOISE_INT){
      Serial.println("Noise."); 
      //reduceNoise(); //See note below above reduceNoise function.
    }
    else if(intVal == DISTURBER_INT){
      Serial.println("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); //Let's not be too crazy.
}

In case you're sitting at a computer and there's a lot of RF noise in your area then you may need a way to make the lightning detection less sensitive to these events. Call this function and feed it a number lower than seven to increase it's noise threshold. Likewise, you could put this function in the noise if-statement listed above and it will increase that threshold every time a noise event occurs. This may give you an idea of which number works best for the area you're in.

language:c
void reduceNoise(){
  ++noiseFloor; // Manufacturer's default is 2 with a max of 7. 
  if(noiseFloor > 7){
    Serial.println("Noise floor is at max!"); 
    return;
  }
  Serial.println("Increasing the event threshold.");
  lightning.setNoiseLevel(noiseFloor);  
}

If you've heard some lightning, than you'll see the following in your Arduino serial window.

Example Output With Noise and Lightning Emulated