Maker.io main logo

SparkFun Qwiic AS3935 Lightning Detector Hookup Guide

2019-06-21 | By SparkFun Electronics

License: See Original Project Programmers Arduino

Courtesy of SparkFun

Introduction

The SparkFun Qwiic AS3935 Lightning Detector adds lightning detection to your next weather station or to your next bike ride. Are you worried about the looming clouds in the distance, how far away is that storm exactly? The lightning detector can tell you the distance to the front of the storm 40 km away with an accuracy of 1km. It has false positive rejection and comes with many configurable features. To get at those features we have written a library that gives you access to settings such as storm sensing sensitivity when detecting indoors vs outdoors, or the number of lightning strikes needed to trigger an interrupt! The board supports both I2C and SPI, and so we've whipped out the Qwiic connector to make this easy to integrate into the Qwiic environment.

Required Materials

To follow along with the example code used in this tutorial, you will also need the following materials. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.

If you need different size Qwiic cables we offer a kit that contains many sizes but we also carry them individually as well:

Tools

Depending on your setup, you will may need a soldering iron, solder, and general soldering accessories.

Suggested Reading

If you aren't familiar with the Qwiic system, we recommend reading here for an overview.

qwiic

Qwiic Connect System

We would also recommend taking a look at the following tutorials if you aren't familiar with them.

  • Serial Peripheral Interface (SPI): SPI is commonly used to connect microcontrollers to peripherals such as sensors, shift registers, and SD cards.
  • I2C: An introduction to I2C, one of the main embedded communications protocols in use today.
  • How to Work with Jumper Pads and PCB Traces: Handling PCB jumper pads and traces is an essential skill. Learn how to cut a PCB trace, add a solder jumper between pads to reroute connections, and repair a trace with the green wire method if a trace is damaged.
  • RedBoard Qwiic Hookup Guide: This tutorial covers the basic functionality of the RedBoard Qwiic. This tutorial also covers how to get started blinking an LED and using the Qwiic system.

Hardware Overview

Power

You can provide 3.3V through the Qwiic connector on the board or through the 3V3 labeled pin on the through hole header. When you correctly power the board, the on board power LED will turn on.

2_power

LED

There is one red LED on the product that will turn on when power is supplied to the board. You can disconnect this LED by cutting the jumper on the underside of the product labeled LED.

3_LED

Qwiic Connector or I2C Pins

There is one Qwiic connector on the board to easily connect to the sensor via I2C. If you prefer the old school method of connecting to the I2C pins, we've also broken out those four pins on the side of the board as plated through holes.

4_Pins

Interrupt Pin

The interrupt pin turns high when the lightning detector has sensed an event, whether it's lightning, a disturber, or noise. Make sure to connect to this pin to check if there is an event detected.

5_Interrupt

Jumpers

There are four jumpers on this product all on its underside.

6_Jumpers

From left to right starting at the bottom: There are two address jumpers labeled ADR that allow you to change. The default I2C address is 0x03 but it can be changed to three other options: 0x02, 0x01, 0x00. We recommend 0x03 and 0x02, but if you really know what you're doing then you can also use 0x01 or 0x00. These two addresses are reserved for special I2C commands and you can read more about I2C addressing from this article.

Next to the two address jumpers are the I2C pull-up resistor jumpers. If you have many I2C devices chained together you may have to cut these jumpers. Next to that you, have the SPI jumper that enables SPI when closed. Finally, in the upper right is the power LED disconnect jumper. If that power LED is irritating or unnecessary, then cut that jumper.

Antenna

The large-ish part on the board is the board's antenna. Keep the area around the antenna free as it is what picks up lightning events. With that said, check below for some common sources of false positives and noise.

7_Antenna

False Positives and Noise

There are a number of sources that can cause false positives but the lightning detector itself can reliably filter these out by default. If not, there are a number of settings you can configure using the lightning detector library to increase the chip's robustness to noise and false positives. However, it can help to know some potential sources of noise (from the AS3935 fact sheet) fluorescent lighting, microwave ovens, and switches. From the datasheet, it states that smartphone and smartwatch displays, and DC-DC converters can also trigger as noise. Keep in mind that you would probably have to put your lightning detector on top of your phone for it to register. I found that using the lightning detector in the office was near impossible because I'm surrounded by at least ten computers, three 3D-printers, and numerous soldering irons.

Hardware Assembly

This one is very easy to put together if you have a Qwiic enabled microcontroller like the RedBoard Qwiic. Plug a Qwiic cable between the RedBoard Qwiic and the SparkFun AS3935 Lightning Detector. You will also need to solder wire between the RedBoard Qwiic's pin 4 and the Qwiic AS3935's INT pin. For quick testing, an IC hook was used. For a secure connection, we recommend soldering a header or wire between the two.

8_Hardware

Note: The sensor has a logic level of 3.3V. I have attached the interrupt to pin 4 on the Redboard Qwiic even though the pin is at 5 volts. This will not harm the AS3935 since we're doing a simple digitalRead().

Library Installation

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

We've provided a library for the SparkFun Lightning Detector that configures every available setting the IC offers. Some of the features include modifying the sensitivity of the antenna, whether you're inside or outside, fine tuning when the IC triggers an interrupt, or modifying the resonance frequency of the antenna. You can search the Arduino Library Manager for "SparkFun Lightning Detector", or you can click the button below to get the library and install it manually. Finally, you can also download it manually from the GitHub Repo.

