Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

AS3935 Franklin Lightning Sensor IC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sat Dec 03, 2016 11:45 pm    Post subject: AS3935 Franklin Lightning Sensor IC Reply with quote

Hello.
This sensor can detect "storm" from 40km
It detect lighting but can reject incorrect signals.

Sensor have antenna that must be tuned for 500kHz.
For that purpose device have internall capacitors that can be attached via software settings.
Device can be set to mode in which one pin give output of antena resonation/16 (this divider can be also set but this is default)
So we need some time base (1s from RTC or Timer) and once choose best tune value.
After best value is found there is no need to search it every boot.
Enough is to read it from eeprom and write to device reg.

But at every reboot, and after write correct tune value, RCO calibration is needed (takes 2ms)

If you find bug please share Very Happy

Code:
$regfile = "m644pdef.dat"
$crystal = 11059200
$hwstack = 64
$swstack = 32
$framesize = 128

Config Submode = New

Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.4 , Db6 = Porta.5 , Db7 = Porta.6 , E = Porta.2 , Rs = Porta.0 , Wr = Porta.1       'Kujawa
Cursor Off , Noblink
Config Lcd = 20x4
Cls

'***********************************************************
'*                     I2C CONFIG                          *
'***********************************************************
$lib "i2c_twi.lbx"

Config Scl = Portc.0
Config Sda = Portc.1

I2cinit

Config Twi = 100000

'***********************************************************
'*                 TIMER FOR TIMEBASE                      *
'***********************************************************

Config Timer0 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Enable Compare0a : On Compare0a Timer0_isr : Compare0a = 107       '10ms @11MHz/1024

Dim 10ms As Byte , 1s As Byte , Overflows As Byte , N As Byte , Helpb As Byte
'***********************************************************
'*                     ALS CONFIG                          *
'***********************************************************
 Const Als_addr = &H06 : Const Direct = &H96
 Dim Read_als As Byte , Antena As Byte , Antena_ee As Eram Byte

 Int_pin Alias Pinb.1
 Buzz Alias Portc.7 : Config Buzz = Output
'***********************************************************
'*                     FUNCTIONS                           *
'***********************************************************

'Function Tune() is only needed once after programming.
' AS3935 have internal capacitors for antena tune to 500kHz.
' Output INT pin can be temporary programmed to be output of antena frequency/16 (by default)
' So 500000/16=31250 and by setting one of 16 capacitors variation we search best tune
' Last step is memorize best combination for skip 16s test in next boot

Function Tune() As Byte

 Config Timer1 = Counter , Edge = Falling

 Locate 1 , 1 : Lcd "Wait..."

 Local Reg As Byte : Reg = &B10000000
  Local Helpi As Integer , Diff As Word , Mem As Word
   Local Tim As Word , Best As Byte

  Waitms 10

  For N = 0 To 15
   Locate 1 , 15 : Lcd N
   I2cstart                                                 'send start
    I2cwbyte Als_addr                                       'send address
    If Err = 0 Then
       I2cwbyte &H08
       I2cwbyte Reg
      I2cstop
        1s = 0
         Do : Loop Until 1s = 1
          Timer1 = 0 : 1s = 0
         Do : Loop Until 1s = 1
          Tim = Timer1
          Helpi = Tim - 31250                               'search for minimal diff
          Diff = Abs(helpi)
         Locate 4 , 1 : Lcd Diff ; "  "
        If N > 0 Then
         If Diff < Mem Then
          Best = N : Mem = Diff
         End If
        Else
          Best = N : Mem = Diff
        End If

       Incr Reg
    Else
      Locate 3 , 1 : Lcd "I2C ERROR"
       Exit For
    End If
  Next

  Locate 2 , 1 : Lcd "N=" ; Best ; " Diff=" ; Mem

  Tune = Best
End Function

Sub Write_reg(byval Adrs As Byte , Value As Byte)

  I2cstart
   I2cwbyte Als_addr
   I2cwbyte Adrs
   I2cwbyte Value
  I2cstop

