AS3935 lightning detection using Arduino Uno


Some time ago I bought an AS3935 lightning sensor breakout board from Tindie. After some time I needed another sensor for a new project, only to find that it has been discontinued.

After some googling around I found the Thunder click board manufactured by the Mikroelektronika. The Thunder Click is a breakout board in the mikroBUS form factor, a standardized interface from Mikroelektronika. For the Arduino fans, there is an adapter shield that can accommodate two microBUS devices. Both the Thunder Click and the Click shield are also available from the EU distributor TME.


In this example, the Thunder Click is installed on socket #1 of the adapter shield. The PWR_SEL bridge was set to 5V (originally all mikroBUS boards come set for 3.3V operation). The pin configuration is:

  • AS3935 CS – Arduino D10
  • AS3935 MOSI – Arduino D11
  • AS3935 MISO – Arduino D12
  • AS3935 SCK – Arduino D13
  • AS3935 INT – Arduino D2 (INT). Note this corresponds to Interrupt  #0

If the Socket #2 is used, the CS line connects to Arduino Pin D9, and the INT pin connects to Arduino pin D3, corresponding to Interrupt #1 (see http://arduino.cc/en/Reference/attachInterrupt for reference). Don’t forget to change the code to reflect the new pin settings.

The code is based on the original library written by Raivis Rengelis available at https://github.com/raivisr/AS3935-Arduino-Library with some modifications that are explained below:

First, I added a line to display the value of the calibration register if the calibration is successful.
Second, I moved all the code to display the events inside the interrupt routine itself. The main loop code just displays “Waiting” so I know that the system is not stuck. Now all I have to do is wait for a thunderstorm 🙂

The output of this program looks like this:

Tuning cap register is 7
Noise floor is: 5
Spike rejection is: 2
Watchdog threshold is: 2
Waiting…
Noise level too high, try adjusting noise floor
Waiting…

Waiting…
Disturber detected
Waiting…
Disturber detected
Waiting…
Waiting…
Lightning detected 27 kilometers away.
Storm overhead, watch out!
Waiting…

From time to time the sensor reports “Noise level too high”. A possible workaround and future work is to count the number of noise warnings in a given amount of time, and dynamically adjust the noise level using AS3935.setNoiseFloor() function until a compromise between noise warnings and sensitivity is reached.

Update: Normally Arduino disables all interrupt sources during the ISR routine, this is why they recommend keeping the ISR routine short. Delay() also uses interrupts, and it seems that in very rare situations this causes Arduino to hang. For the moment I consider this as being a minor inconvenience, but I have to see what happens during a summer thunderstorm when tens of interrupts per second will occur.

Update 6 April 2015: Commented AS3935Irqtriggered = 0; on code line 97. It caused an error when compiling.

 

Post a Comment

0 Comments