Franklin Lighting Sensor AS3935

In this example we'll use a sensor which detects lighnings in a range of kilometers (Franklin Lightning Sensor). This device has an antenna that detects lightning and clouds discharge to determine the distance and energy of the thunder detected. The lightning data is stored inside the registry and then will be available to access it.

What do we need

  • an Arduino Yun board
  • a Franklin Lightning Sensor (AS3935)
  • a breadboard
  • a bunch of wires

The circuit

Image   
Be sure to follow the connections shown in the fritzing circuit  and in the next table :

Sensor (Vdd)    => 3.3V
Sensor (GND)   => Arduino GND pin
Sensor (IRQ)     => Arduino PIN 7
Sensor (SCL)     => Arduino PIN 3
Sensor (SDA)    => Arduino PIN 2

The code

#include <SPI.h>
#include <AS3935.h> void printAS3935Registers(); byte SPItransfer(byte sendByte); void AS3935Irq(); volatile int AS3935IrqTriggered; // First parameter - SPI transfer function, second - Arduino pin used for CS // and finally third argument - Arduino pin used for IRQ AS3935 AS3935(SPItransfer,SS,7); void setup() { //Serial.begin(9600); Serial1.begin(230400); Serial1.println("OK"); SPI.begin(); SPI.setDataMode(SPI_MODE1); // NB! max SPI clock speed that chip supports is 2MHz, // but never use 500kHz, because that will cause interference SPI.setClockDivider(SPI_CLOCK_DIV16); SPI.setBitOrder(MSBFIRST); AS3935.reset(); if(!AS3935.calibrate()) Serial1.println("Tuning out of range, check your wiring, your sensor and make sure physics laws have not changed!"); // tell AS3935 we are indoors, for outdoors use setOutdoors() function AS3935.setIndoors(); //AS3935.setOutdoors(); // turn on indication of distrubers, once you have AS3935 all tuned, you can turn those off with disableDisturbers() AS3935.enableDisturbers(); printAS3935Registers(); AS3935IrqTriggered = 0; // ChipKit Max32 - irq connected to pin 7 // attachInterrupt(4,AS3935Irq,RISING); attachInterrupt(4,AS3935Irq,CHANGE); } void loop() { if(AS3935IrqTriggered) { AS3935IrqTriggered = 0; int irqSource = AS3935.interruptSource(); if (irqSource & 0b0001) Serial1.println("Noise level too high, try adjusting noise floor"); if (irqSource & 0b0100) Serial1.println("Disturber detected"); if (irqSource & 0b1000) { // need to find how far that lightning stroke, function returns approximate distance in kilometers int strokeDistance = AS3935.lightningDistanceKm(); if (strokeDistance == 1) Serial1.println("Storm overhead, watch out!"); if (strokeDistance == 63) Serial1.println("Out of range lightning detected."); if (strokeDistance < 63 && strokeDistance > 1) { Serial1.print("Lightning detected "); Serial1.print(strokeDistance,DEC); Serial1.println(" kilometers away."); } } } } void printAS3935Registers() { int noiseFloor = AS3935.getNoiseFloor(); int spikeRejection = AS3935.getSpikeRejection(); int watchdogThreshold = AS3935.getWatchdogThreshold(); Serial1.print("Noise floor is: "); Serial1.println(noiseFloor,DEC); Serial1.print("Spike rejection is: "); Serial1.println(spikeRejection,DEC); Serial1.print("Watchdog threshold is: "); Serial1.println(watchdogThreshold,DEC); } byte SPItransfer(byte sendByte) { return SPI.transfer(sendByte); } void AS3935Irq() { AS3935IrqTriggered = 1; }
Write or paste code in Arduino IDE, then load the sketch on Yun board.

Python Serial Test

Read data from shell. 
To do this we used the included library “pyserial”. You must create a python script to be launched from a terminal. Then connect to your board with ssh, create file serialTest.py with the following code and start it with the command  “python serialTest.py”. Read data now.

import serial
try:
     print "Trying..."
     arduino = serial.Serial('/dev/ttyATH0',230400)
except:
     print "Failed to connect on"
while True:
     x=arduino.readline()
     print x

Referral link: 

Franklin Lightning Sensor AS3935
Franklin Lightning Sensor Library