End Sub

Sub Tune_cap()
'This sub write learned value of needed capacitors or run Tune() function for first time
 Antena = Antena_ee
 If Antena_ee = &HFF Then
  Antena = Tune()
   Antena_ee = Antena
  Cls
 End If
  Call Write_reg(&H08 , Antena)
   Waitms 2
End Sub

Sub Tune_rco()
'This is procedure that must be performed once at reboot
  Helpb = Direct  'constant &H96
  Call Write_reg( &H3d, Helpb) 'write &H96 into Reg &H3D
   Antena.5 = 1
  Call Write_reg(&H08 , Antena)
   Waitms 2
   Antena.5 = 0
  Call Write_reg(&H08 , Antena)
End Sub

Function Read_reg(byval Adrs As Byte)as Byte

 I2cstart
   I2cwbyte Als_addr
   I2cwbyte Adrs
 I2crepstart
   I2cwbyte Als_addr +1
   I2crbyte Read_reg , Nack
 I2cstop

End Function

Sub Set_outside()
'Use this if sensor is outside because default sense is boosted for indoor use
 Helpb = Read_reg(&H00) And &B00000001
  Helpb = Helpb Or &B00011100
   Write_reg(&H00 , Helpb)
End Sub

Sub Set_powerdown()
'Not tested but this should set this mode Wink
 Helpb = Read_reg(&H00) Or &B00000001
  Write_reg(&H00 , Helpb)
End Sub

Enable Interrupts

 Call Tune_cap()
 Call Tune_rco()

 Waitms 500 : Lcd "Waiting..."

 Pcmsk1 = &B00000010 : Portb.1 = 1
  Enable Pcint1 : On Pcint1 Als_isr


Do

 If Read_als = 1 Then
  Read_als = 0
  Set Buzz : Waitms 2 : Reset Buzz                          'quick beep and needed(datasheet) wait time for read

   Locate 2 , 1 : Lcd "Got something!"

   Helpb = Read_reg(&H03) And &B00001111

   Locate 2 , 1
    Select Case Helpb
     Case &B00000001 : Lcd "Noise to high"
     Case &B00000100 : Lcd "Disturber det"
     Case &B00001000 : Lcd "Lightening ! "
      Helpb = Read_reg(&H07) And &B00111111

      Locate 3 , 1

       Select Case Helpb
        Case &H01 : Lcd "Storm is here"
        Case &H05 To &H28 : Lcd Helpb ; "km  "
        Case &H3F : Lcd "Out of range"
       End Select


     Case Else
       Locate 3 , 1 : Lcd Bin(helpb)
    End Select



 End If

Loop
End


Timer0_isr:

  Incr 10ms
   If 10ms = 100 Then                                       '100x10ms=1s
    10ms = 0
     1s = 1
   End If

Return

Als_isr:

  If Int_pin = 1 Then Read_als = 1
'if sensor report event you must wait 2ms and then read regs
Return


EDIT : Address for read fixed


Last edited by EDC on Mon Jan 02, 2017 12:26 am; edited 6 times in total
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Sun Dec 04, 2016 1:30 am    Post subject: Reply with quote

Hi EDC
I also like this sensor.
What kind of system are you assembling?

I made it possible to operate about half a year on dry batteries.




The distance of lightning is also displayed on the LCD with an easy graph.


I can output the recorded lightning movement to the serial terminal.


I'm sorry it's written in Japanese.
http://www.ne.jp/asahi/shared/o-family/ElecRoom/AVRMCOM/AS3935/AS3935_test.html#AS3935Clk
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sun Dec 04, 2016 2:13 am    Post subject: Reply with quote

Haha Very Happy I know your code O-Family and thats why I mentioned about RTC for 1s signal Razz
For those who wants your code is not complicated but this is code for complete clock with alarms etc.
So essential way to understand how it works is blurred Very Happy
Thats why I read datasheet and want to clarify how to handle it- it is simple I think Very Happy
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Sun Dec 04, 2016 3:32 am    Post subject: Reply with quote

