AS3935 Lightening Detector

755 views
Skip to first unread message

dan Forster

unread,
Jul 8, 2019, 4:48:22 AM7/8/19
to weewx-user

Hi,

I bought one of these from eBay  - MA5532 AS3935 2.4V to 5.5V Lightning Fulmine Sensor Breakout SPI/I2C Antenna. I have connected it all up and have it working fine, confirmed by using the demo script and install instructions supplied here https://github.com/pcfens/RaspberryPi-AS3935. I can see that a few people had problems connecting the module, so I have attached a picture of how I have wired to get it working perfectly (note one address line has to be set to common ground and the address is 00x0 NOT 00x3).

I then followed these instructions https://github.com/weewx/weewx/wiki/as3935 in order that it can be used within my weewx weather station. I have managed to do everything as it says on this page, including the part at the bottom of the page with regards to the extra sqlite database holding the lightening detector data (which I confirmed exists in the same place as the weewx db).

 

Now I am using the Seasons skin and would like to get help on adding the two new lightening detector values (lightning_strikes and avg_distance)into the Seasons skin web page (at the bottom of the Current Conditions list). I think the file I need to alter is /etc/weewx/skins/Seasons/current.inc.

 

I am OK with electronics and getting better at Linux, but I am terrible at HTML. Any pointers would be greatly appreciated...

 

Dan

as3935RPi.jpg

Mikael Fredriksson

unread,
Jul 8, 2019, 2:53:04 PM7/8/19
to weewx-user
Hi!

I have another issue with this setup, but that´s in another question.

I saw your connections and they differ from mine quite much so I wonder which one is correct?!

I´ve been told to connect like this:

VCC -> 3,3V
GND -> GND
SCL -> PIN 5
MOSI -> PIN 3
SI -> 3,3V
A0 -> 3,3V
A1 -> 3,3V

And I got respones when running demo.py
And also in syslog weewx seeing lightning or maybe its something that looks like lightning. But it seems to react to something anyway.
You can read about my database issue in my own question, and if you have some answer to that it would be great!
I´m not very good at linux so there is much trail&error :)
I hope someone can help you with your question!

dan Forster

unread,
Jul 9, 2019, 4:44:32 AM7/9/19
to weewx-user

Mikael,

 

The two pads A0 and A1 are for setting the base address and are not allowed to float (not documented very well so this stumped me for ages). Your setup is exactly (all be it with a different base address) the same as mine by the looks of things.

 

When you stop and then restart start weewx using: -

 

sudo /etc/init.d/weewx stop

sudo /etc/init.d/weewx start

 

and then issue this command quickly: -

 

tail -f /var/log/syslog

 

you should see the AS3935 load and adjust itself for noise. Bear in mind where you are using this board, it is sensitive to noise around 500khz, which can occur many places inside a house (bad and cheap LED lamps are very noisy). If you can use outside in a weatherproof housing you would experience less interference (Google about long cables from the board to your PC, this needs to be done correctly too).

 

Take care

 

Dan.

gjr80

unread,
Jul 9, 2019, 5:31:31 PM7/9/19
to weewx-user
> and then issue this command quickly: -
>
> tail -f /var/log/syslog

You can also open a second (or third or fourth etc) terminal window and run tail -f in that terminal; much easier to monitor what is going on and you don’t miss the first so many lines.

Gary

dan Forster

unread,
Jul 10, 2019, 3:39:07 AM7/10/19
to weewx-user
Cheers Gary, I didnt think to do this... Dan

Mikael Fredriksson

unread,
Jul 10, 2019, 6:15:18 PM7/10/19
to weewx-user
Dan,

thanks for your answer!

I now have the AS3935 running and sending data to my database. Working on other issues, wrote about them in my post "AS3935 database issue"
Planning on moving it outside because of the interference with things inside the house,

Mikael

Ian Rich

unread,
Aug 10, 2019, 4:40:24 PM8/10/19
to weewx-user
Hi Dan, you are a lifesaver! - I bought this exact model and have spent days trying to dig out information about how wire it for IC2 correctly.

In fact I had thrown it in the back of the drawer and bought a different type with which I have had more success. - I just wired exactly as you detailed and it works like a charm :)
I now have a backup unit for my dev/test weewx box.

Many thanks for providing this information! 

Kind regards,

Ian

Steve2Q

unread,
Aug 24, 2019, 1:02:37 PM8/24/19
to weewx-user
Message has been deleted
Message has been deleted

Mikael Fredriksson

unread,
Sep 1, 2019, 7:24:17 AM9/1/19
to weewx-user
Hi!

Got a new issue with my as3935.
Running weewx 3.9.1


