Comments (8)
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.
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.
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.
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.
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.
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.
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.
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)
- requestADC(pin); should have 0 as default, HOT 1
- ADS1115 reading HOT 14
- ADS1115 on RP2040 Wire, 0x4B is busy HOT 14
- Arduino Micro and ADS1115 - 860SPS (Only on 1 channel ?) HOT 62
- Error during compiling HOT 5
- ESP32 work on Espressif 1.04 but No on Espressif 2,07 HOT 4
- Synchronous mode very slow HOT 13
- [Query] Example for continuously reading all four channels at max sps? HOT 2
- Differential continuous mode HOT 12
- Cannot change address HOT 11
- ADS1115 value from one channel is being copied to the other HOT 6
- The use of Wire1 is not allowed for ESP32 or ESP8266 HOT 3
- Alternate I2C/TWI library or library abstraction layer HOT 8
- [question] About using ALERT RDY in ADS_continuous_differential.ino example. HOT 8
- getMaxVoltage() does not handle ADS1113 HOT 6
- interrupt driven reads HOT 6
- Differential input and gain for maximum precision. HOT 4
- wire speed HOT 22
- 10-12 Channel Continuous with interrupts. HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ads1x15.