Thank you for knowing my site!

My program is completed as a clock, but each process is divided into blocks.
If can understand Japanese comments, I think that it can be used as a module.
I am not good at English, so I'm sorry I have not written comments in English.

I think that your program is also good, but I was interested in your whole system!
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Mon Dec 05, 2016 4:11 pm    Post subject: Reply with quote

amazing chip. had no idea something like that was possible and/or existed.
Thanks for sharing the code and pdf.

_________________
Mark
Back to top
View user's profile Visit poster's website
aphawk

Bascom Member



Joined: 23 Jan 2010
Posts: 168
Location: Brazil

brazil.gif
PostPosted: Sun Jan 01, 2017 11:08 pm    Post subject: Reply with quote

Hi EDC,

I have some problems with your program and my sensor.

First, in the sub tune_rco() :

This don't worked ( don't set the frequency to the correct ) :

Code:
  Antena.5 = 1
  Call Write_reg(&H08 , Antena)
   Waitms 2
   Antena.5 = 0
 



This worked :

Code:
  Antena.7 = 1
  Call Write_reg(&H08 , Antena)
   Waitms 2
   Antena.7 = 0
 



Second, in the function Read_reg() :

The code you used don't works to me. I always receive &b00000101 in all register I tried read.
I changed to this code and now is reading ok :

Code:
Local Buff As Byte
   Buff = Adrs
   I2creceive Als_addr , Buff , 1 , 1
   Read_reg = Buff
 


My sensor is form Embedded Adventures, version 8.

Paulo
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sun Jan 01, 2017 11:43 pm    Post subject: Reply with quote

I isolate this routines from another project and some of them I write for this demo again.
Read address in I2C is bigger then address for writing Very Happy
I dont know how this happend but in code popup "Als_addr -1" Embarassed
So with this everything should work fine. Sorry for that Very Happy
I also try to fix this in first post.
Code:
Function Read_reg(byval Adrs As Byte)as Byte

 I2cstart
   I2cwbyte Als_addr
   I2cwbyte Adrs
 I2cstart
   I2cwbyte Als_addr + 1
   I2crbyte Read_reg , Nack
 I2cstop

End Function


But second part is debatable Very Happy Look at the picture.
Back to top
View user's profile Visit poster's website
aphawk

Bascom Member



Joined: 23 Jan 2010
Posts: 168
Location: Brazil

brazil.gif
PostPosted: Mon Jan 02, 2017 3:35 am    Post subject: Reply with quote

EDC,

What a hell ..... I downloaded the last version from the datasheet in the AMS Website, Version 1.04... see this :



Maybe the AMS people likes to see many of us in despair kkkkk ! Wait for the next revision, they will put bit 7 here Laughing !

Anyway, this information is in the case of the CI be set in powerdown mode, that is not the case here ( well ... i think .... is debatable, really Very Happy ).

But I used antena.7 , and this fixed the LCO frequency. But the only way I found to calibrate the SRCO and TRCO was using that auto-calibrate command sequence after made the antenna LCO tuning.

I had lost two days to learn a lot of this sensor, tried many ways to calibrate all the oscillators, and the best I could was put the Antenna oscillator in 500.680 hz. But the SRCO is at 1.134 Mhz , and the TRCO is running at 32468 Hz. My room temperature is 25 Celsius.

The datasheet indicate that is possible to make the manual calibration for SRCO and TRCO, but I can't made this work...

My sensor is a little deaf, even at maximum sensibility only detects a strike very near.
I buy another today, maybe I have a better luck.

Paulo
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Mon Jan 02, 2017 2:00 pm    Post subject: Reply with quote

Antenna tuning DISP_LCO is bit 7 of register 08h.
In my case, SRCO and TRCO are operating normally with automatic calibration.

The manual states that it is necessary to perform the CALIB_RCO command (Write 96h to register 3Dh) when putting AS3935 into power down mode.
In case of normal power on (including reset), I think that RCO automatic calibration (bit 6 of register 08h) is good after antenna tuning.

