Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

AS3935 SPI interface with Feather M0 LoRa
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AS3935 SPI interface with Feather M0 LoRa

by Korteweg87 on Thu Feb 04, 2021 1:05 pm

Hello everyone!

I'm trying to use a "Thunder Click" sensor (AS3935):

https://it.rs-online.com/web/p/strumenti-di-sviluppo-per-sensori/8828966/

using the SPI interface on a Feather M0 LoRa. I'm using this library:

https://github.com/raivisr/AS3935-Arduino-Library

With the example sketch included in this library, i made it work on Arduino UNO, but not on my Feather M0 LoRa.
These are the connections i made, referring to this pinout, purple: https://cdn-learn.adafruit.com/assets/assets/000/046/255/large1024/feather_Feather_M0_RFM95_v1.2-1.png?1504886606

ThunderClick --> Feather M0

CS --> 10
IRQ --> 1 (TX1, but )
SDI --> MIS
SDO --> MOS
SCK --> SCK

And obviously, GND and 3v3.
I tought it could be an issue of the clock divider in the setup of the SPI communication, and i divided by 64 the max frequency provided by the MCU, so it could run at 750kHz (48MHz/64) because the Thunder Click works at a maximum clock speed for SPI of 2MHz (we could use also 32 as divider).
Still... not working on Feather M0. Here i post the code i loaded on my Feather, but its a little edit of the example sketch in the library:

Code: Select all | TOGGLE FULL SIZE

/*
Collegamento con la Feather M0 LoRa

3v3 --> 3v3
GND --> GND
SCK --> SCK
SDI --> MIS
SDO --> MOS
CS  --> 10,
IRQ --> TX1 o pin 1

*/

#include <SPI.h>
#include <AS3935.h>

void printAS3935Registers();
byte SPItransfer(byte sendByte);
void AS3935Irq();
volatile int AS3935IrqTriggered;

//primo argomento: funzione di trasferimento SPI
//secondo argomento: pin usato per CS
//terzo argomento: pin usato per IRQ
//AS3935 AS3935(SPItransfer,SS,2);
AS3935 AS3935(SPItransfer,10,1);

void setup()
{
  Serial.begin(115200);

  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
  // DIV64 --> SPI clock a 750kHz
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  // and chip is MSB first
  SPI.setBitOrder(MSBFIRST);
  // reset all internal register values to defaults
  AS3935.reset();
 
  // and run calibration
  // if lightning detector can not tune tank circuit to required tolerance,
  // calibration function will return false
  while (!Serial){}
  if(!AS3935.calibrate()){
    Serial.println("Calibrazione fallita");
  }
  else{
    Serial.println("Calibrazione riuscita");
  }

  // 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.setIndoors();
  // turn on indication of distrubers, once you have AS3935 all tuned, you can turn those off with disableDisturbers()
  AS3935.enableDisturbers();
  printAS3935Registers();
  AS3935IrqTriggered = 0;

  // Aggiunto qui digitalPinToInterrupt, sul pin 1
  attachInterrupt(digitalPinToInterrupt(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);
}

void loop()
{
 
  if(AS3935IrqTriggered)
  {
    // 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();
    Serial.print("Noise Level: ");
    Serial.println(AS3935.getNoiseFloor());
    // 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");
    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 < 63 && strokeDistance > 1)
      {
        Serial.print("Lightning detected ");
        Serial.print(strokeDistance,DEC);
        Serial.println(" kilometers away.");
      }
    }

  }
//    printAS3935Registers();
    Serial.println("Waiting...");
    delay(3000);
}

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

byte SPItransfer(byte sendByte)
{
  return SPI.transfer(sendByte);
}

void AS3935Irq()
{
  AS3935IrqTriggered = 1;
}


Hope someone could give me a hint, i'm really struggling making this SPI work with this sensor >_<

Thank you everyone!

Korteweg87
 
Posts: 3
Joined: Fri Jun 21, 2019 8:43 am

Re: AS3935 SPI interface with Feather M0 LoRa

by adafruit_support_carter on Fri Feb 05, 2021 5:15 pm

The LoRa radio module also uses the SPI bus. So you'll need to make sure to deal with its chip select also, even if you aren't using it.

adafruit_support_carter
 
Posts: 20354
Joined: Tue Nov 29, 2016 2:45 pm

Re: AS3935 SPI interface with Feather M0 LoRa

by Korteweg87 on Fri Feb 05, 2021 5:30 pm

adafruit_support_carter wrote:The LoRa radio module also uses the SPI bus. So you'll need to make sure to deal with its chip select also, even if you aren't using it.


Thank you for your kind answer, I'll try to figure it out trying to select a different slave and i'll post the results if it could be helpful for other people aswell.

Korteweg87
 
Posts: 3
Joined: Fri Jun 21, 2019 8:43 am

Re: AS3935 SPI interface with Feather M0 LoRa

by Korteweg87 on Thu Feb 11, 2021 6:12 am

Thanks to the hint, I made it work!

The LoRa radio module uses pin 8 as SS, as the official pinout states, so, connecting different slaves in a regular mode (sharing MISO, MOSI, SCK, but different pins for SS), is possible enabling the slave and disabling (for the acquisition) the LoRa Radio Module.

So, to disable the LoRa Module and enable the other slave, I simply had a RISING edge on pin 8, and a FALLING on the other slave's SS (because they are selected with falling edge, so transition 3v3 --> gnd). After the acquisition, I just inverted the two edges, giving a FALLING on pin 8 and a RISING on the SS of the Thunder Click.

Hope this could be helpful for other people aswell!

Korteweg87
 
Posts: 3
Joined: Fri Jun 21, 2019 8:43 am

Re: AS3935 SPI interface with Feather M0 LoRa

by owen_Job on Mon May 03, 2021 11:42 am

Hi, I am currently experiencing the same problem. I see that you found a solution, but a lot of that doesn't make sense to me^, do you mind sharing how you did that with code ? It would be highly appreciated :)

owen_Job
 
Posts: 1
Joined: Mon May 03, 2021 11:40 am

Please be positive and constructive with your questions and comments.