<< Click to Display Table of Contents >>

Navigation:  Bits and PCs Blog - 2025.11.19 >

AS3935 Lightning Sensor

Previous pageReturn to chapter overviewNext page

Benjamin Franklin nearly electrocuted himself by flying a kite into a thunderstorm. This is not recommended (see Disclaimer). He realized that lightning was a natural phenomena which could be explained by Science.

Before that, the general opinion was that it was Zeus's weapon against Cronus and the Titans (if you were a Greek), or was caused by Thor showing off with his Hammer (if you were a Norseman). Both of these widely-held beliefs have recently been proved to be totally wrong. These days, thanks to modern technology, you can safely use the Franklin Lightning Sensor™ for atmospheric lightning detection, even in the comfort of your own living room (see Disclaimer, again).

lightning-1

 

As far as I know, there is only one chip which detects lighting strikes, the AS3935. This chip is over 10 years old (Jan 2014) and it still has no competition. It has a rather varied history, but is currently marketed by ScioSense.
https://www.sciosense.com/franklin-lightning-sensor/

They say, "Lightning is a leading cause of storm deaths and causes $ billions of damage." This is true, but the Franklin Lightning Sensor™ gives you precious time to move yourself - and your real estate - out of the way, so Thor's Hammer won't do any damage.

"The AS3935 is a programmable fully integrated Lightning Sensor IC that detects the presence and approach of potentially hazardous lightning [and Thor's hammer] activity in the vicinity and provides an estimation of the distance to the head of the storm."

The lightning detector will [probably] detect nuclear explosions too, but I have not been able to test that yet, and it's not in the manufacturer's recommended application list.

There are two common modules which use this chip, the Gravity/DFRobot board and the Sparkfun board, see the references at the end of this page. For serial communications, the Gravity board uses only I2C, whereas the Sparkfun board uses SPI. The chip supports both I2C and SPI (but not at the same time). I found a "Playing with fusion" board too, but I don't think you can buy it on this side of the pond.

as3935-gravity   as3935-sparkfun

 

Matt's Tip

Building a lightning detector is not very satisfying if there are no lightning storms for several months (speaking from experience). You need to have your detector connected and running within a few minutes or you may miss it. Most storms only have a few strikes, and are over in 10 or 15 minutes.

 

Summary

Here's a slightly shorter summary of the 40-page data sheet, so you don't have to read it...

The chip runs on 3.3V or 5V. The Sparkfun board has 3V3 written on the pin, but it also works on 5V. NOTE: If using 3.3V microcontroller, always use 3.3V to power the board, NOT 5V!

The board has an inductor/capacitor resonant circuit as an antenna, which is included on the module. This must be tuned to resonate at 500kHz +-3%, see below.

The chip's Analog Front End (AFE) detects incoming signals that are above the "watchdog threshold" and alerts the "lightning algorithm block". This uses a "disturber rejection algorithm" to determine if it's a genuine lightning strike or a disturber event.

A disturber event is a man-made (or machine-made) source of interference that could be misinterpreted as a lightning strike. If the algorithm determines the signal was a disturber, it disables the sensor for 1.5 seconds. Background noise is also registered, as the sensor will not work if the background noise is too high.

The sensitivity of these checks can be configured, see the Mattlabs' library methods setAFEGain(), setWatchdogThreshold(), setSpikeRejection() and setNoiseFloorLevel(). But it's probably best to leave these at their default values because it's difficult to test. For indoor or outdoor use, setAFEGain() has two optimal settings (INDOOR and OUTDOOR), the default is INDOOR.

If it's determined to be a lightning strike, the event is stored in an on-chip circular buffer from which it uses a statistical algorithm to estimate the distance of the storm in kilometres. Further strikes will not be detected for 1 second. The circular buffer can be cleared with clearStatistics(), which sets the storm distance to 0.

 

Readings

The distance to the leading edge of the storm is estimated in km, and returned by getStormDistance(). 1 = directly overhead (disconnect your computer, TV and HiFi!), 63 = out of range (get back on the sofa).

When a lightning strike is detected, it stores the energy level which can be read with getStrikeEnergy() as a unitless 21-bit value.

If using the IRQ output as an event signal, call getIRQSource() to determine what happened, but wait 2ms, see below. Or simply poll with getIRQSource() to see if anything was detected.

 

IRQ output

The active-high IRQ output is turned on when:

A programmed number of lightning strikes have occurred, see  setMinimumStrikes()

Noise levels are exceeded and measurements can no longer be taken, see setNoiseFloorLevel()

A disturber event was detected, this interrupt can be disabled with disableDisturberInterrupt()

You could use the IRQ output to generate a interrupt, but I think it's best just to poll it as a status pin, which is way faster than polling the chip with I2C messages. If IRQ goes high, you must wait 2ms before determining the interrupt reason with getIRQSource().

The signal from the internal oscillators can also be connected to  the IRQ output pin for frequency measurement and calibration, see selectIRQOutput().

See data sheet p34.

 

I2C address

The I2C address is defined by how A1 and A0 are connected. My Gravity/DFRobot module uses switches to assign the address. Both OFF = 0x03.
0=switch ON, 1=OFF

A1

A0

I2C Address

0

0

INVALID

0

1

0x01

1

0

0x02

1

1

0x03

The chip supports SPI communications too, but the Gravity board allows only I2C. If you need SPI for the SparkFun board, just add a new begin() and write new readRegister() and writeRegister() methods for SPI.

 

Tuning the antenna's resonant frequency