Quote:
My sensor is a little deaf, even at maximum sensibility only detects a strike very near.

This sensor analyzes the components of lightning over time, so it may be difficult to test if it is not actual lightning.
Back to top
View user's profile Visit poster's website
aphawk

Bascom Member



Joined: 23 Jan 2010
Posts: 168
Location: Brazil

brazil.gif
PostPosted: Mon Jan 02, 2017 2:43 pm    Post subject: Reply with quote

O-Family,

Thanks a lot for your confirmation.

I found one small bug in the EDC code, and that was blinding my sensor.

If the sensor catches some disturb or high noise , the interrupt goes high. If there is a lot of noise, this can happens very quickly.

After the instruction CALL Tune_rco() , there is a delay before the interrupt be enabled, about 500 milliseconds.
If during this time the interrupt fires, the INT pin goes HIGH, and stay HIGH, waiting for the interrupt register be read, and only after this the interrupt can works again.

But the interrupt enabled in Atmega is a Changing Level interrupt. If the pin stay high, interrupt never will be triggered and the Read_als flag was not set !

And even if other interrupt fire INSIDE the Do : Loop code, this will blind the sensor again.

Then I modified the code, disabling the interrupt, and checking if the Interrupt pin are in High state, then the code can proceed to check what was reported by the sensor. This resolved the blinding situation.

After that, I had a lot of beeps, like a Geiger counter kkkkk .... and can make a better adjust of noise level and watchdog. I had 2 or 3 beeps every minute, reported as Disturbs, and now I set the flag to ignore the Disturb detection with this small code :

Code:

Helpb = Read_reg(&H03)
Helpb.5 = 1
Call Write_reg(&H03 , Helpb)
 


I think that now the sensor will catch more lightnings... almost every day one storm pass near my house ( its Summer here and with temperatures above 30 Celsius every day... )

I modified all the program to works with one Arduino Uno, and I'm using one PCF8574 LCD adapter in my LCD and using only I2C to make the wiring less complicated. I will test more one or two days and report the progress here.

EDC, I can post the code modified for Arduino Uno in the Arduino section of this Forum ( sure with your credits Very Happy ) ?


Paulo
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Mon Jan 02, 2017 3:20 pm    Post subject: Reply with quote

Hi aphawk

I am using the sensitivity setting of the sensor as the initial value.
It is an "indoor" mode.

Quote:
If the sensor catches some disturb or high noise , the interrupt goes high. If there is a lot of noise, this can happens very quickly.

Yes, the datasheet says that it will wait for 1 second after detecting lightning, but in fact it will detect continuously.
I think this is a sensor bug or a mistake in the datasheet.

After detecting the pin change interrupt, I detect the level of the [IRQ] pin in the main routine, judge the INT bit of register 03h, display it, read register 03h again and reset [IRQ] pin.

Quote:
After that, I had a lot of beeps, like a Geiger counter kkkkk .... and can make a better adjust of noise level and watchdog.

This sensor is a radio receiver. And it is very delicate!
Therefore, switching power supplies with poor performance can not be used for the power supply of the sensor.
We recommend batteries.

Also, if there is a computer, monitor display, or a device that generates noise near the sensor, the sensor will not operate normally.
Frequently detect noise and miss the lightning.
AVR is also desirable to sleep while waiting.
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Mon Jan 02, 2017 4:20 pm    Post subject: Reply with quote

aphawk wrote:
its Summer here and with temperatures above 30 Celsius every day...


Ahh summer Rolling Eyes Look what I have from about three months. This picture is taken by me today from my window Very Happy
So no storm or even rain in my localisation but I live in some kind of "national park" and my enviroment is quiet I think.
In my working code i have LED configured to show Input of Int pin.
Code:
Led Alias Portb.0
Do
...
Led = Int_pin
Loop

And this Led remains OFF for all the time.

After enabling PCINT it is serviced even after those 500ms. I use this also like Buzz check on Boot Very Happy

aphawk wrote:
I can post the code modified for Arduino Uno in the Arduino section of this Forum

