Hi all,
To help you out, here are some instructions for the SPI software and the connections.
Use the following connections on a Arduino Mega:
Arduino Mega | Lightning Detector |
---|---|
5V | VCC |
GND | GND |
52 | SCL |
50 | MISO |
51 | MOSI |
41 | CS |
18 | INT |
Connections on the VMA343 lightning detector:
Connections on the VMA212 mega sensor shield & Arduino Mega:
Connectors orange, yellow, brown and black will need to be bend to accommodate the mega sensor shield later. Be warned! Bending a Dupont wire will work once! You cannot unbend it later!
End result will look like this:
For the code, you can use this sketch. It is an adaptation of the Sparkfun example by Elias Santistevan. You will need the Sparkfun AS3935 library for this sketch to work.
Download it via the library manager in Arduino IDE / sketch / include library / manage libraries
The buzzer of the mega sensor shield will beeb when lightning or a disturber has been detected.
/*
This example code will walk you through the rest of the functions not
mentioned in the other example code. This includes different ways to reduce
false events, how to power down (and what that entails) and wake up your
board, as well as how to reset all the settings to their factory defaults.
By: Elias Santistevan
SparkFun Electronics
Date: July, 2019
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Modified by PSI @ Velleman NV
Date: Aug, 2019
*/
#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
// SPI
SparkFun_AS3935 lightning;
//pin setup for speaker
#define BuzzerPin A10
// Interrupt pin for lightning detection
const int lightningInt = 18;
// Chip select pin
int spiCS = 41;
// Values for modifying the AS395's settings. All of these values are set to their default values.
byte noiseFloor = 1; // noise treshold value between 1-7, one being the lowest. Default setting is 2
byte watchDogVal = 1; // Watchdog threshold value between 1-10, one being the lowest. Default setting is 2
byte spike = 1; // Spike Rejection value between 1-11, one being the lowest. Default setting is 2.
byte lightningThresh = 1; //lightning treshold value, can be 1, 5, 9 and 16. Default setting is 1.
boolean maskDisturbers = false; //Mask disturbers. Values can be true or false. Default setting is true.
boolean indoorUse = true; //Indoor / outdoor use. Values can be true or false. Default setting is true.
// This variable holds the number representing the lightning or non-lightning event issued by the lightning detector.
byte intVal = 0;
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(); // For SPI
if( !lightning.beginSPI(spiCS, 2000000) ) {
Serial.println ("Lightning Detector did not start up, freezing!");
while(1);
}
else
Serial.println("Schmow-ZoW, Lightning Detector Ready!\n");
// Clearing previous settings
lightning.resetSettings();
delay(250);
// "Disturbers" are events that are false lightning events. If you find
// yourself seeing a lot of disturbers you can have the chip not report those
// events on the interrupt lines.
lightning.maskDisturber(maskDisturbers); //true or false
int maskVal = lightning.readMaskDisturber();
Serial.print("Are disturbers being masked: ");
if (maskVal == 1)
Serial.println("YES");
else if (maskVal == 0)
Serial.println("NO");
// The lightning detector defaults to an indoor setting (less gain/sensitivity).
if(indoorUse)
{
lightning.setIndoorOutdoor(INDOOR);
}
else
{
lightning.setIndoorOutdoor(OUTDOOR);
}
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);
// Noise floor setting from 1-7, one being the lowest. Default setting is
// two. If you need to check the setting, the corresponding function for
// reading the function follows.
lightning.setNoiseLevel(noiseFloor);
int noiseVal = lightning.readNoiseLevel();
Serial.print("Noise Level is set at: ");
Serial.println(noiseVal);
// Watchdog threshold setting can be from 1-10, one being the lowest. Default setting is
// two. If you need to check the setting, the corresponding function for
// reading the function follows.
lightning.watchdogThreshold(watchDogVal);
int watchVal = lightning.readWatchdogThreshold();
Serial.print("Watchdog Threshold is set to: ");
Serial.println(watchVal);
// Spike Rejection setting from 1-11, one being the lowest. Default setting is
// two. If you need to check the setting, the corresponding function for
// reading the function follows.
// The shape of the spike is analyzed during the chip's
// validation routine. You can round this spike at the cost of sensitivity to
// distant events.
lightning.spikeRejection(spike);
int spikeVal = lightning.readSpikeRejection();
Serial.print("Spike Rejection is set to: ");
Serial.println(spikeVal);
// This setting will change when the lightning detector issues an interrupt.
// For example you will only get an interrupt after five lightning strikes
// instead of one. Default is one, and it takes settings of 1, 5, 9 and 16.
// Followed by its corresponding read function. Default is zero.
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();
// Buzzer
pinMode(BuzzerPin, OUTPUT);
digitalWrite(BuzzerPin, LOW);
}
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.");
}
else if(intVal == DISTURBER_INT){
Serial.println("Disturber.");
buzz();
}
else if(intVal == LIGHTNING_INT){
Serial.println("Lightning Strike Detected!");
// Lightning! Now how far away is it? Distance estimation takes into
// account previously seen events.
byte distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
Serial.println("km away!");
buzz();
// "Lightning Energy" and I do place into quotes intentionally, is a pure
// number that does not have any physical meaning.
long lightEnergy = lightning.lightningEnergy();
Serial.print("Lightning Energy: ");
Serial.println(lightEnergy);
}
}
}
void buzz() //sound the buzzer on the VMA212 mega sensor shield
{
digitalWrite(BuzzerPin, HIGH);
delay(100);
digitalWrite(BuzzerPin, LOW);
}
Note: I have made the sensor extremely sensitive with the settings above. We got the best results with these settings below. You can experiment with these settings to make the sensor work according to your convenience:
// Values for modifying the AS395's settings. All of these values are set to their default values.
byte noiseFloor = 2; // noise treshold value between 1-7, one being the lowest. Default setting is 2
byte watchDogVal = 2; // Watchdog threshold value between 1-10, one being the lowest. Default setting is 2
byte spike = 2; // Spike Rejection value between 1-11, one being the lowest. Default setting is 2.
byte lightningThresh = 1; //lightning treshold value, can be 1, 5, 9 and 16. Default setting is 1.
boolean maskDisturbers = true; //Mask disturbers. Values can be true or false. Default setting is true.
boolean indoorUse = true; //Indoor / outdoor use. Values can be true or false. Default setting is true.
Happy testing!
Kind regards,
VEL342