Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:28 03 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : AS3935 lightning detector

     Page 2 of 4    
Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 05:06pm 27 Jun 2017
Copy link to clipboard 
Print this post

I have had a play with setting the tuning capacitor.
It assumes that your INT pin is also a counting pin.
Replace the code starting after the DIM statements to the end of the main loop with:
  Quote   DIM newtune, k$
AS3935_setup
SETPIN IRQ_PIN, INTH, AS3935_ISR
'
DO
' This program only handles an AS3935 lightning sensor. It does nothing until
' an interrupt is detected on the IRQ pin.
IF AS3935_ISR_Trig = 1 THEN ' lightning sensor triggered
PAUSE 5
' reset interrupt flag
AS3935_ISR_Trig = 0
' now get interrupt source
int_src = AS3935_GetInterruptSrc()
SELECT CASE int_src
CASE 0
PRINT "interrupt source result not expected"
CASE 1
lightning_dist_km = AS3935_GetLightningDistKm();
PRINT "Lightning detected! Distance to strike: ";
PRINT lightning_dist_km;
PRINT " kilometers"
CASE 2
PRINT "Disturber detected"
CASE 3
PRINT "Noise level too high"
CASE ELSE
PRINT "OOPS!! The module shouldn't return "+STR$(int_src)
END SELECT
re = AS3935_PrintAllRegs()
' for debug...
ENDIF
PAUSE 5
k$ =
INKEY$
IF UCASE$(k$) = "C" THEN setcap
LOOP

END

' check tuning
SUB setcap
SETPIN IRQ_PIN, FIN
re = AS3935_SetIRQ_Output_Source(
3)
re = AS3935_CAPACITANCE
DO
PAUSE 2000
PRINT "Frequency: ";STR$(PIN(IRQ_PIN)*16)
INPUT "New tuning capacitor (1 - 120pF)? ", newtune
IF newtune > 0 AND newtune <121 THEN
re = AS3935_SetTuningCaps(newtune)
PRINT "Capacitor set to ";STR$(re*8);"pF"
PRINT ""
ENDIF

LOOP UNTIL newtune <0
AS3935_CAPACITANCE = re
PAUSE 1000
AS3935_setup
END SUB




The oscillator frequency appears on the iNT pin so you can connect a meter or use the 'mite to read it.
The frequency is the nominal 500kHz divided by 16 = 31250 Hz, well within the range of the 'mites.

pressing 'C' while in the main loop will initiate the setcap SUB.
It displays the current frequency and asks for a new capacitor value. Valid values are from 1 - 120. The software changes your entered value to a multiple of 8 and sets the register.
It then displays the new frequency.
entering -1 exits the setcap SUB and returns to the main loop.

You should then alter the code with your shiny new capacitor value.

A session looks like:
  Quote  Frequency: 503168
New tuning capacitor? 0 - 120pF110
wrt: 8D Act: 8D
capacitance set to 8x13 = 104pF
Capacitor set to 104pF

Frequency: 501568
New tuning capacitor? 0 - 120pF120
wrt: 8F Act: 8F
capacitance set to 8x15 = 120pF
Capacitor set to 120pF

Frequency: 498368
New tuning capacitor? 0 - 120pF116
wrt: 8E Act: 8E
capacitance set to 8x14 = 112pF
Capacitor set to 112pF

Frequency: 499952
New tuning capacitor? 0 - 120pF-1
Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01
beginning boot procedure....

Edited by TassyJim 2017-06-29
VK7JH
MMedit
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 328
Posted: 03:39pm 28 Jun 2017
Copy link to clipboard 
Print this post

Jim,

The setcap routine was fun to play with and I can see maybe 88pf value would be better than the 80pf marked on my boards packaging. BUT, your routine doesn't seem to be passing the correct values back to the main program. When I select 88pf the main program sets up for 8pf.

Currently the program is not scrolling with the "Noise level too high" message however when I trigger the lightening emulator it only registers as a "disturber" no matter what level strike I trigger.

Wish I had more to toy around with the code and compare/decode register values, unfortunately it has been a little extra busy at the office.

--Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 03:58pm 28 Jun 2017
Copy link to clipboard 
Print this post

It looks like I left the *8 of the end of
  Quote   AS3935_CAPACITANCE = re*8


Once you know the new value, set in the DIM statement so it starts with the new value.
Interestingly, I get better results with a higher value of cap than the supplied one too.
supplied = 96, new value = 112

I measured the frequency with a meter as well as the 'mite.

Jim

VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:05am 29 Jun 2017
Copy link to clipboard 
Print this post

Bugger,

Still haven't had a chance to try your code yet Jim, & had a pearler of a storm last night.

Phil.

 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 328
Posted: 03:13pm 29 Jun 2017
Copy link to clipboard 
Print this post

  Quote   AS3935_CAPACITANCE = re*8

