Hello Forum,
Has anyone here successfully used one of these lightning detectors? I've been playing with this for a while and set one up in the hopes of catching a few lightning strikes, but my setup goes through a wild electrical storm right overhead without reporting anything. If anyone out there have managed to get one of these working, I could use a tip or two.
There seems to be a number of different models out there, but most of them use the AS3935 chip. I have this one:
I have it connected to an UNO with the following connections:
- 5V -> VCC
- GND -> GND
- SCL -> SCL
- MOSI -> SDA
- SI -> 5V (to force I2C)
- IRQ -> 2
- EN_V -> 5V
- A0 -> 5V
- A1 -> GND (forces address of 0x01)
This is the code I'm using:
#include <avr/wdt.h>
#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"
#define AS3935_ADDR 0x01
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01
SparkFun_AS3935 lightning(AS3935_ADDR);
// SparkFun_AS3935 lightning;
const int STRIKEINT = 2;
int tries = 0;
long lastRestart, lastTest;
boolean failed = false;
byte noiseFloor = 2;
byte watchDogVal = 2;
byte spike = 2;
byte lightningThresh = 1;
byte intVal = 0;
byte distance;
void setup()
{
wdt_enable(WDTO_2S);
Wire.begin();
pinMode(STRIKEINT, INPUT);
Serial.begin(19200);
Serial.println("BOOTING...");
setupSensor();
}
void loop()
{
wdt_reset();
if (millis() - lastRestart > 300000) // REFRESH SENSOR EVERY 5 MINUTES
{
setupSensor();
lastRestart = millis();
// testI2C();
}
/* if (millis() - lastTest > 20000) // TEST
{
lastTest = millis();
testI2C();
} */
if (digitalRead(STRIKEINT) == HIGH)
{
intVal = lightning.readInterruptReg();
if(intVal == NOISE_INT)
Serial.println("Noise.");
else if(intVal == DISTURBER_INT)
Serial.println("Disturber.");
else if(intVal == LIGHTNING_INT)
{
Serial.println("Lightning Strike Detected!");
distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
Serial.println("km away!");
long lightEnergy = lightning.lightningEnergy();
Serial.print("Lightning Energy: ");
Serial.println(lightEnergy);
}
/* if (intVal == NOISE_INT)
{
Wire.beginTransmission(2);
Wire.write("///");
Wire.write("NSE*");
Wire.write("!");
Wire.endTransmission();
}
if (intVal == DISTURBER_INT)
{
Wire.beginTransmission(2);
Wire.write("///");
Wire.write("DIS*");
Wire.write("!");
Wire.endTransmission();
} */
if (intVal == LIGHTNING_INT)
{
Wire.beginTransmission(2);
Wire.write("///");
Wire.write("STR*");
Wire.write(distance);
Wire.write("!");
Wire.endTransmission();
}
}
}
void setupSensor()
{
// SPI.begin();
// while (!lightning.beginSPI(CS, 2000000) && tries < 10)
if (!lightning.begin())
{
tries++;
failed = true;
Serial.println (">>> Lightning Detector did not start up!");
while(1);
}
if (!failed)
Serial.println(">>> Lightning Detector Ready");
lightning.maskDisturber(false);
int maskVal = lightning.readMaskDisturber();
Serial.print("Are disturbers being masked: ");
if (maskVal == 1)
Serial.println("YES");
else if (maskVal == 0)
Serial.println("NO");
lightning.setIndoorOutdoor(INDOOR);
int enviVal = lightning.readIndoorOutdoor();
Serial.print("Are we set for indoor or outdoor: ");
if( enviVal == INDOOR )
Serial.println("Indoor.");
else if( enviVal == OUTDOOR )
Serial.println("Outdoor.");
else
{
Serial.println(enviVal, BIN);
Serial.println(F(">>> BAD RESPONSE, REBOOTING"));
while(1);
}
lightning.setNoiseLevel(noiseFloor);
int noiseVal = lightning.readNoiseLevel();
Serial.print("Noise Level is set at: ");
Serial.println(noiseVal);
lightning.watchdogThreshold(watchDogVal);
int watchVal = lightning.readWatchdogThreshold();
Serial.print("Watchdog Threshold is set to: ");
Serial.println(watchVal);
lightning.spikeRejection(spike);
int spikeVal = lightning.readSpikeRejection();
Serial.print("Spike Rejection is set to: ");
Serial.println(spikeVal);
lightning.lightningThreshold(lightningThresh);
uint8_t lightVal = lightning.readLightningThreshold();
Serial.print("The number of strikes before interrupt is triggerd: ");
Serial.println(lightVal);
// When the distance to the storm is estimated, it takes into account other
// lightning that was sensed in the past 15 minutes. If you want to reset
// time, then you can call this function.
// lightning.clearStatistics();
// lightning.resetSettings();
}