AS3935 lightning detection using Arduino Uno

37

Some time ago I bought an AS3935 lightning sensor breakout board from Tindie. After some time I needed another sensor for a new project, only to find that it has been discontinued.

After some googling around I found the Thunder click board manufactured by the Mikroelektronika. The Thunder Click is a breakout board in the mikroBUS form factor, a standardized interface from Mikroelektronika. For the Arduino fans, there is an adapter shield that can accommodate two microBUS devices. Both the Thunder Click and the Click shield are also available from the EU distributor TME.

Thunderstorm detector project

Arduino Uno thunderstorm detector using a Mikoelektronika Thunder Click breakout board and an Arduino Click shield

In this example, the Thunder Click is installed on socket #1 of the adapter shield. The PWR_SEL bridge was set to 5V (originally all mikroBUS boards come set for 3.3V operation). The pin configuration is:

AS3935 CS – Arduino D10
AS3935 MOSI – Arduino D11
AS3935 MISO – Arduino D12
AS3935 SCK – Arduino D13
AS3935 INT – Arduino D2 (INT). Note this corresponds to Interrupt  #0

If the Socket #2 is used, the CS line connects to Arduino Pin D9, and the INT pin connects to Arduino pin D3, corresponding to Interrupt #1 (see http://arduino.cc/en/Reference/attachInterrupt for reference). Don’t forget to change the code to reflect the new pin settings.

The code is based on the original library written by Raivis Rengelis available at https://github.com/raivisr/AS3935-Arduino-Library with some modifications that are explained below:

First, I added a line to display the value of the calibration register if the calibration is successful.
Second, I moved all the code to display the events inside the interrupt routine itself. The main loop code just displays “Waiting” so I know that the system is not stuck. Now all I have to do is wait for a thunderstorm 🙂

The output of this program looks like this:

Tuning cap register is 7
Noise floor is: 5
Spike rejection is: 2
Watchdog threshold is: 2
Waiting…
Noise level too high, try adjusting noise floor
Waiting…

Waiting…
Disturber detected
Waiting…
Disturber detected
Waiting…
Waiting…
Lightning detected 27 kilometers away.
Storm overhead, watch out!
Waiting…

From time to time the sensor reports “Noise level too high”. A possible workaround and future work is to count the number of noise warnings in a given amount of time, and dynamically adjust the noise level using AS3935.setNoiseFloor() function until a compromise between noise warnings and sensitivity is reached.

Update: Normally Arduino disables all interrupt sources during the ISR routine, this is why they recommend keeping the ISR routine short. Delay() also uses interrupts, and it seems that in very rare situations this causes Arduino to hang. For the moment I consider this as being a minor inconvenient, but I have to see what happens during a summer thunderstorm when tens of interrupts per second will occur.

Update 6 April 2015: Commented AS3935Irqtriggered = 0; on code line 97. It caused an error when compiling.

 

Share.

