Skip to main content
shopping_basket Basket 0
Login

Building a Pi Powered Lightning Detector

Andrew Back
2

Using a Raspberry Pi and a Thunder click module to detect lightning

Using a Raspberry Pi and a Thunder click module to detect lightning.

In this post, we take a look at creating a lightning detector using a Mikroe Thunder click module and a Raspberry Pi, with events published to an MQTT broker, which may optionally be integrated with Home Assistant and/or a data platform that provides logging and more sophisticated visualisation.

AS3935

Thunder click module uses an AS3935 chip

The Thunder click module is based on the AS3935 from austriamicrosystems, which detects lightning strikes by the radio frequency emissions that they generate. The IC includes algorithms to distinguish these signals from man-made disturbers — which it is then able to mask — and to calculate strike energy and the distance to the storm front.

I first wrote about the AS3935 coming up for 9 years ago, in a short post that looked at using a breakout board for this together with an Arduino and prototyping shield. The AS3935 was a fairly new device at the time, with limited experience and it being difficult to test due to the fact that, well, you needed to wait for the next thunderstorm.

Upon recently discovering that there now appeared to be various projects using the AS3935 and that it was possible to construct a low cost “lightning emulator”, it was decided to revisit the rather interesting sensor and construct something a bit more practical — and that could be tested.

Hardware

The hardware needed to build a Pi Powered Lightning Detector

The following hardware was used:

An older Raspberry Pi, such as a Model 3 B+, will suffice and is what we used as we had one close to hand, but obviously, if you’re buying new it rarely makes sense to get anything other than a Pi 4 now. Since this will prove a lot more useful if it came to be recycled in future projects and in addition to this, there is greater headroom for running multiple applications alongside each other.

Although the Click Shield is sold as being for a Pi 3, it is also compatible with a Raspberry Pi 4 and this was presumably to distinguish it from a previous version made with the shorter GPIO header.

Note that the Thunder click module should be inserted into mikro BUS slot #1.

Mosquitto

An MQTT broker is required and for testing purposes, we installed Mosquitto and its command-line clients on the same Raspberry Pi, with:

sudo apt install mosquitto mosquitto-clients

However, if you already have a broker running on your network, you may simply use this.

Another option would be to run Mosquitto along with NodeRED, InfluxDB and Grafana, to provide a complete self-hosted data platform, that could be used to maintain a historical record and perhaps plot strikes along with other environmental data. For details of how to set this up, see Raspberry Pi 4 Personal Datacentre Part 3: A Self-hosted IoT Data Platform.

Detector daemon

Screen showing installation of lightning-detector-MQTT2HA-daemon

If you search around there are various libraries and projects for the AS3935, but some of these only support interfacing with I2C, whereas the click module uses SPI. One project that stood out, in particular, was lightning-detector-MQTT2HA-daemon from Stephen Moraco, a.k.a. IronSheep. As the name suggests, this publishes events via MQTT and is intended for use with the Home Assistant (HA) software for home and building automation. With one of the nice features being that it advertises itself via MQTT for HA autodiscovery.

The default instructions assume I2C interfacing and so we’ll be following those for SPI.

The following steps assume that we have a fresh Raspberry Pi OS install — the Lite version is sufficient and generally best for such applications — and have carried out any basic configuration, such as setting the hostname and enabling the SSH server.

First, we need to enable SPI interfacing with:

sudo raspi-config

Following which install some O/S dependencies, clone the GitHub repo and then install the Python dependencies.

sudo apt install git python3 python3-pip python3-pigpio pigpio

sudo git clone https://github.com/ironsheep/lightning-detector-MQTT2HA-Daemon /opt/ISP-lightning-mqtt-daemon

cd /opt/ISP-lightning-mqtt-daemon

sudo pip3 install -r requirements.txt

The default config file can then be copied and edited:

sudo cp /opt/ISP-lightning-mqtt-daemon/config.{ini.dist,ini}

sudo nano /opt/ISP-lightning-mqtt-daemon/config.ini

Details for the MQTT broker should be set in the [MQTT] section. If running Mosquitto on the same Raspberry Pi, most of these can be left as the default. However, at the time of writing it appears that it is necessary to set the hostname and so this line should be uncommented, otherwise the daemon will fail to start.

The following parameters should be set in the [Sensor] section:

  • sensor_attached = SPI
  • intr_pin = 6

Note that it should be possible to have the Thunder click module fitted in the 2nd slot on the HAT and use different values here, but this has not been tested.

After saving changes we can now start the PiGPIOd service and do a first run.

sudo systemctl start pigpiod.service

python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py

Screen showing the first run being successful

And if all goes according to plan we should see output similar to that shown above. However, if the daemon fails to communicate with the AS3935, ensure that SPI is enabled, the click module is fitted in slot 1 and that the sensor configuration parameters are all correct.

