You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DFRobot_AS393DFRobot_AS39355/examples/DFRobot_AS3935_lightning_se.../DFRobot_AS3935_lightning_se...

141 lines
3.7 KiB

6 years ago
/*!
6 years ago
file DFRobot_AS3935_lightning_sensor.ino
6 years ago
SEN0290 Lightning Sensor
This sensor can detect lightning and display the distance and intensity of the lightning within 40 km
It can be set as indoor or outdoor mode.
The module has three I2C, these addresses are:
6 years ago
AS3935_ADD1 0x01 A0 = 1 A1 = 0
AS3935_ADD2 0x02 A0 = 0 A1 = 1
AS3935_ADD3 0x03 A0 = 1 A1 = 1
6 years ago
Copyright [DFRobot](http://www.dfrobot.com), 2018
Copyright GNU Lesser General Public License
6 years ago
version V0.5
date 2018-11-28
6 years ago
*/
#include "Lib_I2C.h"
#include "DFRobot_AS3935_I2C.h"
6 years ago
volatile int8_t AS3935IsrTrig = 0;
6 years ago
#define IRQ_PIN 2
// Antenna tuning capcitance (must be integer multiple of 8, 8 - 120 pf)
#define AS3935_CAPACITANCE 96
// I2C address
6 years ago
#define AS3935_I2C_ADDR AS3935_ADD3
6 years ago
void AS3935_ISR();
6 years ago
DFRobot_AS3935_I2C lightning0((uint8_t)IRQ_PIN);
6 years ago
void setup()
{
Serial.begin(115200);
Serial.println("DFRobot AS3935 lightning sensor begin!");
// Setup for the the I2C library: (enable pullups, set speed to 400kHz)
I2c.begin();
I2c.pullup(true);
I2c.setSpeed(1);
delay(2);
6 years ago
lightning0.setI2CAddress(AS3935_ADD3);
6 years ago
// Set registers to default
6 years ago
if(lightning0.defInit() != 0){
Serial.println("I2C init fail");
while(1){}
}
6 years ago
// Configure sensor
6 years ago
lightning0.powerUp();
6 years ago
//set indoors or outdoors models
lightning0.setIndoors();
//lightning0.setOutdoors();
//disturber detection
lightning0.disturberEn();
//lightning0.disturberDis();
6 years ago
lightning0.setIRQOutputSource(0);
delay(500);
//set capacitance
lightning0.setTuningCaps(AS3935_CAPACITANCE);
Serial.println("AS3935 manual cal complete");
6 years ago
6 years ago
// Enable interrupt (connect IRQ pin IRQ_PIN: 2, default)
// Connect the IRQ and GND pin to the oscilloscope.
// uncomment the following sentences to fine tune the antenna for better performance.
// This will dispaly the antenna's resonance frequency/16 on IRQ pin (The resonance frequency will be divided by 16 on this pin)
// Tuning AS3935_CAPACITANCE to make the frequency within 500/16 kHz ± 3.5%
6 years ago
// lightning0.setLcoFdiv(0);
// lightning0.setIRQOutputSource(3);
6 years ago
6 years ago
// Set the noise level,more than 7 will use the default value:2
6 years ago
lightning0.setNoiseFloorLvl(2);
//uint8_t noiseLv = lightning0.getNoiseFloorLvl();
//used to modify WDTH,alues should only be between 0x00 and 0x0F (0 and 7)
6 years ago
lightning0.setWatchdogThreshold(1);
6 years ago
//uint8_t wtdgThreshold = lightning0.getWatchdogThreshold();
//used to modify SREJ (spike rejection),values should only be between 0x00 and 0x0F (0 and 7)
lightning0.setSpikeRejection(2);
//uint8_t spikeRejection = lightning0.getSpikeRejection();
6 years ago
attachInterrupt(0, AS3935_ISR, RISING);
}
void loop()
{
// It does nothing until an interrupt is detected on the IRQ pin.
6 years ago
while (AS3935IsrTrig == 0) {}
6 years ago
delay(5);
// Reset interrupt flag
6 years ago
AS3935IsrTrig = 0;
6 years ago
// Get interrupt source
6 years ago
uint8_t intSrc = lightning0.getInterruptSrc();
6 years ago
if (intSrc == 1)
6 years ago
{
// Get rid of non-distance data
6 years ago
uint8_t lightningDistKm = lightning0.getLightningDistKm();
6 years ago
Serial.println("Lightning occurs!");
Serial.print("Distance: ");
6 years ago
Serial.print(lightningDistKm);
6 years ago
Serial.println(" km");
// Get lightning energy intensity
6 years ago
uint32_t lightningEnergyVal = lightning0.getStrikeEnergyRaw();
6 years ago
Serial.print("Intensity: ");
6 years ago
Serial.print(lightningEnergyVal);
6 years ago
Serial.println("");
}
6 years ago
else if (intSrc == 2)
6 years ago
{
Serial.println("Disturber discovered!");
}
6 years ago
else if (intSrc == 3)
6 years ago
{
Serial.println("Noise level too high!");
}
6 years ago
//View register data
//lightning0.printAllRegs();
6 years ago
}
//IRQ handler for AS3935 interrupts
void AS3935_ISR()
{
6 years ago
AS3935IsrTrig = 1;
6 years ago
}