That fixed the problem with wrong value being returned. Unfortunately after doing the setcap routine the program no longer responds to disturber/lightening events, even if I used the same cap value the program started with. If I re-run the program it works again until I do the setcap.

Moved on to trying to produce a lightening interrupt. Eventually, out of frustration, I started plugging random values in for registers 0 and 1 until I finally produced a lightening interrupt. Then the program crashed due to a rogue semi-colon.

CASE 1
lightning_dist_km = AS3935_GetLightningDistKm();


Now I need to go back and work out what the values really should be using my random values as a starting reference. The weather channel is predicting thunder storms this weekend... Hopefully I can work them out by then.

--Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 03:30pm 29 Jun 2017
Copy link to clipboard 
Print this post

That rogue semicolon is what happens when you translate arduino code and don't check things well enough.
I hope that's the only one I missed.

I am going to write a setup program that lets you play with most of the settings.
Hopefully, it works better than my present cludge.

I would like a bit more information about some of the settings but the chip manufacturer wants you to purchase their development board before you get the setup application notes.

No sign of thunder storms here, just warnings about snow.

Jim
VK7JH
MMedit
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 328
Posted: 06:18pm 29 Jun 2017
Copy link to clipboard 
Print this post

  TassyJim said  I am going to write a setup program that lets you play with most of the settings.

Can't wait! Assembling the bits by hand is a real pain.

  Quote  I would like a bit more information about some of the settings but the chip manufacturer wants you to purchase their development board before you get the setup application notes.

That explains why the development board is outlandishly expensive and wny the data sheet is lacking in some detail.

  Quote  ...just warnings about snow.

You can always hope for some rare "thunder snow".

--Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 06:52pm 29 Jun 2017
Copy link to clipboard 
Print this post

  Justplayin said  
  Quote  ...just warnings about snow.

You can always hope for some rare "thunder snow".

--Curtis

A thunder storm when the hills are covered in snow is impressive to view.
But not so impressive if you work for the local electricity mob and are on call

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 04:31pm 30 Jun 2017
Copy link to clipboard 
Print this post

Hi Jim & Curtis,

I'm hoping to set something up to start giving this a try this weekend.
Either an E64 or the Original Backpack.

On the topic of using the Backpack, I presume the code should be fine if I disable the Display & Touch, but what if I decided to display the output on the LCD?

Is that a doable option or is it not possible to use SPI for anything other than the display & touch.

The E64 is a bit hard to add a display to without building a birds nest...

But the other Option I have is to stick a 2.8" screen on a SnadPic;
I do have a couple of Piggy Back boards to do that. (Thanks Palcal).

Regards the calibration, I did email their support as they suggest & they replied with a value of 80pF.

Thanks

Phil.

 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 04:59pm 30 Jun 2017
Copy link to clipboard 
Print this post

Provided you have use chip select pin on the display, you should be able to have both devices co-existing.
That is why I only turn the SPI on during read/write operations and leave it fee when not in use.
Once setup, the lightning detector doesn't initiate traffic on the SPI. It only triggers an interrupt when it has something to say. There are no critical timing events to worry about.

Jim

VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 07:23pm 30 Jun 2017
Copy link to clipboard 
Print this post

Thanks Jim,

Getting to it.

It will go on a solderless board for starters, but just curious as the whether you went with straight or 90° header pins.

90° ones might.... or might not....
stand the aerial at the top in the final device.

I see a 50% chance of deciding I made the wrong decision at a later stage.

Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 09:15pm 30 Jun 2017
Copy link to clipboard 
Print this post

Phil,
Geoff's new update to MMBasic has:
  Quote  New Features:
. Relaxed error checking on I2C addresses to allow addresses 7 and below to be used.

That means you can use I2C to talk to the sensor if it makes sharing easier for you

I don't think it will make any difference to the ability of the sensor to do it's job, but it is good to have the choice of communication modes.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 11:16pm 30 Jun 2017
Copy link to clipboard 
Print this post

Thanks Jim,

I hadn't seen 5.04.05 until you mentioned it.

Too busy carting firewood in & fitting a new fan to the wood heater.

Expecting a few cold night; the type you are probably not accustom to.

-2°C last night & heading for the same again tonight; already down to 5°.

Phil.
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 328
Posted: 01:56pm 01 Jul 2017
Copy link to clipboard 
Print this post

Played around with the register values trying for maximum sensitivity and came up with following: (your actual mileage may vary)

Register 0 - 3Eh
Register 1 - 11h
Register 2 - C0h


We had a lightening storm!!!

The good news is I had lightening events triggered, and the distances readings diminished as the storm got closer.

The bad news... As the storm moved away, the chip continued to output the distance of the closest event it recorded. The energy readings dropped, but the estimated distance remained at a constant 1km. I'm not sure how to handle this problem at the moment.

--Curtis






I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 04:04pm 01 Jul 2017
Copy link to clipboard 
Print this post

