In this project, we'll create a small device that alerts you to nearby lightning strikes. The total cost of all materials in this project will be cheaper than purchasing a commercial lightning detector, and you'll get to hone your circuit-making skills in the process!
The sensor used in this project can detect lightning strikes up to 40 km away, and is also capable of determining the distance of a strike to within a tolerance of 4 km. While this is a reliable sensor, you should never depend upon it to warn you of lightning strikes if you're outdoors. Your own circuit handiwork will not be as reliable as a commercial lightning detector.
This project is based upon the AS3935 lightning sensor IC, with a carrier circuit from DFRobot. It detects electromagnetic radiation that's characteristic of lightning and uses a special algorithm to convert this information to a distance measurement.
Supplies:
This project only requires a few parts. Information is outputted to the user via a piezo buzzer, and the circuit is powered through a lithium ion polymer battery. Below is a complete list of all the parts:
DFRobot Lightning Sensor
DFRobot Beetle
DFRobot LiPoly Charger
Piezo buzzer (only need one - many different types work)
500 mAh LiPoly (any 3.7V LiPoly will work)
Slide switch (any small switch will work)
In addition to these items, you'll want the following tools/items:
Soldering iron
Solder
Hookup wire
Wire strippers
Hot glue gun
I also detail the process of creating a 3D-printed case for this project. If you don't have a 3D printer, operating the device without a case is still fine.
Step 1: The Circuit
As there's a relatively small number of parts in this build, the circuit is not particularly intricate. The only data lines are the SCL and SDA lines for the lightning sensor and one connection for the buzzer. The device is powered by a lithium ion polymer battery, so I decided to also integrate a lipoly charger into the circuit.
The above image depicts the entire circuit. Note that the connection between the lipoly battery and the lipoly battery charger is via the JST male/female connectors and does not require soldering. See the video at the beginning of this project for further details on the circuit.
Step 2: Circuit Assembly
Final Circuit Before Folding
Final Circuit After Folding
This device is a great candidate for a circuit assembly technique known as free-forming. Rather than affix the parts in this project to a substrate such as a perf board, we will instead just connect everything with wires. This makes the project much smaller, and it is somewhat faster to assemble, but generally produces less aesthetically pleasing results. I like to cover my free-formed circuits with a 3D-printed case at the end. The video at the beginning of this project details the free-forming process, but I will go over all the steps I took textually as well.
First Steps
The first thing I did was unsolder the green terminal blocks from the lipoly charger. These aren't needed, and take up space. I then connected the "+" and "-" terminals of the lipoly charger to the "+" and "-" terminals at the front of the Beetle. This feeds the raw voltage of the lipoly battery straight into the microcontroller. The Beetle technically needs 5V, but it will still operate on the roughly 4V from the lipoly.
Wiring the Lightning Sensor
I then cut the included 4-pin cable such that roughly two inches of wire remained. I stripped the ends, plugged the cable into the lightning sensor, and made the following connections:
"+" on the lightning sensor to "+" on the Beetle
"-" on the lightning sensor to "-" on the Beetle
"C" on the lightning sensor to the "SCL" pad on the Beetle
"D" on the lightning sensor to the "SDA" pad on the Beetle
I also connected the IRQ pin on the lightning sensor to the RX pad on the Beetle. This connection needed to go to a hardware interrupt on the Beetle, and the RX pad (pin 0) was the only interrupt-capable pin remaining.
Wiring the Buzzer
I connected the short lead of the buzzer to the "-" terminal on the Beetle (ground), and the long lead to pin 11. The buzzer's signal pin should be connected to a PWM pin for maximum versatility, which pin 11 is.
Switching the Battery
The last thing necessary is to add a switch inline to the battery to turn the project on and off. To do this, I first soldered two wires to adjacent terminals on the switch. I fixed these in place with hot glue, as the switch's connections are fragile. I then cut the red wire on the battery about halfway down, and soldered the wires coming off the switch to each end. Make sure you cover the exposed sections of wire with heat shrink tubing or hot glue, as these could easily come in contact with one of the ground wires and make a short. After adding the switch, you can plug the battery into the battery charger.
Folding Everything In
The last step is to take the gangly mess of wires and components and make it look somewhat presentable. This is a delicate task, as you want to be sure that you don't break any wires. I first started by hot gluing the lipoly charger to the top of the lipoly battery. I then glued the Beetle on top of that, and finally glued the lightning sensor at the very top. I left the buzzer to sit off to the side, as shown in the above image. The final result is a stack of boards with wires running throughout. I also left the switch's leads to run freely, as I later wish to integrate those into a 3D-printed case.
Step 3: Programming
The software for this circuit is simple at the moment but is heavily customizable to suit your needs. You can find it in the "code" section. When the device detects lightning, it will first beep many times to alert you that lightning is nearby, then beep a certain number of times corresponding to the lightning's distance. If the lightning is less than 10 kilometers away, the device will emit one long beep. If it is more than 10 km from you, the device will divide the distance by ten, round it, and beep that many times. For example, if lightning strikes 26 km away, the device will beep three times.
The entire software revolves around interrupts from the lightning sensor. When an event is detected the lightning sensor will send the IRQ pin high, which triggers an interrupt in the microcontroller. The sensor can also send interrupts for non-lightning events, such as if the noise level is too high. If the interference/noise is too high, you'll need to move the device away from any electronics. The electromagnetic radiation coming from these devices can easily dwarf the comparatively weak electromagnetic radiation from a distant lightning strike.
To program the microcontroller you can utilize the Arduino IDE - make sure the board selection is set to "Leonardo." You will also need to download and install the library for the lightning sensor. You can find this here.
Step 4: 3D-Printed Case
Case Rendered in Carbon Fiber
Case Rendered in ABS
I modeled a case for my device. Your free-form circuit will likely have different dimensions, but I tried to make my case big enough such that many different designs can still fit in it. You can download the files here, and then print them out. The top of the case snaps on to the bottom, so no special parts are required for the case.
You can also try making a model of your own device and creating a case for it. I detail this process in the video at the beginning of this project, but the basic steps to follow are as such:
Capture the dimensions of your device
Model your device in a CAD program (I like Fusion 360 - students can get it for free)
Create a case by offsetting a profile from the device model. A tolerance of 2 mm generally works well.
Step 5: Using Your Device and More
Congratulations, you should now have a fully-functioning lightning detector! Before using the device for real, I recommend waiting until there's a thunderstorm around you to make sure that the device actually is capable of detecting lightning. Mine worked the first try, but I do not know the reliability of this sensor.
Charging the device is simple - you can just plug a micro-USB cable into the lipoly charger until the charging light turns green. Make sure that the device is on while you charge it, or no power will go to the battery! I also recommend changing the beeps to something that you like more; you can use the Tone.h library to generate more pleasant-sounding notes.
Let me know in the comments if you have any problems or questions. To see more of my projects, check out my website www.AlexWulff.com.
Schematics
Device Circuit
Code
Lightning (Arduino)
/*
* Original Sketch from DFRobot.com
* Modified by Alex Wulff (www.AlexWulff.com)
* on 7/25/19
*/
#include "Lib_I2C.h"
#include "DFRobot_AS3935_I2C.h"
#define AS3935_CAPACITANCE 96
// Indoor/outdoor mode selection
#define AS3935_INDOORS 0
#define AS3935_OUTDOORS 1
#define AS3935_MODE AS3935_INDOORS
// Enable/disable disturber detection
#define AS3935_DIST_DIS 0
#define AS3935_DIST_EN 1
#define AS3935_DIST AS3935_DIST_EN
// I2C address
#define AS3935_I2C_ADDR AS3935_ADD3
#define IRQ_PIN 0
#define BUZZ 11
#define LIGHT 13
void AS3935_ISR();
DFRobot_AS3935_I2C lightning0((uint8_t)IRQ_PIN, (uint8_t)AS3935_I2C_ADDR);
// Needs to be voltatile because this variable is accessed in an ISR
volatile int8_t AS3935_ISR_Trig = 0;
void setup()
{
Serial.begin(115200);
Serial.println("Lightning Sensor Start");
I2c.begin();
I2c.pullup(true);
I2c.setSpeed(1);
delay(2);
lightning0.defInit();
lightning0.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST);
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), AS3935_ISR, RISING);
pinMode(BUZZ, OUTPUT);
pinMode(LIGHT, OUTPUT);
}
void loop()
{
// blink light while waiting for lightning
while (AS3935_ISR_Trig == 0) {
digitalWrite(13, HIGH);
delay(700);
digitalWrite(13, LOW);
delay(200);
}
delay(5);
// Reset interrupt flag variable
AS3935_ISR_Trig = 0;
// Get interrupt source
uint8_t int_src = lightning0.getInterruptSrc();
// lightning nearby
if (int_src == 1) {
uint8_t lightning_dist_km = lightning0.getLightningDistKm();
if (lightning_dist_km == 1) {
Serial.println("Lightning overhead!");
}
else if ((lightning_dist_km >= 5) && (lightning_dist_km <= 40)) {
Serial.println("Lightning occurs!");
Serial.print("Distance: ");
Serial.print(lightning_dist_km);
Serial.println(" km");
}
else if (lightning_dist_km == 0x3F){
Serial.println("Out of range.");
}
// Get lightning energy intensity
uint32_t lightning_energy_val = lightning0.getStrikeEnergyRaw();
Serial.print("Intensity: ");
Serial.print(lightning_energy_val);
Serial.println("");
// round to nearest tens place
float distf = (float)lightning_dist_km;
int dist_tens = (int)roundf(distf / 10);
// buzz to get attention
for (int i = 0; i < 20; i++) {
digitalWrite(BUZZ, HIGH);
delay(25);
digitalWrite(BUZZ, LOW);
delay(25);
}
if (lightning_dist_km <= 5) {dist_tens = 1;}
// buzz to indicate distance
for (int i = 0; i < dist_tens; i++) {
digitalWrite(BUZZ, HIGH);
delay(1000);
digitalWrite(BUZZ, LOW);
delay(1000);
}
}
// interference detected
else if (int_src == 2)
{
for (int i = 0; i < 3; i++) {
digitalWrite(LIGHT, HIGH);
delay(200);
digitalWrite(LIGHT, LOW);
delay(200);
}
Serial.println("interference detected");
}
// too much noise
else if (int_src == 3)
{
for (int i = 0; i < 5; i++) {
digitalWrite(LIGHT, HIGH);
delay(100);
digitalWrite(LIGHT, LOW);
delay(100);
}
Serial.println("Noise level too high!");
}
}
//ISR function just sets the flag to 1
void AS3935_ISR()
{
AS3935_ISR_Trig = 1;
}
This article copied from hackster.io, Author: Alex Wulff