Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. i2c Lightning Sensor +

i2c Lightning Sensor +

Scheduled Pinned Locked Moved My Project
16 Posts 5 Posters 10.3k Views 8 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Boots33B Offline
    Boots33B Offline
    Boots33
    Hero Member
    wrote on last edited by
    #5

    Just an update on this project. It took a while but we finally entered the storm season a couple of months back. This allowed me to get some real world data at last which has led to a few changes to this project. Firstly I have moved the Lightning sensor to its own housing with no other sensors/actuators. The reason for this is I would occasionally get false lightning strikes when the switches were operated.

    I have also made a few changes to the sketch. I now use the dust sensor for both distance and intensity readings, this gives a better display in Domoticz and of course I no longer needed the code for the switches etc. This code is working on MySensors V2.1.1

    0_1486273154916_display.jpg

    I have mounted it all on a new board and have fitted it to one of my tube cases. These cases by the way are working out great for outside housings. I have a few scattered around the yard now and they have all performed well through our wet season, no signs of water in them at all and very easy to get at the arduino if i need to.

    0_1486273479326_board.jpg

    I hot glued the NRF to the lid

    0_1486273534399_nrf.jpg

    And then it all just slips into the tube

    0_1486273589577_complete.jpg

    I am still using i2c for the interface. It has been interesting watching the readouts as storms approach.

    The Sketch:

    /* This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board
    * with the MySensors V2 environment and is based on a sketch provided by Playing With Fusion
    * http://playingwithfusion.com/productview.php?pdid=22&catid=1001 and from the MySensors
    * forum post at  https://forum.mysensors.org/topic/880/sketch-for-lightning-sensor
    * 
    * Circuit:
    *    Arduino Uno     -->  SEN-39001: AS3935 Breakout
    *    SDA:    SDA     -->  MOSI/SDA   (SDA is labeled on the bottom of the Arduino)
    *    SCLK:   SCL     -->  SCK/SCL    (SCL is labeled on the bottom of the Arduino)
    *    SI:     pin  8  -->  SI (select interface; GND=SPI, VDD=I2C
    *    IRQ:    pin  3  -->  IRQ
    *    GND:    GND     -->  CS (pull CS to ground even though it's not used)
    *    GND:    GND     -->  GND
    *    5V:     5V      -->  Arduino I/O is at 5V, so power board from 5V. Can use 3.3V with Due, etc
    */
    
    
    
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enabled repeater feature for this node
    #define MY_REPEATER_FEATURE
    #define MY_NODE_ID 11
    #define MY_RF24_CHANNEL 84
    #define MY_TRANSPORT_WAIT_READY_MS 3000     //set how long to wait for transport ready.
    
    #include "I2C.h"                           // the lightning sensor can communicate via SPI or I2C. This sketch uses the I2C interface
    #include "PWFusion_AS3935_I2C.h"           // include Playing With Fusion AXS3935 libraries
    #include "SPI.h"
    #include <MySensors.h>
    
    /*------- defines for hardware config---------*/
    #define SI_PIN               8              // this pin will be switched high for i2c
    #define IRQ_PIN              3              // digital pins 2 and 3 are available for interrupt capability
    #define AS3935_ADD           0x03           // x03 - standard PWF SEN-39001-R01 config
    #define AS3935_CAPACITANCE   48             // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD                     
    volatile int8_t AS3935_ISR_Trig = 0;
    
    /*---- #defines for general chip settings----*/
    #define AS3935_INDOORS       0                       //use this setting if the sensor is indoors... more sensitive 
    #define AS3935_OUTDOORS      1                       //use this for sensor outdoors... less sensitive
    #define AS3935_DIST_DIS      0                       // dissable disturber reporting
    #define AS3935_DIST_EN       1                        // enable disturber reporting.... handy when setting up
    
    // prototypes
    void AS3935_ISR();
    
    PWF_AS3935_I2C  lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD);
    #define CHILD_ID_DISTANCE 1
    #define CHILD_ID_INTENSITY 2
    unsigned long heartbeatDelay = 120;       // how often the heartbeat will be sent, in minutes
    unsigned long lastHeartbeat = millis();    // holder for last time heartbeat was sent
    
    
    MyMessage msgDist(CHILD_ID_DISTANCE, V_LEVEL);
    MyMessage msgInt(CHILD_ID_INTENSITY, V_LEVEL);
    
    void setup()
    {
      Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01");
      Serial.println("beginning boot procedure....");
    
      /* setup for the the I2C library: (enable pullups, set speed to 400kHz) */
      I2c.begin();
      I2c.pullup(true);
      I2c.setSpeed(1);
      //delay(2);
       wait(2); 
      lightning0.AS3935_DefInit();            // set registers to default
      // now update sensor cal for your application and power up chip
      lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_DIS);  // capacitance , inside or outside , disturbers enabled or disabled
      // AS3935_ManualCal Parameters:
      //   --> capacitance, in pF (marked on package)
      //   --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
      //   --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:0)
      // function also powers up the chip
    
      // enable interrupt (hook IRQ pin to Arduino Pro Mini, Nano, Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
      attachInterrupt(1, AS3935_ISR, RISING);
      // dump the registry data to the serial port for troubleshooting purposes
      lightning0.AS3935_PrintAllRegs();
      AS3935_ISR_Trig = 0;           // clear trigger
      // delay execution to allow chip to stabilize.
      //delay(1000);
      wait(1000);
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Lightning Sensor", "1.0");
    
      // Register all sensors to gateway (they will be created as child devices)
      present(CHILD_ID_DISTANCE, S_DUST, "Lightning Distance");
      present(CHILD_ID_INTENSITY, S_DUST, "Lightning Intensity");
    }
    
    
    
    void loop()
    {
    
     if ( AS3935_ISR_Trig == 1){     // check to see if interrupt triggered
        wait(5);
        lightningDetected();             // call the lightning function
     }
       // wait(5);
       heartbeatCheck();                                    // call heartbeat function
    
    }
    
    /*-------------------------start of functions-------------------*/
    
    void heartbeatCheck(){
    unsigned long millisNow = millis();           // get the current time
    if ((millisNow - lastHeartbeat) > (heartbeatDelay*60000)) {  
     
      sendHeartbeat();
      
      lastHeartbeat = millis();
      #ifdef MY_DEBUG
        Serial.println("Heartbeat Sent" );
      #endif
    }
    }
    
    // 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 AS3935_ISR()
    {
      AS3935_ISR_Trig = 1;
    }
    
    /*--------------Lightning function----------------*/
    void lightningDetected() {
      
      AS3935_ISR_Trig = 0;                     // reset interrupt flag
    
      
      uint8_t int_src = lightning0.AS3935_GetInterruptSrc();     // now get interrupt source
    
      
    /*---0 = Unknown, 1 = Lightning detected, 2 = Disturber detected, 3 = Noise level too high ---*/
       switch (int_src) {
           case 0: {
              #ifdef MY_DEBUG                                                 
              Serial.println("Unknown interrupt source");
              #endif
              }
              break;
            case 1: {                                                      
              uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
              uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw();
              #ifdef MY_DEBUG
              Serial.print("Lightning detected! Distance to strike: ");
              Serial.print(lightning_dist_km);
              Serial.println(" kilometers");
              Serial.print("Lightning detected! Lightning Intensity: ");
              Serial.println(lightning_intensity);
              #endif
              send(msgDist.set(lightning_dist_km)); 
              wait (10);
              send(msgInt.set(lightning_intensity));
              }
              break;
            case 2: {
              #ifdef MY_DEBUG                                                    
              Serial.println("Disturber detected");
              #endif
              }
              break;
            case 3: {
              #ifdef MY_DEBUG                                                   
              Serial.println("Noise level too high");
              #endif
              }
              break;
         }        
     }
    
    
    
    
    
    
    1 Reply Last reply
    1
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #6

      How much did you pay for the sensor? I looked around and that's not very cheap for such small board. Does the library only works for that specific vendor or is it universal as long as the same chip is used of course?

      H Boots33B 2 Replies Last reply
      0
      • gohanG gohan

        How much did you pay for the sensor? I looked around and that's not very cheap for such small board. Does the library only works for that specific vendor or is it universal as long as the same chip is used of course?

        H Offline
        H Offline
        HarryDutch
        wrote on last edited by
        #7

        @gohan Maybe this one?
        http://www.ebay.com/itm/EMP-sensor-detector-module-for-lightning-detection-/302051777514?hash=item4653b05bea:g:L8UAAOSwLnBX2sVY

        1 Reply Last reply
        0
        • gohanG gohan

          How much did you pay for the sensor? I looked around and that's not very cheap for such small board. Does the library only works for that specific vendor or is it universal as long as the same chip is used of course?

          Boots33B Offline
          Boots33B Offline
          Boots33
          Hero Member
          wrote on last edited by Boots33
          #8

          @gohan I can't remember the exact price, i think it was around $40 back then. They are a little cheaper now I Have a weather station setup so it was a nice addition.
          The Library is written to work with their module although you could probably use it as a base if you had the same chip from another supplier. The playingwithfusion modules come pre calibrated which makes them a breeze to use. There are other modules also available with their own libraries as well.

          1 Reply Last reply
          1
          • YFandSY Offline
            YFandSY Offline
            YFandS
            wrote on last edited by
            #9

            Hello Boots33,

            I flashed your sketch in an Arduino pro mini, it is registering but "both" sensors did not show in Domoticz. I had to trigger them (send a msg with values) after that Domoticz showed them both. The heartbeat is working from the mysensors point of view, confirmation from the Arduino monitor and a led flashing on the pro mini (I activated the err, tx, rx option) but Domoticz won't register the heartbeat, can you confirm that?

            Regards
            Frank

            Boots33B 1 Reply Last reply
            0
            • YFandSY YFandS

              Hello Boots33,

              I flashed your sketch in an Arduino pro mini, it is registering but "both" sensors did not show in Domoticz. I had to trigger them (send a msg with values) after that Domoticz showed them both. The heartbeat is working from the mysensors point of view, confirmation from the Arduino monitor and a led flashing on the pro mini (I activated the err, tx, rx option) but Domoticz won't register the heartbeat, can you confirm that?

              Regards
              Frank

              Boots33B Offline
              Boots33B Offline
              Boots33
              Hero Member
              wrote on last edited by
              #10

              @YFandS said in i2c Lightning Sensor +:

              Hello Boots33,

              I flashed your sketch in an Arduino pro mini, it is registering but "both" sensors did not show in Domoticz. I had to trigger >them (send a msg with values) after that Domoticz showed them both.

              Yes that is a Domoticz thing i think. the node and sensors will show up in the mysensors hardware device after being presented but the sensors will not be seen in the Devices list until they have sent some data. Domoticz will do this with other sensors as well.

              The heartbeat is working from the mysensors point of view, confirmation from the Arduino monitor and a led flashing on >the pro mini (I activated the err, tx, rx option) but Domoticz won't register the heartbeat, can you confirm that?

              I experienced the same issue, as you said the heartbeat message is shown to be sent in the serial monitor but no update is registered in domoticz. At the time i thought it would be something at the Domoticz end and would be fixed in a future release. I am fairly sure i am using heartbeat correctly. If someone can shed some light on this i would be grateful.

              Sorry I have been a bit remiss with this project, I should have mentioned this in a followup post.

              I have been meaning to modify the sketch a bit anyway as I am not completely happy with how the data is shown. At the moment the data displays the last strike until new data is available. This means even if there is no lightning around you will still see the most recent strike data.

              I would prefer the display to be reset after a period of time until the next storm period. I think I will use this to both clear the display and also be used as a heartbeat. It will only need minor changes to the current heartbeatCheck function.

              One last thing I found that even though my sensor is outdoors i get the best results using AS3935_INDOORS in the setup line. :)

              1 Reply Last reply
              0
              • YFandSY Offline
                YFandSY Offline
                YFandS
                wrote on last edited by
                #11

                Thank for the reply,

                I found this online: https://github.com/domoticz/domoticz/issues/1396
                march 23, 2017

                the short version is:
                gizmocuz commented on 25 Mar

                Could be around the switch case 492, i am only handling the heartbeat for the following value types:

                		case V_TRIPPED:
                		case V_ARMED:
                		case V_LOCK_STATUS:
                		case V_STATUS:
                		case V_PERCENTAGE:
                		case V_SCENE_ON:
                		case V_SCENE_OFF:
                		case V_UP:
                		case V_DOWN:
                		case V_STOP:
                		case V_RGB:
                		case V_RGBW:
                

                Maybe we miss some... it could be a complicated issue to support all types, as for all types we should recover the IDX in the database, or we should remember this...

                End
                I checked the Domoticz source files and still no change (11 sep 2017) so no heartbeat with v_values.

                Regards
                Frank

                1 Reply Last reply
                1
                • Boots33B Offline
                  Boots33B Offline
                  Boots33
                  Hero Member
                  wrote on last edited by
                  #12

                  I have made the changes I mentioned above. The node now resets the lightning data to 0 every hour. This I hope will give a more meaningful log over time and also provide a heartbeat to indicate the node is functioning. If lightning is detected the data reset timer begins its count from the beginning, this is to stop the data from being reset too soon.

                  /* This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board
                  * with the MySensors V2 environment and is based on a sketch provided by Playing With Fusion
                  * http://playingwithfusion.com/productview.php?pdid=22&catid=1001 and from the MySensors
                  * forum post at  https://forum.mysensors.org/topic/880/sketch-for-lightning-sensor
                  * 
                  * Circuit:
                  *    Arduino Uno     -->  SEN-39001: AS3935 Breakout
                  *    SDA:    SDA     -->  MOSI/SDA   (SDA is labeled on the bottom of the Arduino)
                  *    SCLK:   SCL     -->  SCK/SCL    (SCL is labeled on the bottom of the Arduino)
                  *    SI:     pin  8  -->  SI (select interface; GND=SPI, VDD=I2C
                  *    IRQ:    pin  3  -->  IRQ
                  *    GND:    GND     -->  CS (pull CS to ground even though it's not used)
                  *    GND:    GND     -->  GND
                  *    5V:     5V      -->  Arduino I/O is at 5V, so power board from 5V. Can use 3.3V with Due, etc
                  */
                  
                  
                  
                  
                  
                  // Enable debug prints to serial monitor
                  #define MY_DEBUG
                  
                  // Enable and select radio type attached
                  #define MY_RADIO_NRF24
                  
                  // Enabled repeater feature for this node
                  #define MY_REPEATER_FEATURE
                  #define MY_NODE_ID 11
                  #define MY_RF24_CHANNEL 84
                  #define MY_TRANSPORT_WAIT_READY_MS 3000     //set how long to wait for transport ready.
                  
                  #include "I2C.h"                           // the lightning sensor can communicate via SPI or I2C. This sketch uses the I2C interface
                  #include "PWFusion_AS3935_I2C.h"           // include Playing With Fusion AXS3935 libraries
                  #include "SPI.h"
                  #include <MySensors.h>
                  
                  /*------- defines for hardware config---------*/
                  #define SI_PIN               8              // this pin will be switched high for i2c
                  #define IRQ_PIN              3              // digital pins 2 and 3 are available for interrupt capability
                  #define AS3935_ADD           0x03           // x03 - standard PWF SEN-39001-R01 config
                  #define AS3935_CAPACITANCE   48             // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD                     
                  volatile int8_t AS3935_ISR_Trig = 0;
                  
                  /*---- #defines for general chip settings----*/
                  #define AS3935_INDOORS       0                       //use this setting if the sensor is indoors... more sensitive 
                  #define AS3935_OUTDOORS      1                       //use this for sensor outdoors... less sensitive
                  #define AS3935_DIST_DIS      0                       // dissable disturber reporting
                  #define AS3935_DIST_EN       1                        // enable disturber reporting.... handy when setting up
                  
                  // prototypes
                  void AS3935_ISR();
                  
                  PWF_AS3935_I2C  lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD);
                  #define CHILD_ID_DISTANCE 1
                  #define CHILD_ID_INTENSITY 2
                  
                  unsigned long lastReset = millis();    // holder for last time reset was triggered
                  unsigned long resetTime = 60;          //  time between data resets in minutes
                  bool firstBoot = true;
                  
                  MyMessage msgDist(CHILD_ID_DISTANCE, V_LEVEL);
                  MyMessage msgInt(CHILD_ID_INTENSITY, V_LEVEL);
                  
                  void setup()
                  {
                    Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01");
                    Serial.println("beginning boot procedure....");
                  
                    /* setup for the the I2C library: (enable pullups, set speed to 400kHz) */
                    I2c.begin();
                    I2c.pullup(true);
                    I2c.setSpeed(1);
                    wait(2); 
                    lightning0.AS3935_DefInit();            // set registers to default
                    // now update sensor cal for your application and power up chip
                    lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_INDOORS, AS3935_DIST_EN);  // capacitance , inside or outside , disturbers enabled or disabled
                    // AS3935_ManualCal Parameters:
                    //   --> capacitance, in pF (marked on package)
                    //   --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
                    //   --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:0)
                    // function also powers up the chip
                  
                    // enable interrupt (hook IRQ pin to Arduino Pro Mini, Nano, Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
                    attachInterrupt(1, AS3935_ISR, RISING);
                    // dump the registry data to the serial port for troubleshooting purposes
                    lightning0.AS3935_PrintAllRegs();
                    AS3935_ISR_Trig = 0;           // clear trigger
                    
                    wait(1000);                    // delay execution to allow chip to stabilize.
                  }
                  
                  void presentation()  {
                    // Send the sketch version information to the gateway and Controller
                    sendSketchInfo("Lightning Sensor", "1.1");
                  
                    // Register all sensors to gateway (they will be created as child devices)
                    present(CHILD_ID_DISTANCE, S_DUST, "Lightning Distance");
                    present(CHILD_ID_INTENSITY, S_DUST, "Lightning Intensity");
                  }
                  
                  
                  
                  void loop()
                  {
                  
                  if (firstBoot){     //When node first boots clear the lightning data
                   send(msgDist.set(0)); 
                    wait (10);
                    send(msgInt.set(0));
                    firstBoot = false; 
                  }
                    
                    // This program only handles an AS3935 lightning sensor. It does nothing until
                    // an interrupt is detected on the IRQ pin.
                    //while(0 == AS3935_ISR_Trig){}
                   // wait(5);
                   if ( AS3935_ISR_Trig == 1){
                      wait(5);
                      lightningDetected();             // call the lighning function
                   }
                     // wait(5);
                     resetCheck();                      // call data reset function
                  
                  }
                  
                  /*-------------------------start of functions-------------------*/
                  
                  void resetCheck(){
                  unsigned long millisNow = millis();           // get the current time
                  if ((millisNow - lastReset) > (resetTime*60000)) {  
                    lastReset = millis();
                    send(msgDist.set(0));       // send lightning distance
                    wait (10);
                    send(msgInt.set(0));        // send lightning intensity
                  }
                  }
                  
                  // 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 AS3935_ISR()
                  {
                    AS3935_ISR_Trig = 1;
                  }
                  
                  /*--------------Lightning function----------------*/
                  void lightningDetected() {
                    
                    AS3935_ISR_Trig = 0;                     // reset interrupt flag
                  
                    
                    uint8_t int_src = lightning0.AS3935_GetInterruptSrc();     // now get interrupt source
                  
                    
                  /*---0 = Unknown, 1 = Lightning detected, 2 = Disturber detected, 3 = Noise level too high ---*/
                     switch (int_src) {
                         case 0: {
                            #ifdef MY_DEBUG                                                 
                            Serial.println("Unknown interrupt source");
                            #endif
                            }
                            break;
                          case 1: {                                                      
                            uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
                            uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw();
                            #ifdef MY_DEBUG
                            Serial.print("Lightning detected! Distance to strike: ");
                            Serial.print(lightning_dist_km);
                            Serial.println(" kilometers");
                            Serial.print("Lightning detected! Lightning Intensity: ");
                            Serial.println(lightning_intensity);
                            #endif
                            send(msgDist.set(lightning_dist_km)); 
                            wait (10);
                            send(msgInt.set(lightning_intensity));
                            lastReset = millis();
                            }
                            break;
                          case 2: {
                            #ifdef MY_DEBUG                                                    
                            Serial.println("Disturber detected");
                            #endif
                            }
                            break;
                          case 3: {
                            #ifdef MY_DEBUG                                                   
                            Serial.println("Noise level too high");
                            #endif
                            }
                            break;
                       }        
                   }
                  
                  
                  
                  
                  
                  MiKaM 1 Reply Last reply
                  3
                  • Boots33B Boots33

                    I have made the changes I mentioned above. The node now resets the lightning data to 0 every hour. This I hope will give a more meaningful log over time and also provide a heartbeat to indicate the node is functioning. If lightning is detected the data reset timer begins its count from the beginning, this is to stop the data from being reset too soon.

                    /* This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board
                    * with the MySensors V2 environment and is based on a sketch provided by Playing With Fusion
                    * http://playingwithfusion.com/productview.php?pdid=22&catid=1001 and from the MySensors
                    * forum post at  https://forum.mysensors.org/topic/880/sketch-for-lightning-sensor
                    * 
                    * Circuit:
                    *    Arduino Uno     -->  SEN-39001: AS3935 Breakout
                    *    SDA:    SDA     -->  MOSI/SDA   (SDA is labeled on the bottom of the Arduino)
                    *    SCLK:   SCL     -->  SCK/SCL    (SCL is labeled on the bottom of the Arduino)
                    *    SI:     pin  8  -->  SI (select interface; GND=SPI, VDD=I2C
                    *    IRQ:    pin  3  -->  IRQ
                    *    GND:    GND     -->  CS (pull CS to ground even though it's not used)
                    *    GND:    GND     -->  GND
                    *    5V:     5V      -->  Arduino I/O is at 5V, so power board from 5V. Can use 3.3V with Due, etc
                    */
                    
                    
                    
                    
                    
                    // Enable debug prints to serial monitor
                    #define MY_DEBUG
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_NRF24
                    
                    // Enabled repeater feature for this node
                    #define MY_REPEATER_FEATURE
                    #define MY_NODE_ID 11
                    #define MY_RF24_CHANNEL 84
                    #define MY_TRANSPORT_WAIT_READY_MS 3000     //set how long to wait for transport ready.
                    
                    #include "I2C.h"                           // the lightning sensor can communicate via SPI or I2C. This sketch uses the I2C interface
                    #include "PWFusion_AS3935_I2C.h"           // include Playing With Fusion AXS3935 libraries
                    #include "SPI.h"
                    #include <MySensors.h>
                    
                    /*------- defines for hardware config---------*/
                    #define SI_PIN               8              // this pin will be switched high for i2c
                    #define IRQ_PIN              3              // digital pins 2 and 3 are available for interrupt capability
                    #define AS3935_ADD           0x03           // x03 - standard PWF SEN-39001-R01 config
                    #define AS3935_CAPACITANCE   48             // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD                     
                    volatile int8_t AS3935_ISR_Trig = 0;
                    
                    /*---- #defines for general chip settings----*/
                    #define AS3935_INDOORS       0                       //use this setting if the sensor is indoors... more sensitive 
                    #define AS3935_OUTDOORS      1                       //use this for sensor outdoors... less sensitive
                    #define AS3935_DIST_DIS      0                       // dissable disturber reporting
                    #define AS3935_DIST_EN       1                        // enable disturber reporting.... handy when setting up
                    
                    // prototypes
                    void AS3935_ISR();
                    
                    PWF_AS3935_I2C  lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD);
                    #define CHILD_ID_DISTANCE 1
                    #define CHILD_ID_INTENSITY 2
                    
                    unsigned long lastReset = millis();    // holder for last time reset was triggered
                    unsigned long resetTime = 60;          //  time between data resets in minutes
                    bool firstBoot = true;
                    
                    MyMessage msgDist(CHILD_ID_DISTANCE, V_LEVEL);
                    MyMessage msgInt(CHILD_ID_INTENSITY, V_LEVEL);
                    
                    void setup()
                    {
                      Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01");
                      Serial.println("beginning boot procedure....");
                    
                      /* setup for the the I2C library: (enable pullups, set speed to 400kHz) */
                      I2c.begin();
                      I2c.pullup(true);
                      I2c.setSpeed(1);
                      wait(2); 
                      lightning0.AS3935_DefInit();            // set registers to default
                      // now update sensor cal for your application and power up chip
                      lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_INDOORS, AS3935_DIST_EN);  // capacitance , inside or outside , disturbers enabled or disabled
                      // AS3935_ManualCal Parameters:
                      //   --> capacitance, in pF (marked on package)
                      //   --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
                      //   --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:0)
                      // function also powers up the chip
                    
                      // enable interrupt (hook IRQ pin to Arduino Pro Mini, Nano, Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
                      attachInterrupt(1, AS3935_ISR, RISING);
                      // dump the registry data to the serial port for troubleshooting purposes
                      lightning0.AS3935_PrintAllRegs();
                      AS3935_ISR_Trig = 0;           // clear trigger
                      
                      wait(1000);                    // delay execution to allow chip to stabilize.
                    }
                    
                    void presentation()  {
                      // Send the sketch version information to the gateway and Controller
                      sendSketchInfo("Lightning Sensor", "1.1");
                    
                      // Register all sensors to gateway (they will be created as child devices)
                      present(CHILD_ID_DISTANCE, S_DUST, "Lightning Distance");
                      present(CHILD_ID_INTENSITY, S_DUST, "Lightning Intensity");
                    }
                    
                    
                    
                    void loop()
                    {
                    
                    if (firstBoot){     //When node first boots clear the lightning data
                     send(msgDist.set(0)); 
                      wait (10);
                      send(msgInt.set(0));
                      firstBoot = false; 
                    }
                      
                      // This program only handles an AS3935 lightning sensor. It does nothing until
                      // an interrupt is detected on the IRQ pin.
                      //while(0 == AS3935_ISR_Trig){}
                     // wait(5);
                     if ( AS3935_ISR_Trig == 1){
                        wait(5);
                        lightningDetected();             // call the lighning function
                     }
                       // wait(5);
                       resetCheck();                      // call data reset function
                    
                    }
                    
                    /*-------------------------start of functions-------------------*/
                    
                    void resetCheck(){
                    unsigned long millisNow = millis();           // get the current time
                    if ((millisNow - lastReset) > (resetTime*60000)) {  
                      lastReset = millis();
                      send(msgDist.set(0));       // send lightning distance
                      wait (10);
                      send(msgInt.set(0));        // send lightning intensity
                    }
                    }
                    
                    // 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 AS3935_ISR()
                    {
                      AS3935_ISR_Trig = 1;
                    }
                    
                    /*--------------Lightning function----------------*/
                    void lightningDetected() {
                      
                      AS3935_ISR_Trig = 0;                     // reset interrupt flag
                    
                      
                      uint8_t int_src = lightning0.AS3935_GetInterruptSrc();     // now get interrupt source
                    
                      
                    /*---0 = Unknown, 1 = Lightning detected, 2 = Disturber detected, 3 = Noise level too high ---*/
                       switch (int_src) {
                           case 0: {
                              #ifdef MY_DEBUG                                                 
                              Serial.println("Unknown interrupt source");
                              #endif
                              }
                              break;
                            case 1: {                                                      
                              uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
                              uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw();
                              #ifdef MY_DEBUG
                              Serial.print("Lightning detected! Distance to strike: ");
                              Serial.print(lightning_dist_km);
                              Serial.println(" kilometers");
                              Serial.print("Lightning detected! Lightning Intensity: ");
                              Serial.println(lightning_intensity);
                              #endif
                              send(msgDist.set(lightning_dist_km)); 
                              wait (10);
                              send(msgInt.set(lightning_intensity));
                              lastReset = millis();
                              }
                              break;
                            case 2: {
                              #ifdef MY_DEBUG                                                    
                              Serial.println("Disturber detected");
                              #endif
                              }
                              break;
                            case 3: {
                              #ifdef MY_DEBUG                                                   
                              Serial.println("Noise level too high");
                              #endif
                              }
                              break;
                         }        
                     }
                    
                    
                    
                    
                    
                    MiKaM Offline
                    MiKaM Offline
                    MiKa
                    wrote on last edited by
                    #13

                    @boots33
                    which I2C library did You used???

                    gohanG 1 Reply Last reply
                    0
                    • MiKaM MiKa

                      @boots33
                      which I2C library did You used???

                      gohanG Offline
                      gohanG Offline
                      gohan
                      Mod
                      wrote on last edited by
                      #14

                      @mika I'd guess #include "PWFusion_AS3935_I2C.h" by looking at the code 😀

                      1 Reply Last reply
                      0
                      • MiKaM Offline
                        MiKaM Offline
                        MiKa
                        wrote on last edited by
                        #15

                        @boots33 said in i2c Lightning Sensor +:

                        #include "I2C.h" // the lightning sensor can communicate via SPI or I2C. This sketch uses the I2C interface

                        I mean this:

                        #include "I2C.h" // the lightning sensor can communicate via SPI or I2C. This sketch uses the I2C interface

                        1 Reply Last reply
                        0
                        • Boots33B Offline
                          Boots33B Offline
                          Boots33
                          Hero Member
                          wrote on last edited by
                          #16

                          The Playing with fusion sketch uses the I2C library found here

                          some more info on the library here

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          14

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          Posts


                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • MySensors
                          • OpenHardware.io
                          • Categories
                          • Recent
                          • Tags
                          • Popular