It is odd to have users of electronic equipment happy about electrical storms!

Going by your settings, I assume you have a quite location as far as radio is concerned.
I don't know why the device didn't detect the storm receding. Perhaps clearing the stats would help but according to the data sheet, it should work without any intervention.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 09:47pm 05 Jul 2017
Copy link to clipboard 
Print this post

Finally got to have a look at this, but not sure I'm getting the expected results.

[Code]wrt: 81 Act: 81
Frequency: 517424
New tuning capacitor (1 - 120pF)? 80
wrt: 8A Act: 8A
capacitance set to 8x10 = 80pF
Capacitor set to 80pF

Frequency: 501728
New tuning capacitor (1 - 120pF)? -1
Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01
beginning boot procedure....
wrt: 24 Act: 24
wrt: 20 Act: 20
wrt: 00 Act: 00
wrt: 24 Act: 24
set up for indoor operation
wrt: 01 Act: 00
disturber detection enabled
wrt: 00 Act: 00
wrt: 01 Act: 01
capacitance set to 8x1 = 8pF
AS3935 manual cal complete

17:43:07 06-07-2017
Reg &H00: 24
Reg &H01: 22
Reg &H02: C2
Reg &H03: 00
Reg &H04: 00
Reg &H05: 00
Reg &H06: 00
Reg &H07: 3F
Reg &H08: 01
AFE Gain Boost: 10010
Power-down: 0
Noise Floor Level: 010
Watchdog threshold: 0010
Clear statistics: 1
Minimum num lightning:00
Spike rejection: 0010
Freq div -ant tuning: 00
Mask Disturber: 0
Interrupt: 0000
Energy LSB: 00000000
Energy MSB: 00000000
Energy MMSB: 00000
Distance estimation: 111111
Display LCO on IRQ: 0
Display SRCO on IRQ: 0
Display TRCO on IRQ: 0
Internal Tuning Caps: 0001
Energy: 0[/code]

It doesn't seem to be responding to the emulator.
Also says the capacitor is set to 8pF after I enter 80; have I missed something in the thread?

It's currently on a 28 pin backpack, screen & touch not configured.
Using I2C (Ver 5.04.05) & Pin 16 for the Irq.

Any thoughts?

Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 11:37pm 05 Jul 2017
Copy link to clipboard 
Print this post

Phil,
There are a couple of bugs in my code.
Once you know the correct capacitance value, change the value in the DIM statement to suit and then just run the main program.

I hope that makes sense!

I will update the code with the fixes later.

Jim


VK7JH
MMedit
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6284
Posted: 11:52pm 05 Jul 2017
Copy link to clipboard 
Print this post

2017-07-06_094644_AS3935.zip
OK
This version runs the same as before with a few 'refinements'
A disturber event simply prints a 'D' rather than waste a full line.
all other events print the time and event.

Calibrating the capacitor value should work properly now.
C to enter the set routine, -1 to exit the set routine.

I intend to add adjustments for the other settings eventually.

I made a lightning simulator using the Signal Generator Geoff described a few months ago. It seems to work.

More details about that later.

Jim


VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 11:37am 06 Jul 2017
Copy link to clipboard 
Print this post

Thanks Jim,

It looks like there's a fair bit of info about on different calibration settings.
Mostly Arduino routines. Trying to see if I can understand them a bit.

Setting the Noise floor was one that jumped out.

Found missing edit I had which set 80pF as 8 so past that.

But getting a lot of disturbers & a few phantom strikes without triggering the emulator.

Guess that's where the noise floor comes in.

Final thing I did last night was run it for about 30 seconds & capture the output.

A NP++ search for "Disturber Detected" counts 73 instances.
But if I then do a search for "Energy: 0", I get a count of 62.
Some of the Disturber entries have a significant Energy Value.

Don't quite understand what it means, but it's an interesting observation.

Searching "Lightning Detected" has a count of 13,
And Searching "Energy: 0\r\nLightning detected!" a count of 7.

Don't know what that explains either.

I attached the capture file if you're interested.

Will create another one later & try & add known presses of the different buttons on the Emulator at known time stamps.

Cheers.

Phil.2017-07-06_213659_AS3935_Output.zip

 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 11:46am 06 Jul 2017
Copy link to clipboard 
Print this post

  TassyJim said  A disturber event simply prints a 'D' rather than waste a full line.
all other events print the time and event.


I might revert it to the full line for now;
I can easily search that in a capture file.

Another thing that might help is a Variable to count the disturbers & report them.

Not sure how you'd define my front office in regards to RF noise.

There's 3 Fluro's; Elecronic ballast in the 4' & iron in the two 2' ones.

There's a Wifi AP a few metres away & another about 5m away in the next room.

There's also an ESP-01 on the breadboard I'm using, about 3" away from the Lightning module. Not sure if that's a bad thing or irrelevant.

Phil.
 
     Page 2 of 4    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025