Got this in my log when starting weewx:

Sep  1 11:42:05 raspberrypi weewx[570]: engine: Debug is 1
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Initializing engine
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Loading station type FineOffsetUSB (weewx.drivers.fousb)
Sep  1 11:42:05 raspberrypi weewx[570]: fousb: driver version is 1.9
Sep  1 11:42:05 raspberrypi weewx[570]: fousb: polling mode is PERIODIC
Sep  1 11:42:05 raspberrypi weewx[570]: fousb: polling interval is 60
Sep  1 11:42:05 raspberrypi weewx[570]: fousb: found station on USB bus= device=
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Loading service weewx.engine.StdTimeSynch
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Finished loading service weewx.engine.StdTimeSynch
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Loading service user.as3935.AS3935
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: service version is 0.6
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: address=0x03
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: bus=1
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: indoors=False
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: noise_floor=0
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: calibration=0x06
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: pin=17
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: data_binding=None
Sep  1 11:42:05 raspberrypi weewx[570]: as3935: binding=archive
Sep  1 11:42:05 raspberrypi weewx[570]: engine: Caught unrecoverable exception in engine:
Sep  1 11:42:05 raspberrypi weewx[570]:     ****  [Errno 121] I/O-fel på fjärrmaskin
Sep  1 11:42:05 raspberrypi weewx[570]:     ****  Traceback (most recent call last):
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/share/weewx/weewx/engine.py", line 884, in main
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      engine = engine_class(config_dict)
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/share/weewx/weewx/engine.py", line 78, in __init__
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      self.loadServices(config_dict)
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/share/weewx/weewx/engine.py", line 142, in loadServices
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      self.service_obj.append(weeutil.weeutil._get_object(svc)(self, config_dict))
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/share/weewx/user/as3935.py", line 154, in __init__
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      self.sensor.set_indoors(indoors)
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 162, in set_indoors
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      self.read_data()
Sep  1 11:42:05 raspberrypi weewx[570]:     ****    File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 234, in read_data
Sep  1 11:42:05 raspberrypi weewx[570]:     ****      self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
Sep  1 11:42:05 raspberrypi weewx[570]:     ****  IOError: [Errno 121] I/O-fel på fjärrmaskin
Sep  1 11:42:05 raspberrypi weewx[570]:     ****  Exiting.
Sep  1 11:44:34 raspberrypi systemd[1]: Stopping LSB: weewx weather system...

Haven´t changed anything and suddenly I got an I/O error on the machine.

If I run "sudo i2cdetect -y 1" several times in a row I sometimes get 0x03 but every other time I just get --.


Any clue what this could be or how to debug?

Got it connected to my raspberry pi like this:

      VCC -> 3.3V

        GND -> GND
        SCL -> PIN 5
        MOSI -> PIN 3
        SI -> 3.3V
        IRQ -> PIN 11
        A0 -> 3.3V
        A1 -> 3.3V


/Mikael


Den måndag 8 juli 2019 kl. 10:48:22 UTC+2 skrev dan Forster:
Message has been deleted

Mikael Fredriksson

unread,
Sep 5, 2019, 9:02:54 AM9/5/19
to weewx-user
Hi Jerry!

Thank you for your answer.
I've tried changing all wires and looked for any bad connections. But no success.
I guess it's a faulty sensor so I will buy another one and see if i'm lucky with that.
The sensor I used is a CJMCU AS3935.
Is there another recommended sensor to use with Weewx? Or are they almost all the same?

/Mikael