I don`t have any problem with that Very Happy I want show idea behind how this sensor can be used and how it work. Bug can happend.

Like I wrote in my localisation is quiet and I have alert about Disturber maybe one, two times a day only.

I saw O-Family RCO tuning code part with Bit 6 but I`ve read my datasheet and with my 5 bit my sensor in autumn shows storms.
Code:
I2cbuff(1) = &H3D                                    
   I2cbuff(2) = &H96                                      
   Gosub As3935comtr                                        
   I2cbuff(1) = &H08                                      
   I2cbuff(2) = &H40 + Temp4     'Temp4 in O-Family code holds Antena tune value                        
   Gosub As3935comtr                                    
   Waitms 2                                                
   I2cbuff(1) = &H08                                      
   I2cbuff(2) = &H00 + Temp4                                
   Gosub As3935comtr


So I`m only happy if code can be immunized for other enviroments Very Happy
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Mon Jan 02, 2017 5:20 pm    Post subject: Reply with quote

Hi EDC
Quote:
I saw O-Family RCO tuning code part with Bit 6 but I`ve read my datasheet and with my 5 bit my sensor in autumn shows storms.

The data sheet you are looking at is old.
The latest version is below.
https://www.openhacks.com/uploadsproductos/as3935_datasheet_v1-04.pdf

I think that calibration of SRCO and TRCO will work with initial value if antenna tuning is done normally.
Therefore, even though mistook bit 5 and bit 6, I think there is no big influence on the detection of lightning.
Back to top
View user's profile Visit poster's website
aphawk

Bascom Member



Joined: 23 Jan 2010
Posts: 168
Location: Brazil

brazil.gif
PostPosted: Tue Jan 03, 2017 12:50 am    Post subject: Reply with quote

EDC,

Wow .... I think that maybe we can exchange our homes kkkkk ... at now, 21:08 , temperature outside my room is 29 Celsius, and is "so low" because some hours ago a storm with many rain passed here in São Paulo, Brasil...... really here is very hot ( and wet ) at this month.

I loved to see your picture, is like a dream to me. Snow, animals, quiet, all things I hoped ! What is the name of the city you live ?

Finally my sensor detected many strikes today ! From 27 Km ( the far ) until Storm Here, I'm happy with it.

My final goal is using this sensor with an ESP8266 with battery, this way I can put it at high, outside, some meters above my roof, sending only strikes report via WIFI using the website ThingSpeak. Will use the interrupt to wake up the ESP8266. But this is for some time ahead. I need to learn mode about ESP8266Basic to use sleep modes.

Now I will make small mods in this program, including one button to show the events in sequence with relative time between them.

I made one smalll routine to show the values of the SRCO and TRCO, will include then in the program.

Thanks for your permission to modify and share this project !


O-Family,

I see your project too, I learned a lot seeing your code. Thanks for sharing it !

I will make other project, and using one RTC, using the AVR sleep mode too, and awake with interrupt from sensor or pressing a button. Will use one small Nokia display too, but from now I will play a little more based in my Arduino Uno.

Paulo
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Tue Jan 03, 2017 4:13 am    Post subject: Reply with quote

Quote:
MFinally my sensor detected many strikes today ! From 27 Km ( the far ) until Storm Here, I'm happy with it.

that's nice!

Quote:
My final goal is using this sensor with an ESP8266 with battery, this way I can put it at high, outside, some meters above my roof, sending only strikes report via WIFI using the website ThingSpeak.

This sensor is designed to be usable even indoors.
If you can receive Medium wave radio broadcasts in your room, the AS3935 will fully inform us of the thunder's approach.

Quote:
I see your project too, I learned a lot seeing your code. Thanks for sharing it !

Although the source comment is Japanese, the language of BASCOM is common all over the world!
Thank you Mark.

I edited the motion video so that everyone can be helpful.
https://youtu.be/eRW2tWo0w4g
After detecting the lightning strike as noise, you can see how it is determined to be a lightning strike about 1 second later.
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum