AS3935 lightning detector with Wemos D1 Mini

Lightning detection has been on my wish list for my weather station for some time. Detectors often come as stand alone units with a display or indicator LEDs. This makes sense when the use case involves taking it along as a hand held device or installing it on your boat. However, in order to process lightning strike events and display them on the meteodrenthe.nl web page, I need some kind of interface that allows me to do so.

There are a few weather stations that come with a lightning detector. Ecowitt has the WH57. There are also some cheaper Chinese clones that could do the trick. After some checking, I do believe I could setup the gateway in such a way that allows custom processing of the data. But since I’ve been into using sensors with Wemos (Arduino) microcontrollers, my first attempt to make this work is with the AS3935 lightning detector sensor. It’s not particularly cheap. I ordered one from Amazon for just under 30 euro’s.

The sensor comes with an I2C and an SPI interface. Being familiar with I2C, I figured it would be a piece of cake to connect the sensor. Getting it to work took a slightly more effort than I expected. In the end I got the SPI interface working first.

SPI means connecting pins D5 through D8 on the Wemos as well as the 3.3V and G (as it appears on the Wemos board, otherwise GND or ground) pins. On top of that there 2 other pins required. First SI or Select Interface. This pin must be connected to G on the Wemos to tell the sensor to use the SPI interface rather than I2C. (Using I2C requires connecting 3.3V to SI in stead of G). Then there is the IRQ pin. Because we want to detect lightning strike events and we do not know when they might occur, the sensor needs a way to tell the Wemos something happened. This is what the IRQ or interrupt pin is for. The sensor will send a signal to the interrupt pin when a lightning strike has been detected. In my setup it is connected to D1, but any available pin other than D0 can be used.

The three remaining pins, EN_V, A0 and A1 are left unconnected in the current setup. EN_V is only important when connecting the chip to 5V.

So, in SPI mode, the AS3935 pins are connected to the Wemos D1 Mini as follows:

AS3935Wemos D1 Mini
VCC3.3V
GNDG
SCLD5 (SCK)
MISOD6 (MISO)
MOSID7 (MOSI)
CSD8
SIG
IRQD1

Note that both GND and SI are connected to the same pin on the Wemos. You can use a breadboard to realize this, but for testing anything will do. I have been using lever nuts (as they are apparently called in English) to link two wires to the same pin in simple setups.

Then, the code! There are a few libraries and examples out there that will work. Searching in the Arduino IDE library manager will currently give three results:

I have had succes with the AS3935MI library, as well as the Sparkfun library. Since the latter is officially intended for the Sparkfun version, I went with the AS3935MI library. It has an example called AS3935MI_LightningDetector_SPI, which works for me. Be sure to ignore the pin setup mentioned at the top and adjust the values for PIN_IRQ and PIN_CS. To match D1 and D8, set them as follows:

#define PIN_IRQ 5
#define PIN_CS 15

Now upload the code in regular fashion to your Wemos board. Open your serial monitor at 9600 baud and you should see this message after startup:

11:27:30.621 -> Initialization complete, waiting for events…

Subsequently, the code will start to adjust the sensitivity of the sensor, depending on the amount of noise detection. This will create a bunch of logs in the serial monitor, and will go on indefinitely.

So now, how to test if the sensor is actually working? The Netherlands isn’t necessarily known for its frequent thunder storms. The internet has a bunch of suggestions how to fake a lightning strike. That is, how to fake the signal generated by a strike. None of those suggestions were working for me. I was tweaking some code when the serial monitor suddenly lit up with a bunch of these messages:

21:31:11.094 -> Lightning detected! Storm Front is 14km away.

As it turns out, my phone was lying just a few centimeters from the sensor and I had just received a text message. Receiving a Whatsapp message will trigger an alert! This is of course good and little bad at the same time. It confirms that the sensor will detect something. At the same time you don’t want false positives like that. No reason for too much concern though, because moving the phone half a meter (and maybe a lot less, I didn’t test precisely how far the phone needs to be) will prevent the sensor from showing an alert. It does tell me that placement of the detector will be important.

Now it is just a matter of waiting for an actual thunder storm to see the real effect and determine what to do with the information. As with all sensor data, I forward it to my server by queueing the update on a Raspberry Pi running on my local network first. This is done via a regular HTTP POST. The details of that are a subject for another blog.