SPARKFUN AS3935 LIGHTENING DETECTOR LIBRAY (ZIP)

Example Code

The example below will start with the basic functionality in Example1_BasicLightning.ino. Open the example up to follow along.

At the very top, we have a few defines that will help us to distinguish what sort of event the lightning detector has sensed. There are three possible "events". The first of course is lightning, but we may also get a false lightning event called a disturber, and finally we may hear noise.

Copy Code
#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"

// 0x03 is default, but the address can also be 0x02, 0x01, or 0x00
// Adjust the address jumpers on the underside of the product.
#define AS3935_ADDR 0x03
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

// If you using SPI, instantiate class without address:
//SparkFun_AS3935 lightning;

// If you're using I-squared-C then keep the following line. Address is set to
// default.
SparkFun_AS3935 lightning(AS3935_ADDR);

// Interrupt pin for lightning detection
const int lightningInt = 4;
int noiseFloor = 2;

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector.
int intVal = 0;

In the setup, we set the IC to be run inside because I'm assuming you're at your computer running this code. If you're outside change the parameter to OUTDOOR.

Copy Code
void setup()
{
// When lightning is detected the interrupt pin goes HIGH.
pinMode(lightningInt, INPUT);

Serial.begin(115200);
Serial.println("AS3935 Franklin Lightning Detector");

//SPI.begin()
Wire.begin(); // Begin Wire before lightning sensor.

if( !lightning.begin() ) { // Initialize the sensor.
//if( !lightning.beginSPI(9, 2000000){ // Uncomment for SPI.
Serial.println ("Lightning Detector did not start up, freezing!");
while(1);
}
else
Serial.println("Schmow-ZoW, Lightning Detector Ready!");

// The lightning detector defaults to an indoor setting at
// the cost of less sensitivity, if you plan on using this outdoors
// uncomment the following line:
//lightning.setIndoorOutdoor(OUTDOOR);
}

...and finally the meat. Here we're just monitoring the interrupt pin. If the pin reads high, then the IC has heard some sort of event. Afterwards we're going to read the interrupt register to see what the event is, whether it's lightning, a disturber, or noise. Each of these events are printed out in the serial window at 115200 baud. The lightning event however will also print out the estimated distance to the front of the storm. Keep in mind that this is not the distance to the lightning but rather the storm.

Copy Code
void loop()
{
if(digitalRead(lightningInt) == HIGH){
// Hardware has alerted us to an event, now we read the interrupt register
// to see exactly what it is.
intVal = lightning.readInterruptReg();
if(intVal == NOISE_INT){
Serial.println("Noise.");
//reduceNoise(); //See note below above reduceNoise function.
}
else if(intVal == DISTURBER_INT){
Serial.println("Disturber.");
}
else if(intVal == LIGHTNING_INT){
Serial.println("Lightning Strike Detected!");
// Lightning! Now how far away is it? Distance estimation takes into
// account any previously seen events in the last 15 seconds.
byte distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
Serial.println("km away!");
}
}
delay(100); //Let's not be too crazy.
}

In case you're sitting at a computer and there's a lot of RF noise in your area then you may need a way to make the lightning detection less sensitive to these events. Call this function and feed it a number lower than seven to increase it's noise threshold. Likewise, you could put this function in the noise if-statement listed above and it will increase that threshold every time a noise event occurs. This may give you an idea of which number works best for the area you're in.

Copy Code
void reduceNoise(){
++noiseFloor; // Manufacturer's default is 2 with a max of 7.
if(noiseFloor > 7){
Serial.println("Noise floor is at max!");
return;
}
Serial.println("Increasing the event threshold.");
lightning.setNoiseLevel(noiseFloor);
}

If you've heard some lightning, than you'll see the following in your Arduino serial window.

9_window

Resources and Going Further

For more on the AS3935, check out the links below:

Need some other weather sensing parts for your project? Check out some of the ones listed below.

制造商零件编号 DEV-15123
REDBOARD QWIIC ATMEGA328 EVAL BD
SparkFun Electronics
制造商零件编号 UR050-006
CBL USB2.0 A PLUG-MCR B PLUG 6'
Eaton Tripp Lite
制造商零件编号 PRT-14426
QWIIC CABLE - 50MM
SparkFun Electronics
制造商零件编号 CAB-09741
TEST LEAD HOOK TO TIP PLUG 2.5"
SparkFun Electronics
制造商零件编号 KIT-15081
QWIIC CABLE KIT
SparkFun Electronics
制造商零件编号 PRT-14427
QWIIC CABLE - 100MM
SparkFun Electronics
制造商零件编号 PRT-14425
QWIIC CABLE - BREADBOARD JUMPER
SparkFun Electronics
制造商零件编号 PRT-14429
QWIIC CABLE - 500MM
SparkFun Electronics
制造商零件编号 TOL-14456
SOLDERING IRON - 60W (ADJUSTABLE
SparkFun Electronics
制造商零件编号 SEN-08942
WEATHER METER KIT
SparkFun Electronics
制造商零件编号 SEN-14348
QWIIC ENVIRONMENTAL COMBO BRKOUT
SparkFun Electronics
制造商零件编号 SEN-12909
SPARKFUN PRESSURE SENSOR BREAKOU
SparkFun Electronics
制造商零件编号 DEV-13956
WEATHER SHIELD
SparkFun Electronics
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.