Fine tuning

Image showing the 500kHz antenna on the Thunder click module

Before moving proceeding to further testing and configuring the daemon to start automatically, there is first some fine-tuning that we need to do. If we run:

python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap

This will take a few minutes to complete and output a tuning capacitor value for the 500kHz antenna on the Thunder click module.

Screen showing the fine-tuning output for tuning capacitor value for the 500kHz antenna on the Thunder click module

We need to then take this value and plug it into the tuning_capacitor parameter in the config file. What this does is tune the antenna to make it resonant as close as possible to the desired frequency.

There are a few other interesting parameters in the [Sensor] section. The first of these, detector_afr_gain_indoor, can be set to False if we have the detector mounted somewhere outdoors. This reduces the gain of the RF front-end accordingly, as the signal will be less attenuated. There is also a detector_noise_floor parameter that can be tweaked and, last but not least, detector_min_strikes, which we set to 1, so that we register the very first strike. These are parameters that we’ll likely want to fine-tune over time, based on the observed performance.

Testing

Pi Powered Lightning Detector being tested

Playing With Fusion has created a simple shield that when paired with an Arduino can be used to emulate lightning, generating a signal that should register with the AS3935 as a genuine strike. In practice, we’ve found this to be somewhat fiddly and quite critical in terms of the distance between the emulator antenna and detector antenna. We’ll put this down to the AS3935 being pretty good at distinguishing man-made disturbers, but it may also be that emulator code could be tweaked to improve performance, since it appears to simply feed values that could easily be edited into a DAC.

Above we can see the console output when a strike has successfully been registered.

Note that we can also subscribe to the MQTT topic where strike data will be published with:

mosquitto_sub -h <HOSTNAME> -t home/nodes/sensor/lightningdetector/detect

Screen showing the energy and distance calculated for three emulated strikes

Here we can see the energy and distance calculated for three emulated strikes.

Automatic startup

The daemon user will need to access the SPI bus and GPIO, hence can be added to the relevant groups with:

sudo usermod daemon -a -G spi,gpio

The following commands can then be used to configure automatic start-up:

sudo ln -s /opt/ISP-lightning-mqtt-daemon/isp-lightning.service /etc/systemd/system/isp-lightning.service

sudo systemctl daemon-reload

sudo systemctl enable pigpiod.service

sudo systemctl enable isp-lightning.service

sudo systemctl start pigpiod.service

sudo systemctl start isp-lightning.service

To check the status of the daemon service and to watch its output:

sudo systemctl status isp-lightning.service

sudo journalctl -f -u isp-lightning.service

Press CTRL-C to stop the 2nd command.

Home Assistant

Handily, Stephen has also created a neat Lightning Detector “card” that can be installed in Home Assistant’s web dashboard, Lovelace. This can be installed manually, or via the Home Assistant Community Store (HACS). If you don’t already have a Home Assistant running on your network, see the post, Raspberry Pi 4 Personal Datacentre Part 2: Home Assistant. Although note that the last two lines from the Ansible playbook could be omitted if you do not have an enOcean wireless receiver plugged into the USB port of the Raspberry Pi.

Concluding notes

It should be entirely possible to run this application on the same Raspberry Pi as Home Assistant and, if so desired, also the “self-hosted IoT data platform” detailed in the aforementioned post.

Generally speaking, when running multiple application stacks on the same Pi it would likely be advisable to use a Raspberry Pi 4 — and of course the more RAM it has, the more apps it should be possible to run side-by-side. With the caveat that you need to take care to ensure that there is not a clash where two applications want to use the same IP port number. Where this is the case, it is more often than not easily remedied by modifying the application configuration or, if it’s running inside a container, the configuration for this which maps an internal port number to an external one.

Finally, I’d just like to say thank you to Steven Moraco for sharing these projects.

Andrew Back

Open source (hardware and software!) advocate, Treasurer and Director of the Free and Open Source Silicon Foundation, organiser of Wuthering Bytes technology festival and founder of the Open Source Hardware User Group.

Related Content

Comments

September 9, 2021 15:31

Hi
Have set up a lightning as per this document and running the program manually
python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py
and it works OK
When I set it up for Automatic Start Up if fails with
getHostSpecifics()
s.connect((gw[2], 0))
Index is out of range
In the config.ini file its set up for localhost with everthing else as default
Im using a Ri ver3
Just wondered what Ive missed
Again after trying this from boot up I try it manually it works OK
Any help appreciated
Thanks Phil

0 Votes

August 5, 2021 10:19

Ohhh I read the title as a Pi powered Lie detector, can we have that in the future???

0 Votes
DesignSpark Electrical Logolinkedin