sparkfun / SparkFun_ADS1015_Arduino_Library

Library for Qwiic_Flex_Glove_Controller
https://www.sparkfun.com/
Other
5 stars 4 forks source link

Sample rates between 128Hz - 920Hz #6

Closed santaimpersonator closed 1 year ago

santaimpersonator commented 3 years ago

Customer was unable to configure and verify Qwiic ADC for sample rates between 128Hz - 920Hz, using .setSampleRate() and .getSampleRate(). Asked customer to add further details to this issue.

StefanThoresson commented 3 years ago

Arduino 1.82 SparkFun_ADS1015_Arduino_Library-2.3.0 Arduino ATmega2560

Using Library:''SparkFun_ADS1015_Arduino_Library.h'' for reading voltages of 2 inputs of ADS1015 works and gives expected values for the Data Rate setting of 1600, 2400 and 3300 Hz. 1600Hz is the default Data rate both for circuit and library as I understand from datasheet and code. When setting of 128,250,490 and 920 Hz is done I get non expected wrong levels of voltages for both inputs. In summary: 0x0000 : 128HZ //0x0000 ->NOK 0X0020 : 250HZ //0x0020 ->NOK 0X0040 : 490HZ //0x0040 ->NOK 0X0060 : 920HZ //0x0060 ->NOK 0X0080 : 1600HZ //0x0080=128->OK 0X00A0 : 2400HZ //0x00A0=160->OK 0X00C0 : 3300HZ //0x00C0=192->OK

I use a own made board used as a HF coupler to read Power in Fwd and Rev direction up to 2kW. I have attached a code based on example in library where reading of the voltages of AIN0 and AIN1 are done. Since the settings of the Data rate is not as expected I think maybe something is not set correct. Any ideas for helping on this matter would be most helpful, Schematic below with snip of ADS1015 part: image

Code:

/*
  Using the SparkFun Qwiic 12 Bit ADC - 4 Channel ADS1015

  This example shows how to output ADC values on one single-ended channel (A0).
  *at default gain setting of 1 (and 3.3V VCC), 0-2V will read 0-2047.
  *anything greater than 2V will read 2047.

  Hardware Connections and initial setup:
  Connect USB type B to Arduino Board
  Select TOOLS>>BOARD>>"Arduino MEGA"
  Select TOOLS>>PORT>> "COM 6" (note, yours may be different)
  Click upload, and watch streaming data over serial monitor at 115200.

*/

#include <SparkFun_ADS1015_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_ADS1015
#include <Wire.h>

ADS1015 adcSensor;

//-----------------------------------------------------------------------------------------------------//
void setup() {
  Wire.begin();
  Serial.begin(115200);                             //changed to 115200 instead of 9600
  if (adcSensor.begin(0x48) == true)                //0x48 is default if imepty()
                                                    //if (adcSensor.begin(0x48) == true)(GND)
                                                    //if (adcSensor.begin(0x49) == true)(VDD)
                                                    //if (adcSensor.begin(0x4A) == true)(SDA)..not recommended due to timing in datasheet..
                                                    //if (adcSensor.begin(0x4B) == true)(SCL)
  {
    Serial.println("Device found. I2C connections are good.");
  }
  else
  {
    Serial.println("Device not found. Check wiring.");
    while (1);                                       // stall out forever
  }
    adcSensor.setGain(ADS1015_CONFIG_PGA_2);
   /*
0x0000 - gain:2/3, input range:± 6.144V
0X0200 - gain:1, input range: ± 4.096V
0X0400 - gain:2, input range: ± 2.048V
0X0600 - gain:4, input range: ± 1.024V
0X0800 - gain:8, input range: ± 0.512V
0X0A00 - gain:16, input range: ± 0.256V 
    */  
    //adcSensor.setSampleRate(128);//128=0x0080->1600Hz// suggest  to keep default              
    /*
//sample rate lower than 1600 Hz gives wrong data
0x0000 : 128HZ    //0x0000 ->NOK
0X0020 : 250HZ    //0x0020 ->NOK
0X0040 : 490HZ    //0x0040 ->NOK
0X0060 : 920HZ    //0x0060 ->NOK
0X0080 : 1600HZ   //0x0080=128
0X00A0 : 2400HZ   //0x00A0=160
0X00C0 : 3300HZ   //0x00C0=192
    */
}
//-----------------------------------------------------------------------------------------------------//
void loop() {
float adc_multiplier=adcSensor.getMultiplier();
uint16_t channel_A0 = adcSensor.getSingleEnded(0);
uint16_t channel_A1 = adcSensor.getSingleEnded(1);

uint16_t adc_samplerate = adcSensor.getSampleRate();                     
uint16_t adc_gain = adcSensor.getGain();

  Serial.print("A0:");
  Serial.print(channel_A0);
  Serial.print("\t");
  Serial.print("(");
  Serial.print(channel_A0*adc_multiplier);
  Serial.print("mV)\t");

  Serial.print("A1:");
  Serial.print(channel_A1);
  Serial.print("\t");
  Serial.print("(");
  Serial.print(channel_A1*adc_multiplier);
  Serial.print("mV)\t");

  Serial.print("adc_samplerate:");
  Serial.print(adc_samplerate);
  Serial.print("\t");
  Serial.print("adc_gain:");
  Serial.println(adc_gain);

  delay(100);                                       // avoid bogging up serial monitor
}
PaulZC commented 1 year ago

Hi Stefan (@StefanThoresson ),

Thank you for reporting this issue, and apologies for the incredibly slow reply!

I was able to trace the cause of this issue. The library was using a fixed delay of 1ms between starting a conversion and reading the result. This worked when the rate was set to >= 1600Hz, but introduced errors at slower speeds...

I have corrected this in v2.3.1 - which will be released in a few minutes.

I've used the code you provided in a new example (Example7). I will tag you in the release notes as your code really helped me to trace the cause.

Thanks again, Paul