Lightning Detector - Switching from Uno to Due - Prescaler considerations?

Some coworkers and I put togehter lightning detectors by following this guide [GitHub - klauscam/Arduino-Lightning-Detector: Detect Lightnings with Arduino] and have been tweaking the numbers, antenna lengths, etc and enjoying when storms roll through.

But, I can't help but think that a Due would be a better fit for a lightning detector.

  1. would there be a benefit to using a Due?
  2. how would I modify the code (and resistors) below (particularly prescaler-related) to accomodate the Due's lower voltage and higher analog resolution?

I have a couple Dues on hand, but have no experience using the 12 bit ADC yet.

Here's the basic code running on ATMega328

#define FASTADC 1

// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif


int data = 512;
int storage[512];

long batchStarted;
long batchEnded;
int reading;
int count;
int maximum;
int minimum;
bool toSend;

void setup() {
#if FASTADC
 // set prescale to 16
 sbi(ADCSRA,ADPS2) ;
 cbi(ADCSRA,ADPS1) ;
 cbi(ADCSRA,ADPS0) ;
#endif
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(A4,INPUT);
  Serial.println(micros());
  
  batchStarted=0;
  batchEnded=0;
  reading=0;
  count=0;
  maximum=0;
  minimum=1023;
  toSend=false;
}


void loop() {
  // put your main code here, to run repeatedly:
  reading = (analogRead(A4));
  storage[count]=reading;
  if ((!toSend)&&(count!=0)&&((reading>storage[count-1]+10)||(reading<storage[count-1]-10))){
      toSend=true;
  }
  
  count=count+1;
  if ((count == 512) && (toSend))
  {
    count=0;
    batchEnded = millis();
    sendData();
    batchStarted = millis();
    
  }
  else if (count==512){
    count=0;
    batchEnded = millis();
    //sendData();
    batchStarted = millis();
  
  }

 
}

void sendData()
{
  //Serial.print(">>>");
  //Serial.println(batchStarted);
  
  for (int i=0;i<data;i++){
    Serial.println(storage[i]);
  }
  //Serial.print("<<<");
  //Serial.println(batchEnded);
  //Serial.println("END");
  
  toSend=false;
}

besides the prescaler you need to know that a 12bit ADC goes from 0..4095
where the Arduino UNO has a range from 0..1023

There are some related variables e.g. minimum = 1023; that should be changed.

keith204:

  1. would there be a benefit to using a Due?

Due is generally faster and has 2 more bits ADC resolution. Depending on your circuit, these extra 2 bits may be useless because of noise. Noise can be filtered out by taking many samples and averaging, but that is not an option for lightning detection because of the short timescales involved.

As for the speed, the code you posted has been optimised to give fast conversion times on atmega328. This code will not run on Due, forcing you to go back to the standard Arduino functions like analogRead(). This may remove any speed advantage that the Due has.

robtillaart:
besides the prescaler you need to know that a 12bit ADC goes from 0..4095
where the Arduino UNO has a range from 0..1023

There are some related variables e.g. minimum = 1023; that should be changed.

Good point, thanks.

PaulRB:
Due is generally faster and has 2 more bits ADC resolution. Depending on your circuit, these extra 2 bits may be useless because of noise. Noise can be filtered out by taking many samples and averaging, but that is not an option for lightning detection because of the short timescales involved.

As for the speed, the code you posted has been optimised to give fast conversion times on atmega328. This code will not run on Due, forcing you to go back to the standard Arduino functions like analogRead(). This may remove any speed advantage that the Due has.

Noise is one of the things I've been tinkering with reducing. For instance, I even had a custom PCB made a while ago (for another purpose) and fit this circuit on some of the spare space on that, so the antenna is the only wire going anywhere, and it's mounted atop a Pro Mini and hooked up to a pack of AAs that run through the onboard linear regulator.

I'm not sure if that changes anything with the usefulness of the extra 2 bits, but it may.

For some reason (maybe from some of the EMON forums) I'm under the impression that modifying the prescaler on the 328 reduces the reliability of the readings, whereas running the SAM3X with just analogRead() wouldn't have the downgrade in reliability. But, I don't remember exactly what the context was, so maybe that's completely off base.

Thanks for the replies. At the very least, I'll give it a try with simply analogRead and changing the range.

Using simply analogRead() won't give you a real henancement compared to an atmega 328. Whereas ADC direct port programming will give you a real benefit in term of speed (max 1 Msps), SRAM (96 K) and even accuracy (12-bit conversions, or an equivalent of 16-bit conversions if you can oversample 256 times).