Lightning sensor AS3935 spi issue with sdcard IN ESP32, making interfere during writing data

Hi, team,

I'm building an lightning detection project using As3935 sensor, when i write sd card , it generate discturbance to signal ,sometimes it causes to showing as lighenign event by As3935 , making this sensor unreliable.

here my code :

#include <Wire.h> 
#include "SparkFun_AS3935.h" 
#define sdp 25  int i=0,sim=1;
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01
#include "SD.h"
// SPI
SparkFun_AS3935 lightning;

void initFS() {
  if (!SD.begin(25)) {
    Serial.println("An error has occurred while mounting SD"); }
  else {  Serial.println("SD mounted successfully");
  }}
// Interrupt pin for lightning detection 
const int lightningInt =4;  
// Chip select pin 
int spiCS = 14;  // 14 in new v3 pcb

// Values for modifying the IC's settings. All of these values are set to their
// default values. 
byte noiseFloor = 7;
byte watchDogVal = 9;
byte spike = 11;
byte lightningThresh = 1; 

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector. 
byte intVal = 0; 

void setup()
{
  // When lightning is detected the interrupt pin goes HIGH.
  pinMode(lightningInt, INPUT); 
  pinMode(12, OUTPUT);
   pinMode(27, OUTPUT);
  Serial.begin(115200); 
  Serial.println("AS3935 Franklin Lightning Detector"); 
  digitalWrite(27, HIGH);
  delay(1500);
    digitalWrite(27, LOW);
  SPI.begin(); // For SPI
  if( !lightning.beginSPI(spiCS) ) { 
    Serial.println ("Lightning Detector did not start up, freezing!"); 
    while(1); 
  }
  else
    Serial.println("Schmow-ZoW, Lightning Detector Ready!\n");

  lightning.setIndoorOutdoor(INDOOR); 

  int enviVal = lightning.readIndoorOutdoor();
  Serial.print("Are we set for indoor or outdoor: ");  
  if( enviVal == INDOOR )
    Serial.println("Indoor.");  
  else if( enviVal == OUTDOOR )
    Serial.println("Outdoor.");  
  else 
    Serial.println(enviVal, BIN); 
  lightning.setNoiseLevel(noiseFloor);  

  int noiseVal = lightning.readNoiseLevel();
  Serial.print("Noise Level is set at: ");
  Serial.println(noiseVal);
  lightning.watchdogThreshold(watchDogVal); 

  int watchVal = lightning.readWatchdogThreshold();
  Serial.print("Watchdog Threshold is set to: ");
  Serial.println(watchVal);
  
  lightning.resetSettings();
  delay(2000);

  lightning.maskDisturber(false); 

  int maskVal = lightning.readMaskDisturber();
  Serial.print("Are disturbers being masked: "); 
  if (maskVal == 1)
  Serial.println("YES"); 
  else if (maskVal == 0)
  Serial.println("NO"); 
    
  lightning.spikeRejection(spike); 

  int spikeVal = lightning.readSpikeRejection();
  Serial.print("Spike Rejection is set to: ");
  Serial.println(spikeVal);

  lightning.lightningThreshold(lightningThresh); 

  uint8_t lightVal = lightning.readLightningThreshold();
  Serial.print("The number of strikes before interrupt is triggerd: "); 
  Serial.println(lightVal); 
   initFS();

   digitalWrite(spiCS, HIGH);
  digitalWrite(sdp, LOW);

  File conn = SD.open("/conn.csv");
  if (!conn) {
    Serial.println("File doens't exist");
    Serial.println("Creating file...");
    writeFile(SD, "/conn.csv", "TIME,SIM,BMP,LIGHT,BATTERY,SOLAR,RTC,SD,Mac,Imei\r\n");
  }
  else {
    Serial.println("File already exists");
  }
  conn.close();
  
  String dataString1 = "";
  dataString1 += String(sim);
  dataString1 += "\r\n";
  Serial.println(dataString1);
  appendFile(SD, "/conn.csv", dataString1.c_str());
  delay(1000);
 
  digitalWrite(sdp, HIGH);
  digitalWrite(spiCS, LOW);

}

void loop()
{
  light =0; 
  if(digitalRead(lightningInt) == HIGH){
    // Hardware has alerted us to an event, now we read the interrupt register
    // to see exactly what it is. 
    intVal = lightning.readInterruptReg();
    if(intVal == NOISE_INT){
      Serial.println("Noise."); 
        digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(12, LOW); 
    }
    else if(intVal == DISTURBER_INT){
      Serial.println("Disturber."); 
      digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
//  delay(200);    
    }
    else if(intVal == LIGHTNING_INT){
      i++;
      light=1;
      Serial.println("Lightning Strike Detected!"); 
      // Lightning! Now how far away is it? Distance estimation takes into
      // account previously seen events. 
      byte distance = lightning.distanceToStorm(); 
      Serial.print("Approximately: "); 
      Serial.print(distance); 
      Serial.println("km away!"); 
  Serial.println(i); 
      // "Lightning Energy" and I do place into quotes intentionally, is a pure
      // number that does not have any physical meaning. 
      long lightEnergy = lightning.lightningEnergy(); 
      Serial.print("Lightning Energy: "); 
      Serial.println(lightEnergy); 
  //     digitalWrite(12, HIGH); 
   //      digitalWrite(27, HIGH); // turn the LED on (HIGH is the voltage level)

  digitalWrite(spiCS, HIGH);
        digitalWrite(sdp, LOW);
  String dataString2 = "";
   dataString2 += String(light);
        dataString2 += "\r\n";
        Serial.println(dataString2);
       appendFile(SD, "/conn.csv", dataString2.c_str());
        delay(1000);
      digitalWrite(sdp, HIGH);
      digitalWrite(spiCS, LOW);
    }
  }
}

Of course, Append & write functions are simple taken from common example of sd library.

Please someone if you could , check if any pin already low in library of AS3935 , where i need to modification to my chip select possible to control. (as i understand, the CS pin only making issue as both are SPI protocol interfering somewhere due to two library.)

Thanks in advance !

Please don't yell

Please do not write in ALL CAPS - like you're yelling

sorry it was by mistake by keyboard caps lock, I was unaware of this rule of the forum.

I fixed the problem myself,

Thanks you guys!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.