Prototyping an outdoor lightning warning system.
I feel quite lucky to live and work so close to the Rocky Mountains here in Colorado, and it’s a common hobby among us at SparkFun to be out hiking/biking/kayaking or climbing. The mountains contain numerous hazards, not the least of which is lighting strikes. The weather can change suddenly and drastically around these parts. There is a common Colorado saying, “If you don’t like the weather, wait 5 minutes." Unfortunately, this also applies to weather you are enjoying and it can be easy to find yourself suddenly looking at a stormfront that the weather station has assured you would pass to the south. Prevention is always ideal, but even a small amount of lightning prescience can allow you to find cover or start that rappel sooner.
SparkFun is releasing an updated version of our AS3935 Lightning Detector this week. We have had fun using it around the office to confirm that indeed it is raining, and there was strong desire to see this outdoors where it belongs. I decided to start something and get this idea into the prototype phase.
My goals for the outdoor detector were:
The new Lightning Detector goes live tomorrow, and luckily I have one that "fell off" the initial run.
To start, I wanted to pick a microcontroller that fit my needs: the RedBoard Turbo. This was an easy choice. It is battery powered, can communicate over SPI to the AS3935 Lightning Detector, uses 3.3V for its I/O, and even has a Qwiic connector! This is my go-to prototyping with a battery board.
Next, I needed a way to display the data the detector was receiving and sending back to the RedBoard Turbo. The Qwiic Micro OLED was a perfect choice.
With a simple Qwiic Cable, this board connected over I2C and, using example code from the hookup guide, it was displaying data in minutes.
I used a small buzzer from the SIK and some jumper cables for hookup and a lipo battery for power.
Below is the code used with the SAMD21 RedBoard Turbo, Lightning Detector and Qwiic Micro OLED.
/*
This example demonstrates the code used on the Outdoor Lighting Warning Prototype
License: This code is public domain
*/
#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"
#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01
SparkFun_AS3935 lightning;
const int lightningInt = 3;// Interrupt pin for lightning detection
int spiCS = 4; //SPI chip select pin
// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector.
int intVal = 0;
int noise = 2; // Value between 1-7
int disturber = 2; // Value between 1-10
//The library assumes a reset pin is necessary. The Qwiic OLED has RST hard-wired, so pick an arbitrarty IO pin that is not being used
#define PIN_RESET 9
//The DC_JUMPER is the I2C Address Select jumper. Set to 1 if the jumper is open (Default), or set to 0 if it's closed.
#define DC_JUMPER 1
bool warmedup = false;
int distanceview = 3;
const int buzzerPin = 9;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
int beats[] = {1, 1, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1, 1, 1, 4, 4, 2};
int tempo = 113;
//////////////////////////////////
// MicroOLED Object Declaration //
//////////////////////////////////
MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
void setup()
{
// When lightning is detected the interrupt pin goes HIGH.
pinMode(lightningInt, INPUT);
SerialUSB.begin(9600);
delay(50);
SerialUSB.println("AS3935 Franklin Lightning Detector");
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
if (warmedup == false)
{
warmup();
}
SPI.begin();
if (digitalRead(lightningInt) == HIGH)
{
intVal = lightning.readInterruptReg();
SerialUSB.print("intvalis");
SerialUSB.println(intVal);
if (intVal == NOISE_INT) {
SerialUSB.println("Noise.");
// Too much noise? Uncomment the code below, a higher number means better
// noise rejection.
lightning.setNoiseLevel(noise);
}
else if (intVal == DISTURBER_INT) {
SerialUSB.println("Disturber.");
// Too many disturbers? Uncomment the code below, a higher number means better
// disturber rejection.
lightning.watchdogThreshold(disturber);
}
else if (intVal == LIGHTNING_INT) {
SerialUSB.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();
SerialUSB.print("Approximately: ");
SerialUSB.print(distance);
SerialUSB.println("km away!");
distanceview = distance;
lightningData();
buzzing();
}
delay(100); // Slow it down.
}
}
void warmup()
{
SPI.begin();
if ( !lightning.beginSPI(spiCS, 2000000) ) {
SerialUSB.println ("Lightning Detector did not start up, freezing!");
while (1);
}
else
SerialUSB.println("Schmow-ZoW, Lightning Detector Ready!");
warmedup = true;
lightning.setIndoorOutdoor(OUTDOOR);
int enviVal = lightning.readIndoorOutdoor();
SerialUSB.print("Are we set for indoor or outdoor: ");
if ( enviVal == INDOOR )
SerialUSB.println("Indoor.");
else if ( enviVal == OUTDOOR )
SerialUSB.println("Outdoor.");
else
SerialUSB.println(enviVal, BIN);
}
void lightningData()
{
delay(100);
Wire.begin();
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.display(); // Display what's in the buffer (splashscreen)
delay(1000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
printTitle("Storm!", 0);
oled.clear(PAGE); // Clear the screen
oled.setFontType(0); // Set font to type 0
oled.setCursor(0, 0); // Set cursor to top-left
delay(50); // Wait 500ms before next example
oled.clear(PAGE); // Clear the display
oled.setCursor(0, 0); // Set cursor to top-left
oled.setFontType(0); // Smallest font
oled.print("Distance: ");
oled.setCursor(16, 12);// Print "A0"
oled.setFontType(2);
oled.print(distanceview);
oled.setCursor(0, 34);
oled.setFontType(0);
oled.print("Km Away!");
oled.display();
Wire.end();
}
void printTitle(String title, int font)
{
int middleX = oled.getLCDWidth() / 2;
int middleY = oled.getLCDHeight() / 2;
oled.clear(PAGE);
oled.setFontType(font);
// Try to set the cursor in the middle of the screen
oled.setCursor(middleX - (oled.getFontWidth() * (title.length() / 2)),
middleY - (oled.getFontHeight() / 2));
// Print the title:
oled.print(title);
oled.display();
delay(1500);
oled.clear(PAGE);
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return (frequencies[i]); // Yes! Return the frequency
}
}
return (0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
void buzzing()
{
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo / 10); // brief pause between notes
}
}
Next was to create an enclosure for the project. Luckily we have a CO2 laser cutter and plenty of clear acrylic! I tossed together a box design with some 0.8-inch spaced standoff holes to mount the sensor, display and microcontroller.
I later added large 0.75-inch holes to pass a sling through.
Here are a few pictures of the enclosure being used with a harness.
I had a lightning emulator, which helped greatly in the development process, but there was a large amount of disturbance events here at SparkFun and the real test will be bringing it outside with me. With everything hooked up, we get both a auditory indication of a lightning strike being detected and a display of distance on the OLED. When all the screws, standoffs, lipo battery and sling were added, the total enclosure weighed about 5 oz or 0.3 lbs. Although this a tolerable weight to bring backpacking or climbing, I feel like there is a lot of room to decrease the weight of the project. I am very excited to bring this into the woods and the on the rocks with me, update the design and report back to you all with my findings.