Giter Site home page Giter Site logo

Comments (8)

RobTillaart avatar RobTillaart commented on August 10, 2024

Thanks for the issue
You should be aware that your sketch does also use time to print the value and check if the sensor is ready.

To show this effect change the baudrate of serial to 500.000 or 9600.

from ads1x15.

RobTillaart avatar RobTillaart commented on August 10, 2024

High speed will be improved by setting the I2C clock speed e.g to 400K or higher.
Best performance is in continuous mode see examples. Then read a sample by checking micros()

from ads1x15.

yanxiang-wang avatar yanxiang-wang commented on August 10, 2024

Thanks, RobTillaart. I changed the baud rate to 9600 as you suggested. At the same time, I add a timer to print the data count per second.

#include "ADS1X15.h"
#include <MsTimer2.h>
int count = 0;
void check_fq()
{
  Serial.print("count:");
  Serial.print(count);
  Serial.println("");
  count = 0;
}

// choose you sensor
// ADS1013 ADS(0x48);
// ADS1014 ADS(0x48);
ADS1015 ADS(0x48);
// ADS1113 ADS(0x48);
// ADS1114 ADS(0x48);

// ADS1115 ADS(0x48);

void setup()
{
  Serial.begin(9600);
  ADS.begin();
  ADS.setGain(1); // 6.144 volt
  ADS.setDataRate(0);
  ADS.readADC(0);
  MsTimer2::set(1000, check_fq);
  MsTimer2::start();
}

void loop()
{
  // Serial.println(ADS.getValue());
  {
    int16_t data = ADS.getValue();
    count += 1;
  }
}

And the serial output is as follows:

count:1783
count:1784
count:1784
count:1785
count:1784
count:1785
count:1784
count:1784
count:1785
count:1784

from ads1x15.

RobTillaart avatar RobTillaart commented on August 10, 2024

So you see the time the Serial printing takes affects the performance. At lower baud rates you just have less time to read the ADC. Now give it a try with 500000 baud, what is the count then?

Note: I updated your post by adding cpp to the code block to get syntax highlighting.

from ads1x15.

yanxiang-wang avatar yanxiang-wang commented on August 10, 2024

Yes, the println function will affect the speed. The strange point is that I set the data rate to 250 by ADS.setGain(1);. So, the count should be 250, am I right?
Also, I change the baud by Serial.begin(500000);, the result is the same as above:

count:1783
count:1784
count:1784
count:1784
count:1785
count:1784
count:1784
count:1784
count:1785
count:1784
count:1785
count:1784
count:1784
count:1784
count:1785
count:1784
count:1784
count:1784
count:1784

from ads1x15.

RobTillaart avatar RobTillaart commented on August 10, 2024

The strange point is that I set the data rate to 250 by ADS.setGain(1);.

The frequency is not set by setGain(), gain is sort of amplification, the voltage range.
You should use **setDataRate(1)

#include "ADS1X15.h"
#include <MsTimer2.h>

//  VARIABLES
volatile uint32_t count = 0;
ADS1015 ADS(0x48);
uint32_t lastTime = 0;


/////////////////////////////////////
void check_fq()
{
  Serial.print("count: ");
  Serial.println(count);
  count = 0;
}

void setup()
{
  //  initialize Serial
  Serial.begin(500000);

  // initialize ADS
  ADS.begin();
  ADS.setWireClock(400000);  //  higher depending on what your processor supports.

  ADS.setGain(1); // 6.144 volt
  ADS.setDataRate(2);  //  490, note this is faster than we want to sample
  ADS.setMode(0);      // continuous mode
  ADS.readADC(0);

  //  initialize timer
  MsTimer2::set(1000, check_fq);
  MsTimer2::start();
}

void loop()
{
  if (micros() - lastTime >= 4000)  // every 4 millisecond we want a sample.
  {
    lastTime = micros();
    int16_t data = ADS.getValue();
    count++;
  }

  // other code here
}

Give it a try

from ads1x15.

yanxiang-wang avatar yanxiang-wang commented on August 10, 2024

Wow, it works. Very grateful for the comments. Initially, I thought micros() was not accurate. And I need to use the interrupt-based timer for all the functions that run at a fixed period. But your example proves I am wrong—many thanks.
And the result of your code is as follows:

count: 249
count: 250
count: 250
count: 250
count: 250
count: 250
count: 250
count: 250
count: 250

from ads1x15.

RobTillaart avatar RobTillaart commented on August 10, 2024

If you do not have too many (short, non blocking) tasks at hand scheduling with micros() or even millis() works very well.
Did a project with more than 20 tasks that needed to run at least 10 to 50 times per second and that worked well.
Only thing I had adapted was the analogRead() which resulted in => https://github.com/RobTillaart/AsyncAnalog

Note there are two slightly different ways to implement this with different behavior:

if (micros() - lastTime >= threshold)
{
  lastTime = micros();
  ...
}

if (micros() - lastTime >= threshold)
{
  lastTime += threshold;
  ...
}
  • The second is faster as a call to micros() takes longer than just an addition.
  • The second gives a perfect average of calls over a longer period, while the first might drift if other tasks take too long.
  • The second might do multiple calls after each other to make up for other blocking code.

As said, the difference is subtle but can be significant when "the load" of the system is high.
So choose with care.


If this solves the issue, and no further related questions remain you may close the issue.

from ads1x15.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.