NHBSystems / NHB_AD7794

Library for using the Analog Devices AD7794 6ch 24 bit ADC
MIT License
5 stars 3 forks source link

Update rate #3

Closed AndreasLamparter closed 2 years ago

AndreasLamparter commented 2 years ago

Hi,

i reached for a single channel only a update range about 178Hz. The update range is set to 470. Any ideas ?

Best regards Andreas

Device: Adafruit Feather nRF52840 Sense

Code:

#include <Arduino.h>
#include "NHB_AD7794.h"

#define AD7794_CS 10
#define EX_EN_PIN 9

AD7794 adc(AD7794_CS, 4000000, 2.50);

void setup()
{
  pinMode(EX_EN_PIN, OUTPUT);
  digitalWrite(EX_EN_PIN, LOW);

  adc.begin();

  adc.setUpdateRate(470); 

  adc.setBipolar(0, true);
  adc.setGain(0, 128);
  adc.setEnabled(0, true);

  for (int i = 1; i < 6; i++)
  {
    adc.setEnabled(i, false);
  }
}

void loop()
{
  uint32_t count = 0;
  uint32_t stop = millis() + 1000;
  while (millis() < stop)
  {
    adc.getReadingRaw(0);
    ++count;
  }

  Serial.print(count);
  Serial.println("Hz");
}
Result:
178Hz
177Hz
178Hz
178Hz
178Hz
177Hz
178Hz
178Hz
177Hz
178Hz
178Hz
177Hz
178Hz
178Hz
178Hz
177Hz
178Hz
178Hz
178Hz
jjuliano77 commented 2 years ago

Hi Andreas,

The 470 hz update rate comes directly from the data sheet specs, but I have not tried to run that fast (at least not in a long time). I will look into the library code to see if something is limiting the rate. I suspect it may be limited because the ADC is operated in single conversion mode, which makes sharing the SPI bus easier. It's also better for low power applications because the ADC goes into standby between readings.

As soon as I get a moment to sit down with the hardware I'll see if I can get the full rate by forcing continuous conversion mode.

AndreasLamparter commented 2 years ago

Hi,

if i use two channels, the update rate is reduced by half. With 6 channels the speed is at 30Hz.

jjuliano77 commented 2 years ago

That is the expected behavior. The channels are multiplexed, so your maximum data rate is divided up over the total number of channels. There is also a settling time for each time the channel is changed. The settling time is 1/(2 x f), where f is the selected output data rate, according to the datasheet. Because of the settling time, the actual output rate when using multiple channels will always be less than [ rate / channel count ].

jjuliano77 commented 2 years ago

I just committed an update to the library that includes an example for running in continuous mode at 470Hz. You can only do it on one channel at a time however. That is just the way the chip works I'm afraid. It also will not play nice with other devices on the same SPI bus. The update is on GitHub now. It is release 1.2.0 and should propagate out to the Arduino Library Manager soon.

I also add a method called setChopEnabled(bool) to disable the "chop" feature. Doing so will get you significant increase in the effective sampling rate when using the default single conversion mode. I was getting around 280Hz for a single channel, and about 45Hz for all 6 channels in my testing. The downside is that the datasheet warns that there may be some long term drift in the output when running with chop disabled. I added the line //adc.setChopEnabled(false); to the Read_6Ch example so it can just be uncommented to try it out.

I hope this is helpful to anyone needs a little more speed out of this device.

AndreasLamparter commented 2 years ago

I've tested it and it works. Thank you for the fast help!