The antenna has an LCO oscillator (LCO = inductor/capacitor oscillator) which should be tuned to have a resonant frequency of 500kHz +-3.5%.

The oscillator frequency can be output on the IRQ pin for measurement with an accurate frequency counter, see selectIRQOutput().

Tuning is done using setTuningCapacitor() to select the TUN_CAP bits for 0..120pF in steps of 8pF.

Once you have this value, put it in the code so it's configured in setup().

See data sheet p35.

 

Clock frequency calibration

The chip contains two RC (resistor-capacitor) oscillators:

SRCO System RC Oscillator, 1.1MHz

TRCO Timer RC Oscillator, 32.768kHz

Unlike the antenna, these do not need manual calibration. They are calibrated internally by calling calibrateOscillators(). Always call this after begin(), reset(), powerOn() or setTuningCapacitor(). These clocks are derived from the antenna LCO oscillator, which is why setTuningCapacitor() should be called first.

If you want to check the SRCO and TRCO frequencies, you can use selectIRQOutput() to output the SRCO or TRCO signal on the IRQ pin. The data sheet implies these should be affected by the resonant frequency, but changing the antenna resonant frequency with setTuningCapacitor() did not seem to have any effect on these frequencies.

See data sheet p35-36.

 

Testing

You can either wait for the next thunderstorm (ref. Benjamin Franklin), or use some kind of spark generator, like a Tesla Coil. This will be wildly inaccurate but you will get a reaction, mostly disturbers. I used a 1970's ZEROSTAT antistatic pistol, a piezo-electric device which generates a spark if you pull the trigger too fast. Piezo cigarette lighters will do the same thing. Keep the spark a long way away from the electronics!

>>> TAKE CARE! High voltage electrostatic discharges are LETHAL to modern electronics! See Disclaimer! <<<

Alternatively, make a continuous spark generator with the ignition coil from that old rusting Maserati in your garage, see the Optically Isolated Ignition Coil Driver article.

Tesla coils and piezo-electric devices caused I2C communications errors for me (endtx failed), and the I2C communications hung up. This was probably due to interference through the long off-board I2C wiring. I added the [temporary] restartWire() method which lets it recover.

If you hold your mobile phone close to the antenna inductor, it will pick up a lot of INT_D disturber events.

If your lucky enough to get a real thunder storm, the real-time MeteoSwiss 'Precipitation (Radar)' map will show the lightning strikes detected by its network, see the reference link at the end of the page.

 

Gravity board schematic

Click image to zoom in/out

The important thing to notice is that there is a 10K pull-up on the SCL line, but no pull-up on the SDA line. This is OK because the 10K SDA pull-up is in the chip. This means you do not need I2C pull-ups. In fact, if your controller board has 4K7 pull-ups fitted then it will not work! (10K may be OK, but I didn't try it.) The Arduino Zero didn't work because it had 4K7 pull-ups fitted - these had to be removed. The data sheet states, "please do not use 4K7 ohm resistors on both I2CD and I2CL" (SDA and SCL).

 

The Mattlabs' code

This code has many advantages over the official library. It has been extensively tested on a 32-bit SMT32F103, but should work on most controllers with a few minor modifications.

It's in a single include file (.h), containing both the class definition and the code. Separate .h and .cpp files are not needed if you use it in only one source file.

It supports I2C communications, but it's easy to add SPI support: Just add a new begin(SPI* spi, uint csPin) and write new readRegister() and writeRegister() methods for SPI. Maybe I'll do that when I get back from the pub...

  AS3935LightningDetector.h   [Click to show/hide the code]

Here's an example sketch which polls IRQ every 500ms.

  AS3935LightningDetector.ino   [Click to show/hide the code]

 

A Windows Lightning Detector App is in development...

See AS3935 Lightning Detector Application.

lightning-detector-app-3

 

References

AS3935 data sheet

All page references in the code refer to THIS version of the data sheet
AS3935 Data Sheet PDF, [v1-04] 2016-Jan-13
https://www.sciosense.com/wp-content/uploads/2023/12/AS3935-Data-Sheet.pdf

RFID Transponder Coil – MA5532-AE

https://www.coilcraft.com/getmedia/df53f62b-d229-48dc-aca7-27a25052f1de/ma5532.pdf

 

Gravity/DFRobot module and library

https://wiki.dfrobot.com/Gravity:%20Lightning%20Sensor%20SKU:%20SEN0290
https://www.bastelgarage.ch/gravity-as3935-lightning-distance-sensor
https://github.com/DFRobot/DFRobot_AS3935/tree/master

Sparkfun module and library

https://learn.sparkfun.com/tutorials/sparkfun-as3935-lightning-detector-hookup-guide-v20/introduction
https://github.com/sparkfun/SparkFun_AS3935_Lightning_Detector_Arduino_Library

"Playing with fusion" lightning sensor tester and board

https://www.playingwithfusion.com/products/55
https://www.playingwithfusion.com/products/135

 

MeteoSwiss Lightning Detection Network

The real-time 'Precipitation (Radar)' of MeteoSwiss has a 'Lightning detected' button, which reveals what's been detected by their network.

https://www.meteoswiss.admin.ch/services-and-publications/applications/precipitation.html
https://www.meteoswiss.admin.ch/weather/measurement-systems/atmosphere/lightning-detection-network.html

meteoswiss-lightning-detected

 

An alternative lightning detector

This is really good, if you have a large attic.

https://www.giangrandi.org/electronics/lightning/lightning.shtml