Den söndag 1 september 2019 kl. 17:21:07 UTC+2 skrev J D:
Hi,
I have seen this a number of times with these sensors, what I have done is the following to make it less of an issue
Checking for loose wires or wires not making good contact at either the GPIO pins or the device..
Power cycle the system.
Replacing the device.
Making sure there are no other devices with the same bus and address being used
Putting a while loop with a fixed loop count and a try except around the i2cbus read and sleeping for a second or two before trying the read again.. (This is the best solution and if it is still failing after 10-20 seconds of trying then there is probably a hardware failure most likely the device or a loose connection.
Thanks Jerry
Message has been deleted

dan Forster

unread,
Sep 14, 2019, 11:21:02 AM9/14/19
to weewx-user




Sorry I have not been about and replying to board messages. Started some DIY project which turned out larger than expected...

This is how I got my AS3935 to work in Weewx. It may not bee 100% correct, but it is now working on two pi's really well. Note if you think you are going to run this on 6" pieces of cable, so its close to the pi, indoors, next to all your gizzmos, the lightning will bite your bum for this thing sees it. Its best run in a low noise environment (away from power supplies, cheap LED lights, computer screens, etc). From what I can gather, its a 500khz receiver and some clever logic, so like any radio, it likes to be able to see the sky. I will start from scratch so you can get going from a new pi sdcard/AS3935 board

This is the detection board I use - https://www.ebay.co.uk/itm/AS3935-Lightning-Strike-Storm-Distances-Detector-I2C-SPI-Sensor-Programmable/333265065400?_trkparms=ispr%3D1&hash=item4d982555b8:g:VWUAAOSw1NNdKCt2&enc=AQAEAAAB4BPxNw%2BVj6nta7CKEs3N0qX6knsPaWu2HyFk9u1VHKBpz81ghsTUEAWuMAkGOyv6nDm%2FfHF6ZNRVWUmyw6fMizGZ%2BYp4db2m9KpEC601RxylniwLIMm1Yna7V1VtB3WNTdeLsrOFmUXb8ykzBxZK2B3hLzfgKkhHP80k%2BXuEpKA7AgDyaSN%2FJluRWLSOUQ88THmlkf3DW0W6wL2mUZW5Sp9gkMFWxvt2OpppfyNs2YHI%2B2C%2Ftbi5YMd%2BCv5q6K300H7lsatLWwQEkE6kv1YXLLis4zt4NUw58qFrTss5O%2B%2FnahobycDwpibwG%2B%2Bj8LZ5Hn3HJgTc8sQFNyhu%2Bm6M2RUolopnAvcJu%2BWCVgM%2Fz3fr0O7l1u%2FZ1cd64SuOKVm%2FvCNIDddFrgZNW0gbMjAVxwlS11mmQuiIowAqFlf6d16f7t1yrCXjHYPZyTEqdNhTNpycb1IGseu6FoaXpJquvoB4nZrnPvn0qe7P1xPdyHVfNXh224r8jd4NDdC0jCDVDJlJD8ilWZKXe6%2Fb5sNQIoJqw5tWlPbv5gbq%2FC%2BdpTM%2BJMKdeeAPfLpHN%2FpirKMAKcbKuW6wydhGa7aa%2BRk6SnCsySeIvixE2%2Fet4FPn%2BRvEQVFVNg2M1L4tkPkpFW6bdA%3D%3D&checksum=333265065400e9f0e246d5364032ab0e55b879590ef8 - they seem fairly tough little things to be honest and can seemingly withstand lots of abuse - (I must have changed every wire ((not power pins of course)) in my experimentation's).


Using RPi 3 board - using this image https://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/. Its not the latest - I prefer something tried and tested and sort of bug free. NOTE - once the image is burnt to sdcard (https://www.balena.io/etcher/ makes prepping sdcards a cup of tea - DONT buy cheap sdcards, they will make the system crawl like a snail), eject card, then reinsert to the same MAC/WIN PC you just prepped card on, then add a little file called ssh (make with a text editor) with no extension (so thats no full stop after ssh file name - just a file called ssh) to the bootable partition (the partition you see when you insert this rpi OS ready sdcard into a MAC/WIN machine). This enables ssh access as default. I dont use a desktop on the pi, just ssh.

Put new card in pi - boot - log in via ssh

run sudo rpi- config = enable IC2 and expand filesystem, change your locale and give the pi a hostname that makes sense (then it can be any IP it wants to be, you can easily look up on your LAN using hostname - easier to remember than IP addresses. Reboot pi.

Update the pi when its rebooted (sudo apt-get update - and sudo apt-get upgrade). I also change the default pi password, just to be safe (note to use this new password when you reboot pi!).

Reboot pi - ssh in (I run everything from my home pi directory unless otherwise stated)

Install apache2 -  sudo apt-get install apache2 -y  (I like this to be on the pi so weewx sees it on install).

Install weewx as follows (or any other way that you like, this adds the repository to the pi so updating and installation is real easy).

wget -qO - http://weewx.com/keys.html | sudo apt-key add -
wget -qO - http://weewx.com/apt/weewx.list | sudo te e /etc/apt/sources.list.d/weewx.list
sudo apt-get update
sudo apt-get install weewx

Now when you do a sudo apt-get update/upgrade, weewx gets updated too.

Power down you pi

Wire up your as3935 module (earth yourself first for a few seconds, the little pcb is static sensitive).

Board VDD = 3.0VDC - PIN # 1 - RED
Board GND = Ground - PIN # 6 - BLACK
Board SCK/SCL = SCL - PIN # 5 - GPIO 3 - WHITE
Board MOSI/SDA = SDA - PIN # 3 - GPIO 2 - GREEN
Board IRQ = Pin #11 - GPIO 17 - YELLOW
Board SI = 3.0VDC - Pin #17 for i2c - RED
Board A0 and A1 = 3.0VDC - Pin #17

I use a soldering iron to make the last 3 connections above to pin #17 (modify some push on connections with the soldering iron rather than solder anything directly to the pi) - ditto push on connections for the AS3935 board.

The reason to put 3V on A0 and A1 is to give it an address that can read by by ic2-tools.

Power up your pi and ssh in.

Type - sudo apt-get install i2c-tools

Type i2cdetect -y 1

You should now see the module show up as address 03 (if you don't use the address pins of the module A0 and A1 - you leave then unconnected for instance - the module will have an address that wont show on ic2detect).

I then install the python library using - sudo pip install RPi_AS3935 this makes the module directly accessible by python.

I then install smbus - sudo apt-get install python-smbus

I then run the Weewx installer for the AS3935 using this link https://github.com/weewx/weewx/wiki/as3935

I then edit weewx.conf like this - sudo nano /etc/weewx/weewx.conf

Go to the end of the file, I then alter the AS3935 section at the end like this

[AS3935]
        bus = 1
        indoors = false # set to True if indoors
        noise_floor = 0
        calibration = 6
        pin = 17
        address = 3
        binding = archive
       
Save this file

Reboot pi (some say just restart weewx service but when I fiddle lots I like to reboot)

Once pi has rebooted, as quick as you can type sudo tail -f /var/log/syslog

The pi will now display services etc. as they start, including weewx. Look carefully for the 'noise level too hi' line

If its running all OK, move to the next part...

Now its time to alter the weewx schema so the two new variables are recorded in the weewx database (two new variables being lightning_strikes and avg_distance)

For this I followed this page https://github.com/poblabs/weewx-belchertown/wiki/Adding-a-new-observation-type-to-the-WeeWX-database

Read it through a couple of times until it makes sense. Dont forget, you are substituting [('appTemp', 'REAL'), ('windrun', 'REAL')] with  [('lightning_strikes', 'REAL'), ('avg_distance', 'REAL')]

Now you can alter one of the default skins (I use Seasons and played with this so it shows both the new values in text and graph format)


NOTES - My first port of call for all help with weewx is the forum. If you get stuck, post a message. I have used many web sites to help me make the list above so many thanks go to all the clever folk I leanrt from.


Message has been deleted

dan Forster

unread,
Sep 19, 2019, 7:50:22 AM9/19/19
to weewx-user
Jerry,

Sorry for not coming back sooner, I have been traveling with work.

Many thanks for your comments. You are far ahead of me with regards to working these little lightning detector boards out and getting the best from them.

A quick question, what code do you use/alter to cause a re-tune and disable the noise threshold (like squelch I suppose in amateur radio)? I noticed the board does get warm and slightly deaf due to the 500Khz tuning cap being affected by this heat.

Cheers,

Dan



On Saturday, 14 September 2019 14:16:14 UTC-4, J D wrote:
Hi Dan,
Nice write up of your work.

Here are a couple items to help with the correctness of the device.
If you are using the very common python code found on the net there are a number of issues with it.
The big one is the disturber mask part.  In the specs when a disturber is detected the device turns off for a 1.5 seconds. It also sends the interrupt. If you are using the python code the interrupt handler turns on the mask interrupt bit in the device. So now no more interrupts but the device still turns off and this is bad. Also to make things worse very far away lightning is sometimes detected as a disturber. So IMHO I would disable the setting of the mask bit and if you are having a lot of issues with disturbers either move the device or change the Watchdog setting, or the AFE setting or the Spike setting or the Noise setting to reduce the number of disturbers. This is not a perfect solution since changing the settings reduces the device sensitivity but at least it does not turn it off for a 1.5 seconds.
The next problem is the Noise threshold part of the python code. The problem here is noise can come and go but the code only raises the Noise Threshold there is no code for lowering it. Again IMHO I found that it is best to disable this code and if the noise is truly an issue manually raise the Noise threshold or put in timer code that lowers it to see if the noise is still high.
Another code issue is that there is no auto calibrate of the tune cap. There does exist a "c" version for auto tune and I have written my own python version of auto tune. Auto tune IMHO is only needed if you have the device outside where there are large temp swings. I have found that as the devices get hot the tune cap value needs to go higher to keep it as close as possible to 500khz. The specs state that the other oscillators are temp compensated but nothing about the 500khz oscillator.  Usually the change is just one or two steps up or down. So I run the auto-tune 3 times a day (morning, noon,night) and if the device is detecting lightning then I skip the auto-tune for that time.
Thanks Jerry
Reply all
Reply to author
Forward
Message has been deleted
0 new messages