This is the AS3935 Sensor a programmable and fully integrated lightning sensor IC that detects the presence and approach of potential hazardous lightning activity in the area and provides an estimation on the distance to the head of the storm. The AS3935 algorithm will verify the incoming signal pattern to reject the potential man-made disturbances. AS395 can be also provide an information at the noise level and inform the Microcontroller in case of high noise conditions, with the noise ground generator and noise ground evaluation blocks.
This device can be programmed via SPI or i2C via 4 different address, 2 clocks are internally generated by two different RC OSCILLATORS TRCO and SRCO an automatic procedure can increase the precision of the oscillators. AS3935 can detect distance estimation to the head of the storm down to 1 kilometer in 14 steps on lightning storm activity can detects at radius of 40 Kilometer, and detects both cloud-to-ground and intra-cloud (cloud to cloud) flashes. This device is ideal for Weather Stations, Clocks, Sports Equipment’s, Smart UPS, Pool Safety Devices, and Smart Portable Devices, GPS (Global Positioning System, Cellular Phones, and Watches. Etc. For further information see the datasheet below.
Required Components
- Arduino Microcontroller, NodeMCU, Teensy Board, TeensyDuino, ESP8266 12, 12E, ESP8266 NodeMCU, ESPDuino, ATMEGA328 16/12, ATMEGA32u4 16/8/ MHz, ESP8266, ATMEGA250 16 MHz, ATSAM3x8E, ATSAM21D, ATTINY85 16/8 MHz (Note: The Diagram below is using NANO. (please refer to the respective pin-outs)
- AS3935 IC / AS3935 Module
- Capacitors (See the diagram below for soldering manually)
- Resistors (See the diagram below for soldering manually)
- Jumper Wires / DuPont Wires
- Prototyping PCB board (for soldering manually)
Wiring Guide
Wiring on i2C
Wiring on SPI
Source Code for i2C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include "I2C.h" //I2C Library can be downloaded below #include "AS3935_I2C.h" //Include AS393_i2C Library can be downloaded below volatile int8_t ISR_Trigger = 0; // interrupt trigger global var #define SI_PIN 9 //Set SI pin here #define IRQ_PIN 2 // digital pins 2 and 3 are available for interrupt capability #define AS3935_ADD 0x03 // x03 <-- you can use i2C scanner to see the i2c address #define AS3935_CAPACITANCE 72 // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD #define AS3935_INDOORS 0 // Set to enabled for Indoor Used #define AS3935_OUTDOORS 1 // Set to enabled for Outdoor Used #define AS3935_DIST_DIS 0 #define AS3935_DIST_EN 1 void AS3935_ISR(); AS3935_I2C lightX((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD); void setup() { Serial.begin(115200); Serial.println("14CPRE | AS3935 Lightning Sensor Test"); Serial.println("Starting..................."); delay(2000); I2c.begin(); // Set i2C Libr Enable pullups set to 400kHz speed I2c.pullup(true); //Set i2C pUll up I2c.setSpeed(1); //Set speed to 1 delay(2); // Delay lightX.AS3935_DefInit(); // set registers to default lightX.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN); // attachInterrupt(0, AS3935_ISR, RISING); lightX.AS3935_PrintAllRegs(); ISR_Trigger = 0; // clear trigger } void loop() { while(0 == ISR_Trigger){} delay(5); ISR_Trigger = 0; // reset interrupt flag uint8_t int_src = lightX.AS3935_GetInterruptSrc(); if(0 == int_src) { Serial.println("IRQ source result not expected"); } else if(1 == int_src) { uint8_t lightning_dist_km = lightX.AS3935_GetLightningDistKm(); Serial.print("LIGHTNING DETECTED > Distance to strike : "); Serial.print(lightning_dist_km); Serial.println("kilometers"); } else if(2 == int_src) { Serial.println("DISTRUBER DETECTED >"); } else if(3 == int_src) { Serial.println("NOICE LEVEL TO HIGH >"); } lightX.AS3935_PrintAllRegs(); // Set for debug } void AS3935_ISR() //IRQ Handler for AS3935 Interrupts, set to return void take no arg. { ISR_Trigger = 1; } |
Source Code for SPI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/* Note the AS3935 sensor communicates via SPI or I2C the code below use SPI if your using SPI rather than i2C remove the library which is the code library folder*/ #include "SPI.h" //include AS3935 Library which is can be downloaded below #include "AS3935.h" //Include AS3935 library which is can be downloaded below int8_t CS_PIN = 10; //Set CS pin to D10, int8_t SI_PIN = 9; //Set SI pin to D9 int8_t IRQ_PIN = 2; //D2 or D3 are available for IRQ volatile int8_t AS3935_ISR_Trig = 0; #define AS3935_INDOORS 0 //Set to 1 enable 0 disable if your setup is for indoor #define AS3935_OUTDOORS 1 //Set to 1 enable 0 disable if your setup is for outdoor #define AS3935_DIST_DIS 0 //Set disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2) #define AS3935_DIST_EN 1 //Set disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2) #define AS3935_CAPACITANCE 104 void AS3935_ISR(); AS3935 lightning0(CS_PIN, IRQ_PIN, SI_PIN); void setup() { Serial.begin(115200); Serial.println("14CORE | AS3935 Lightning Sensor Test Code"); Serial.println("Initializing........................"); delay(3000); Serial.println("Starting SPI........................"); delay(2000); SPI.begin(); // Set to begin SPI SPI.setClockDivider(SPI_CLOCK_DIV16); // Set the SPI speed to SPI_CLOCK_DIV16/1MHz max 2MHz, NEVER 500kHz SPI.setDataMode(SPI_MODE1); SPI.setBitOrder(MSBFIRST); //Set data sent to chip MSb 1st lightning0.AS3935_DefInit(); // Set the registers to default lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN); attachInterrupt(0, AS3935_ISR, RISING); //Set IRQ input 0 > pin 2, 1 > pin 3 } void loop() { while(0 == AS3935_ISR_Trig){} AS3935_ISR_Trig = 0; //Set reset IRQ flag uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); if(0 == int_src) { Serial.println("IRQ source result not expected"); } else if(1 == int_src) { uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm(); Serial.print("Lightning Detected > Distance Strike >"); Serial.print(lightning_dist_km); Serial.println(" kilometers"); } else if(2 == int_src) { Serial.println("DISTURBANCE DETECTED > "); } else if(3 == int_src) { Serial.println("NOISE LEVEL HIGH > "); } // lightning0.AS3935_PrintAllRegs(); // This line is for debuging.. } void AS3935_ISR() { AS3935_ISR_Trig = 1; //Set IRQ handler interrupts return to vaoid no arg. } |
Source Code for I2C Address Finder / Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <Wire.h> void setup() { Serial.begin(9600); Serial.println("14CORE | i2C SCANNER"); Serial.println("Starting...."); Serial.pirntln("--------------------"); delay(3000); Wire.begin(); while (!Serial); // Scan i2C Address Serial.println("Initializing I2C Scanner"); Serial.println("Please wait.............."); delay(3000); } void loop() { byte error, address; int indevice; Serial.println("14CORE | Scanning please wait..."); indevice = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("14CORE | I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); indevice++; } else if (error==4) { Serial.println("14CORE | Unknow error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (indevice == 0) Serial.println("14CORE | No I2C devices found\n"); else Serial.println("14CORE | Scanning Done\n"); delay(5000); // wait 5 seconds for next scan } |
Downloads
- AS3935 Lightning Sensor Datasheet | PDF
- Download I2C.h Library | Zip
- Download SPI.h Library | Zip
- Download AS3935 Code Library for i2C | Zip
- Download AS3935 Code Library for SPI | Zip
The example code above doesn’t work an a ESP8266.
See your TWI protocol pin configuration which is assigned in your ESP board, be aware of the logic control. if your building a custom board see the ESP8266 chip pinout / you can also use SC18IS600 for SPI to TWI conversion.
May I ask what the significance is in the use of;
AS3935_DIST_DIS 0
AS3935_DIST_EN 1
What affect do these settings have and what setting options are there for the two settings?
My set up using your code appears to work, but I’ve getting a lot of local noise pick up evident by the number of “IRQ source result not expected” if you put your had anywhere near the PCB;
20:24:39.295 -> 14CORE | AS3935 Lightning Sensor Test Code
20:24:39.295 -> Initializing……………………
20:24:42.295 -> Starting SPI……………………
20:24:44.264 -> set up for indoor operation
20:24:44.264 -> disturber detection enabled
20:24:44.779 -> capacitance set to 8×13
20:24:44.779 -> AS3935 manual cal complete
20:24:51.998 -> DISTURBANCE DETECTED >
20:24:52.185 -> IRQ source result not expected
20:24:52.232 -> IRQ source result not expected
20:24:52.232 -> IRQ source result not expected
20:24:52.232 -> IRQ source result not expected
20:24:52.279 -> IRQ source result not expected
20:24:52.279 -> IRQ source result not expected
20:24:52.326 -> IRQ source result not expected
20:24:52.326 -> IRQ source result not expected
20:24:52.326 -> IRQ source result not expected
20:24:52.373 -> IRQ source result not expected
20:24:52.373 -> IRQ source result not expected
Thanks,
K.
Franklin AS3935 has embedded lightning algorithm to checks the incoming signal pattern to reject the potential man-made.
disturbers. #define AS3935_DIST_DIS 0 //Set disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2) #define AS3935_DIST_EN 1 //Set disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2),
Using an Arduino mega and this chip on a board with all the ancillary hardware included.
Simple enough, but I see nothing.
Starting with the address finder/scanner that sees nothing – returns “nothing found” every time.
I notice that in the i2c code above you refer to SI_PIN – but in the circuit diagram you are not connecting SI at all. Is that correct?
Any help would be appreciated of course!
Further to my last note: so it’s wired right; the libraries are present, but the code just returns zeros:
14CPRE | AS3935 Lightning Sensor Test
Starting……………….
wrt: 0 Act: 0
wrt: 20 Act: 0
wrt: 0 Act: 0
wrt: 1C Act: 0
Configured used for outdoor operation
wrt: 0 Act: 0
disturber detection enabled
wrt: 0 Act: 0
wrt: 9 Act: 0
capacitance set to 8×0
AS3935 manual cal complete
Reg 0x00: 0
Reg 0x01: 0
Reg 0x02: 0
Reg 0x03: 0
Reg 0x04: 0
Reg 0x05: 0
Reg 0x06: 0
Reg 0x07: 0
Reg 0x08: 0
0