37 Comments

  1. Hi Teodor, I am Adrian, also from Romania. I also got the MikroE ThunderClick a year ago and made something similar for thunder detection.
    I was stuck with Noise floor modification but your code showed very clear what has to be done.
    Many thanks (also a beer),
    Adrian

  2. Do you know how to manually set the tune cap to 4? I have tried every iteration of this code but have yet to find a way to manually set it to 4 since that’s what was printed on the outside cover of my unit I got from embedded adventures.

    • Hello Rick

      In the above code you can commment the code lines 79 and 80 (where the calibration is done) and replace them with:
      AS3935.registerWrite(AS3935_TUN_CAP,4);

      This will manually set the tuning capacitor to 4.

  3. Hi Theodor,
    I am quite new to this field and not very familiar with programming the arduino. To learn more about it,
    I assembled the Thunder Click on the Arduino using the adapter shield . I installed the AS3935 library from https://github.com/raivisr/AS3935-Arduino-Library and compiled your program using the arduine software.
    Unfortunately, when compiling the program, I get the following error:

    Lightning.ino:28:20: fatal error: AS3935.h: No such file or directory
    compilation terminated.

    When I check the installed libraries is shows that the AS3935-Arduino-Library-Master is installed.

    What did I do wrong?

    Looking forward hearing from you.
    Rien

    • Hi Rien

      Look in the “libraries” folder of your Arduino installation. Locate the “AS3935-Arduino-Libary-master” folder. Within this folder you’ll find a folder named AS3935 and a readme.md file.
      Take the AS3935 folder and move it directly in the libraries folder. Delete the AS3935-Arduino-Libary-master if you wish. It will then work.

      Kind regards,
      Teodor

      P.S. Don’t forget to change the PWR_SEL shunt to 5V or you will damage the click board.

      • Hi Teodor,

        It worked out perfectly. Thanks!
        Fortunately, I did change PWR_SEL shunt to 5V.

        Have you already been able to test the system with a real thunderstorm?

        Kind regards,
        Rien

        • Yes. A few days ago a thunderstorm passed some 10km away from my home. I got about 50 lightning detections, ranging from 8 to 19 km in distance. I didn’t compute their energy, but I noticed that most of cloud-to-ground lightnings were detected, while some cloud-to-cloud lightnings were missed.

          Regards,
          Teodor

  4. HI

    I have exactly same configuration. Unfortunately I’m facing a lot of troubles with the code. List of errors is long and my programming skills are low. Please can you check and help if possible. Thanks in advance
    Jarda
    CZ

    errors:

    LightningDetector:56: error: no matching function for call to ‘AS3935::AS3935(byte (&)(byte), int, int)’

    AS3935 AS3935(SPItransfer,10,2); //change to AS3935(SPITransfer,9,3) if using slot #2

    […]

    LightningDetector:166: error: ‘class AS3935’ has no member named ‘lightningDistanceKm’

    int strokeDistance = AS3935.lightningDistanceKm();

    ^

    exit status 1
    no matching function for call to ‘AS3935::AS3935(byte (&)(byte), int, int)’

    • Hi Jarda

      It looks that it’s something wrong with the AS3935 library. Have you installed it correctly?
      I have found that if you download the library written by Raivis Rengelis you won’t be able to install it in the regular “Sketch -> Include Library -> Add zip library” way. Instead you should manually extract the AS3935 folder into your Arduino libraries folder (typically in Documents -> Arduino -> Libaries). Then it works.
      Note: you have to place only the AS3935 folder, not the whole AS3935-Arduino-Library-master folder.

      If you still have problems please tell me – I’ll be happy to assist you.
      Teodor

      • Hi Teodor

        Great! You were right. I found that files “ccp” and “h” were placed in sub-folder “Arduino -> Libaries -> AS3935 -> src”. It happened when arduino software offer me upgrade to newer version of library AS3935 and I upgraded it.
        Thank you very much.

        Maybe one more question. Is there any way to store data from sensor in some file for later statistical processing, e.g. in excel?

        Jarda

        • If you wish to perform some data logging the simplest way is to use a microSD click placed in the second mikroBUS socket, data being logged in CSV format. Then you can import it in Excel. As for the code, you might wish to take a look at the SD card datalogger tutorial.
          If you need to log the time, things are a bit more complicated. If you don’t need a very precise timing you may use the Arduino Time library which keeps track of time using the main clock of the Arduino Uno. If you need very precise timings you will need an RTC – this will require a board with at least three mikroBUS sockets or one extra shield for the RTC.
          Regarding the use of RTC, you can take a look at the Arduino: data logger with MySQL – there is some RTC related code there.

          • Thanks a lot for the links and help. I don’t need to log precise times so I will try to use one of first two options.

            Have a nice day
            Jarda

        • Hello Teodor
          Finally we had storm today with multiple cells scattered around.
          However detector didn’t work as expected. One cell was roughly 2 km away with moderate activity. But I detected just few lightnings with different distance, like: Lightning detected 17 kilometers away (probably another cell). When there was lightning which I saw (2-3 km away), then detector shows “Disturber detected” or there was not reaction at all.
          Which parameter I can change to increase sensibility and accuracy?
          Thank you
          Jarda

          • Hello

            First you have to check if calibration is OK.
            Then you shoud set correctly set outoor or indoor modes, as this impacts sensitivity – this is in register 0x00, as well as watchdog settings in register 0x01, as described on pages 19-20 in datasheet.
            Finally, you can adjust the noise floor level in register 0x01.

            For the next week there are thunderstorms forecast in my area, so I will try myself some variations of the parameters for lighning. I will tell you when I have new results.

            Regards,
            Teodor

  5. Hello,

    when i start the console i only get this output first:

    Tuning out of range, check your wiring, your sensor and make sure physics laws have not changed!
    Tuning cap register is 0
    Noise floor is: 0
    Spike rejection is: 0
    Watchdog threshold is: 0

    As powered the device the frist time the power select shunt was set to 3,3V. Then i have read that it should be set to 5V.
    Could that be a problem and the board is damaged?

    I use a Mega with the same board like you as shown in the picture above. When i take the AS3935 board away it is the same output to the console.

    Best regards,
    Bastian

    • There’s a small chance to damage the board if powered with 5V and the jumper is set to 3.3V. However, my guess is that its just a logic level problem.
      I would first set the jumper to 5V, then try again.

      Also please be careful that the Mega has the SPI pins in a different position as compared with the UNO, and the Arduino Uno click shield is not compatible with the Mega.

  6. hello,

    thanks for the tutorial!
    some questions:
    it is not very clear to me, when should i turn off disturbers indication?

    in setup they are enabled at line: AS3935.enableDisturbers();
    and the comment says: // turn on indication of disturbers, once you have AS3935 all tuned, you can turn off with disableDisturbers();

    so, should i turn them off after the printAS3935Registers(); line?
    or what do you mean by “once you have AS3935 all tuned”?

    regarding calibration in the setup function: if i leave the sketch to run for very long time (several weeks / months) it should be re-calibrated time by time?

    it is possible to port this library on the arduino due? what should i modify?

    best regards,
    wanek

    • Hi!

      If you live in an urban environment you will have a lot of “disturber detected” indications. Sometimes it’s just easier to ignore those.

      However, when you are setting your device it pays off to look at the disturbers: too few ot too many could indicate wrong settings.

      The routine should work fine for the due. Just set the correct SPI clock – see https://www.arduino.cc/en/Reference/DueExtendedSPI and use attachInterrupt(digitalPinToInterrupt(your_interrupt_pin),AS3935Irq,RISING); instead of attachInterrupt(0,AS3935Irq,RISING);

      As for the calibration, it’s a good ideea to recalibrate from time to time.

      Regards,
      Teodor

  7. thank you!

    for the moment i will test this code on arduino uno. unfortunately, the chances for thunderstorms are theoretically nonexistent in this period in oradea, so it is not possible to do real tests.

    i would like to assemble a complex fixed environmental monitoring station. this thunder click sensor would be the part of a bigger project. all the registered data will be uploaded to a server via an enc28j60 module. for the time being i do some stability tests. with the uno i had frequent disconnections because of the high current consumption of the ethernet module. (the uno is not capable to supply enough current via the 3.3v regulator)

    the planned station will monitor: temperature, pressure, humidity, light, radioactive radiation, lightning, rain, etc. you can have a look here: http://www.uradmonitor.com/building-kit1-extra-features/

    it is worth to check this project: http://www.blynk.cc/ , it could be useful to you also. you can build lots of interesting stuff, and there is a great community behind it.

    regards,
    wanek

  8. Hi

    I added a I2C LCD in socket 2. I added lcd.print(“Disturber detected”); in void AS3935Irq(), but it stopped and no display. Don’t know what is the problem.

    void AS3935Irq()
    {
    // there is no need for this flag anymore
    // AS3935IrqTriggered = 1;

    // I move all the code for dysplaiying events inside the interrupt routine
    // again there is no need for this flag
    // reset the flag
    // AS3935IrqTriggered = 0;
    // first step is to find out what caused interrupt
    // as soon as we read interrupt cause register, irq pin goes low
    int irqSource = AS3935.interruptSource();
    // returned value is bitmap field, bit 0 – noise level too high, bit 2 – disturber detected, and finally bit 3 – lightning!
    if (irqSource & 0b0001)
    Serial.println(“Noise level too high, try adjusting noise floor”);
    lcd.print(“Noise level too high, try adjusting noise floor”);
    if (irqSource & 0b0100)
    Serial.println(“Disturber detected”);
    lcd.print(“Disturber detected”);
    if (irqSource & 0b1000)
    {
    // need to find how far that lightning stroke, function returns approximate distance in kilometers,
    // where value 1 represents storm in detector’s near victinity, and 63 – very distant, out of range stroke
    // everything in between is just distance in kilometers
    int strokeDistance = AS3935.lightningDistanceKm();
    if (strokeDistance == 1)
    Serial.println(“Storm overhead, watch out!”);
    if (strokeDistance == 63)
    Serial.println(“Out of range lightning detected.”);
    if (strokeDistance 1)
    {
    Serial.print(“Lightning detected “);
    Serial.print(strokeDistance,DEC);
    Serial.println(” kilometers away.”);

    • Teodor Costachioiu on

      Hi Hench,

      Unless otherwise speciffied, lcd.print() is for normal LCD. If you use I2C LCD you will also need to use speciffic libraries according to I2C controller, and initialize accordingly.
      Something like https://www.sunfounder.com/learn/Sensor-Kit-v2-0-for-Arduino/lesson-1-display-by-i2c-lcd1602-sensor-kit-v2-0-for-arduino.html or https://learn.adafruit.com/i2c-spi-lcd-backpack
      It also pays off to run first the code example that comes with your I2C LCD library to confirm that the display runs fine.

      • Hi Teodor,

        I used following I2C LCD library and connected the LCD in Sockket 2
        https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

        I added following codes at the beginning of your program:
        #include
        LiquidCrystal_I2C lcd(0x3F, 20, 4);

        I added following in the void set():
        lcd.begin();

        I tested:
        Adding lcd.print(“Noise floor is: “); in the void printAS3935Registers(), the LCD display correctly.

        However, if I added lcd.print(“Noise level too high”); in the void AS3935Irq()

        if (irqSource & 0b0001){
        Serial.println(“Noise level too high, try adjusting noise floor”);
        lcd.print(“Noise level too high”);}

        The serial monitor and the LCD display stopped

        CODING:

        #include
        #include
        #include
        LiquidCrystal_I2C lcd(0x3F, 20, 4);

        void printAS3935Registers();

        // Function prototype that provides SPI transfer and is passed to
        // AS3935 to be used from within library, it is defined later in main sketch.
        // That is up to user to deal with specific implementation of SPI
        // Note that AS3935 library requires this function to have exactly this signature
        // and it can not be member function of any C++ class, which happens
        // to be almost any Arduino library
        // Please make sure your implementation of choice does not deal with CS pin,
        // library takes care about it on it’s own
        byte SPItransfer(byte sendByte);
        // tunecap is needed to display the calibration register value
        int tunecap;

        // Iterrupt handler for AS3935 irqs
        // and flag variable that indicates interrupt has been triggered
        // Variables that get changed in interrupt routines need to be declared volatile
        // otherwise compiler can optimize them away, assuming they never get changed
        void AS3935Irq();
        // volatile int AS3935IrqTriggered; – not needed anymore

        // First parameter – SPI transfer function, second – Arduino pin used for CS
        // and finally third argument – Arduino pin used for IRQ
        // It is good idea to chose pin that has interrupts attached, that way one can use
        // attachInterrupt in sketch to detect interrupt
        // Library internally polls this pin when doing calibration, so being an interrupt pin
        // is not a requirement
        AS3935 AS3935(SPItransfer,10,2); //change to AS3935(SPITransfer,9,3) if using slot #2
        // AS3935 AS3935(SPItransfer,77,26); //if using Flip & Click socket A

        void setup()
        {
        lcd.begin();
        Serial.begin(9600);
        // first begin, then set parameters
        SPI.begin();
        // NB! chip uses SPI MODE1
        SPI.setDataMode(SPI_MODE1);
        // NB! max SPI clock speed that chip supports is 2MHz,
        // but never use 500kHz, because that will cause interference
        // to lightning detection circuit
        SPI.setClockDivider(SPI_CLOCK_DIV16);
        // and chip is MSB first
        SPI.setBitOrder(MSBFIRST);
        // reset all internal register values to defaults
        AS3935.reset();
        delay(10);
        AS3935.setOutdoors();
        AS3935.registerWrite(AS3935_NF_LEV,2);
        // and run calibration
        // if lightning detector can not tune tank circuit to required tolerance,
        // calibration function will return false
        if(!AS3935.calibrate())
        Serial.println(“Tuning out of range, check your wiring, your sensor and make sure physics laws have not changed!”);
        // now we print the value in the calibration register TUN_CAP
        // it is in the range 0 – 15
        tunecap=AS3935.registerRead(AS3935_TUN_CAP);
        Serial.print(“Tuning cap register is “);
        Serial.println(tunecap);

        // since this is demo code, we just go on minding our own business and ignore the fact that someone divided by zero

        // first let’s turn on disturber indication and print some register values from AS3935
        // tell AS3935 we are indoors, for outdoors use setOutdoors() function
        // AS3935.setOutdoors();
        // turn on indication of distrubers, once you have AS3935 all tuned, you can turn those off with disableDisturbers()
        AS3935.enableDisturbers();
        printAS3935Registers();

        // AS3935IrqTriggered = 0;
        // Using interrupts means you do not have to check for pin being set continiously, chip does that for you and
        // notifies your code
        // demo is written and tested on ChipKit MAX32, irq pin is connected to max32 pin 2, that corresponds to interrupt 1
        // look up what pins can be used as interrupts on your specific board and how pins map to int numbers
        // ChipKit Max32 – irq connected to pin 2, or Arduino with irq connected to pin 3
        // Uncomment the next line if using slot #2 of the Arduino mikroBUS adapter
        // attachInterrupt(1,AS3935Irq,RISING);
        // uncomment line below and comment out line above for Arduino Mega 2560, irq still connected to pin 2
        attachInterrupt(0,AS3935Irq,RISING);
        // attachInterrupt(digitalPinToInterrupt(26),AS3935Irq,RISING); // if using Flip & Click socket A
        }

        void loop()
        {
        // here we go into loop checking if interrupt has been triggered, which kind of defeats
        // the whole purpose of interrupts, but in real life you could put your chip to sleep
        // and lower power consumption or do other nifty things

        // I prefer to move this code inside the interrupt routine itself
        // Here I leave only some code to display “Waiting…” so I know everything works

        delay(1000);
        Serial.println(“Waiting…”);

        }

        void printAS3935Registers()
        {
        int noiseFloor = AS3935.getNoiseFloor();
        int spikeRejection = AS3935.getSpikeRejection();
        int watchdogThreshold = AS3935.getWatchdogThreshold();
        Serial.print(“Noise floor is: “);
        lcd.print(“Noise floor is: “);
        Serial.println(noiseFloor,DEC);
        lcd.print(noiseFloor,DEC);
        Serial.print(“Spike rejection is: “);
        Serial.println(spikeRejection,DEC);
        Serial.print(“Watchdog threshold is: “);
        Serial.println(watchdogThreshold,DEC);
        }

        // this is implementation of SPI transfer that gets passed to AS3935
        // you can (hopefully) wrap any SPI implementation in this
        byte SPItransfer(byte sendByte)
        {
        return SPI.transfer(sendByte);
        }

        // this is irq handler for AS3935 interrupts, has to return void and take no arguments
        // always make code in interrupt handlers fast and short
        void AS3935Irq()
        {
        // there is no need for this flag anymore
        // AS3935IrqTriggered = 1;

        // I move all the code for dysplaiying events inside the interrupt routine
        // again there is no need for this flag
        // reset the flag
        // AS3935IrqTriggered = 0;
        // first step is to find out what caused interrupt
        // as soon as we read interrupt cause register, irq pin goes low
        int irqSource = AS3935.interruptSource();
        // returned value is bitmap field, bit 0 – noise level too high, bit 2 – disturber detected, and finally bit 3 – lightning!
        if (irqSource & 0b0001){
        Serial.println(“Noise level too high, try adjusting noise floor”);
        lcd.print(“Noise level too high”);}
        if (irqSource & 0b0100)
        Serial.println(“Disturber detected”);
        if (irqSource & 0b1000)
        {
        // need to find how far that lightning stroke, function returns approximate distance in kilometers,
        // where value 1 represents storm in detector’s near victinity, and 63 – very distant, out of range stroke
        // everything in between is just distance in kilometers
        int strokeDistance = AS3935.lightningDistanceKm();
        if (strokeDistance == 1)
        Serial.println(“Storm overhead, watch out!”);
        if (strokeDistance == 63)
        Serial.println(“Out of range lightning detected.”);
        if (strokeDistance 1)
        {
        Serial.print(“Lightning detected “);
        Serial.print(strokeDistance,DEC);
        Serial.println(” kilometers away.”);
        }
        }
        }

        • Teodor Costachioiu on

          Hi,

          I think the LCD routine in somehow not happy when is called from the ISR (maybe it’s also using interrupts). My suggestion is to change the code such the interrupt routine returns a status variable, then use that variable to update the LCD in the main loop.

          Unfortunately this week I’m not in my office so I can’t write any code 🙁 But if it’s not urgent, next week I can reply with an updated code for I2C LCDs.

          • Hi Teodor,

            Thank you. The reason I want to connect a LCD (I2C) at socket2 is that I want to make above hardware portable. I found that if I connect above sensor (the Thunder Click, adapter field and UNO) to the notebook computer, I saw the serial monitor of Arduino IDE often display “Disturber detected”. But the weather in Sunny day. I doubt that the lighting sensor affected by the notebook computer.

  9. Hi Teodor,

    Thanks for sharing all this info! I use a brand new CJMCU sensor board from AliExpress on an Arduino Uno. Still getting the “Tuning out of range” message and no response with my piezo lightner. Do you know if is this is caused by CJMCU incompatibly issues?

    Best regards, Hans.

    Serial Monitor output:

    Tuning out of range, check your wiring, your sensor and make sure physics laws have not changed!
    Tuning cap register is 0
    Noise floor is: 2
    Spike rejection is: 2
    Watchdog threshold is: 2
    Waiting…
    Waiting…
    Disturber detected
    Waiting…
    Waiting…
    Waiting…
    Waiting…

  10. Hi Teodor,

    Thanks for fast replying! So it is hard to test but of coarse it a perfect that the AS3935 is immune for such disturbtions. In our region (The Netherlands) there are frequently thunderstorms this seasons so I need some patience and trow my 99 eurocent Action lightner in the garbage can 😉

    Do you think I can ignore the Tuning Out of Range message?

    Best regards, Hans.

    • Teodor Costachioiu on

      Hi Hans,

      No, you can’t ignore the tuning out of range message. Try writing 5 into the calibration register, instead of performing auto calibration, and see what happens (if you can read the value correctly). If you read 0 then you have a communication issue.

      Can you give me a link to the sensor board are you using?

  11. Hi Teodor,

    Thanks again for helping, I will try several calibration settings when the next thunderstorm arrives 😉 My AS3935 sensor board is sold via AE for about US$ 20 and there are many resellers. No complaints about the quality, the board looks great. Below the link to one of the sellers. There is a video on Youtube (https://www.youtube.com/watch?v=XP6oLxFdrVw) where Mirko Pavleski shows a working sensor board. Documents in the video (0:07) show the name Eyewink, possibly one of the brands of the sensor board. I have commented his video for more information via Youtube but no respond from Mirko so far.

    Kind regards, Hans.

    https://nl.aliexpress.com/item/New-AS3935-lightning-sensor-Lightning-lightning-lightning-detection-storm-distance/32830108558.html

    • Teodor Costachioiu on

      Hi,

      I took a quick look to your board, and there are some differences between that board and the Thunder Click. To use your board with an Arduino Uno, you will have to do the following connections:
      A0 and A1 will be connected to GND (those are the pins that select I2C address, on Thunder click they are grounded)
      EN_V is connected to Vcc (will enable internal voltage regulator)
      SI is connected to GND (sets communication mode as SPI)
      Then you use IRQ and SPI communication, just as in the blog post.

  12. Hi Teodor, this info is very helpful. Within the next days if will test connected as you wrote and let you know what happens. Thanks!

    Best regards, Hans.

  13. Sorry bordering you with my problems! I’ve found a sketch which shows next results with the sensor connected as you wrote:

    antenna calibration picks value: 11

    tune antenna to capacitor 0 gives frequency: 3710 = 593600 Hz
    tune antenna to capacitor 1 gives frequency: 1886 = 301760 Hz
    tune antenna to capacitor 2 gives frequency: 3766 = 602560 Hz
    tune antenna to capacitor 3 gives frequency: 1907 = 305120 Hz
    tune antenna to capacitor 4 gives frequency: 3808 = 609280 Hz
    tune antenna to capacitor 5 gives frequency: 1928 = 308480 Hz
    tune antenna to capacitor 6 gives frequency: 3798 = 607680 Hz
    tune antenna to capacitor 7 gives frequency: 1893 = 302880 Hz
    tune antenna to capacitor 8 gives frequency: 3764 = 602240 Hz
    tune antenna to capacitor 9 gives frequency: 1990 = 318400 Hz
    tune antenna to capacitor 10 gives frequency: 3769 = 603040 Hz
    tune antenna to capacitor 11 gives frequency: 2018 = 322880 Hz
    tune antenna to capacitor 12 gives frequency: 3737 = 597920 Hz
    tune antenna to capacitor 13 gives frequency: 1938 = 310080 Hz
    tune antenna to capacitor 14 gives frequency: 3801 = 608160 Hz
    tune antenna to capacitor 15 gives frequency: 1948 = 311680 Hz

    So I’ve commented and changed your code:

    AS3935.registerWrite(AS3935_TUN_CAP,11);
    //if(!AS3935.calibrate())
    //Serial.println(“Tuning out of range, check your wiring, your sensor and make sure physics laws have not changed!”);
    //now we print the value in the calibration register TUN_CAP
    //it is in the range 0 – 15
    tunecap=AS3935.registerRead(AS3935_TUN_CAP);
    Serial.print(“Tuning cap register is “);
    Serial.println(tunecap);

    Is this what you ment? If so I will test values 0-15 when the first thunderstorm arrives.

    • Teodor Costachioiu on

      Hi,

      What puzzles me is widespread of frequencies and the fact that none of the frequency values is close to 500KHz. Choosing 11 is also wrong, as capacitor = 0 gives frequency: 3710 = 593600 which is the closest to 500KHz, but still with 100KHz outside of the recommended value.

      Good news is that I have (finally) bought the AS3935 development kit, making a 300$ hole in my budget. It came with source code for PIC microcontroller, and it will take me a few days to understand how it performs the calibration.

      Until then, I can tell you that the dev kit works better than the Arduino counterpart. Yesterday I had a minor storm approaching, and the dev kit didn’t miss any lightning strikes. The Arduino performed much worse.

  14. Hi Teodor,

    No succes so far when using SPI with my CJMCU AS3935 board, so I use I2C now which works fine with Raivis Rengelis AS3935 library (https://github.com/raivisr/AS3935-Arduino-Library) and Wyne Truchsess I2C library (https://github.com/ninjablocks/arduino/blob/master/I2C/I2C.h). If someone intends to use this board also, these are my Uno connections:

    Vcc – 5 volt
    GND – GND
    SCL – A5
    MOSI – A4
    SI – 5 volt (enable I2C)
    IRQ – D2
    A0 – 5 volt (I2C address)
    A1 – 5 volt (I2C address)

    An I2C scanner sketch shows address 0x3 when connect this way. Hope this information is useful for other users of this board.

    Again thanks for your help and I hope to read here sooner or later about your great AS3935 development kit!

    Best regards, Hans.

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.