Comments (7)
Great that you found the bottleneck! Good luck for your project.
from ina219_we.
Hi @Albin76 , sorry for the late response. A burst read should be possible since the registers are in sequential order. I will try that but it will still need some time.
from ina219_we.
I measured now (today) Attiny85, getCurrent_mA() only. 6 measure (and 6 serial.print) is 280ms, SAMPLE_MODE_16 equal with BIT_MODE_12, so the TinyWire is slow, maybe.
from ina219_we.
Hi @BCsabaEngine , can you share your complete code? Then I could see more details. e.g. if you are you using the continuous or triggered mode, which baud rate you use for Serial.print and so on.
And what is the clock speed of your ATtiny85? And which board package do you use? E.g. ATtinyCore from spence Konde or attiny from David A. Mellis?
from ina219_we.
Hi @BCsabaEngine ,
it is the Serial.print() which is slow. I have tested the following small sketch:
void setup() {
Serial.begin(9600);
delay(1000);
unsigned long startTime = millis();
unsigned long durationTime = 0;
for(int i=0; i<100; i++){
Serial.println("Hello World, here I am!");
}
durationTime = millis()-startTime;
Serial.println(durationTime);
}
void loop() { }
The result is 2646 milliseconds which means 26.46 milliseconds per Serial.println(). 6 Serial.println() of this length plus the expected 51 ms for 6 measurements at Sample_Mode_16 would take ~210 milliseconds.
If I reduce the string to be printed to "Hello Word" it's still 1483 milliseconds for 100 Serial.println().
With a baud rate of 115200 and "Hello World" 100 Serial.println() take only 129 milliseconds, which is 1.29 milliseconds per println(),
So, if you need to print all results then I would recommend to keep the output as short as possible and the baud rate as high as possible.
My ATttiny85 was set to 8 MHz clock rate and I have applied the package from Spence Konde.
from ina219_we.
Hi @wollewald !
Thank you for fast response. I use this code to measure 6 INA219 (at final with different I2C address) current, now emulate with same I2C address.
#include <SoftwareSerial.h>
#include <TinyWireM.h>
#include "INA219_WE.h"
#define TX 3
#define RX 2
#define LED 1
#define I2C_ADDRESS 0x40
SoftwareSerial Serial(RX, TX);
INA219_WE ina219 = INA219_WE(I2C_ADDRESS);
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
TinyWireM.begin();
if (!ina219.init()) {
Serial.println(F("INA219 NOT connected"));
while (1) {}
}
else {
Serial.println(F("INA219 connected"));
}
ina219.setMeasureMode(CONTINUOUS);
ina219.setADCMode(SAMPLE_MODE_16);
ina219.setBusRange(BRNG_16);
ina219.setPGain(PG_40);
}
uint32_t lastOutput = 0;
void loop() {
uint8_t isany = false;
for (int i = 0; i < 6; i++) {
int16_t mA = (int16_t)ina219.getCurrent_mA();
if (mA < 0)
mA = mA * -1;
if (mA != 0) {
isany = true;
digitalWrite(LED, HIGH);
}
Serial.print(mA);
Serial.print(F(" "));
}
Serial.print(millis() - lastOutput); // shows 250-320 msec
Serial.println();
lastOutput = millis();
if (!isany)
digitalWrite(LED, LOW);
delay(1); // to operate LED
}
I use David A. Mellis soultion with 8Mhz (clock runs good, I measured) and serial with 9200baud. It can be a bottleneck, I will try the 115200 baud rate (now use 9200 for safety, because attiny85 talks with an atmega328. Both can use 115200, but it should be safe for me) and reduce the serial communication. I will disable RX side of serial (stopListening or set 999 to rx pin).
I have researched some improvement:
- do now use abs(ina219.getCurrent_mA()), because abs() is a macro only and it occurs calculate exporession two times, use abs with local variable only.
- use delay() to make the LED works
Thank you for your work, I will reply when I will get measure info.
ps: this is a DCC model train occupancy detector (is a train on the track?), when I measure AC current. Not a real AC (sinus wave), it just change the polarity very fast to send digital info. This is the reason of abs() use.
from ina219_we.
Problem solved. The RX input of SoftwareSerial add 250ms delay to my code. If I used stopListening, 6x measure is about 20ms. I have refactored code: output is a cstring, eliminate String object, etc.
I must use delay to slow down sketch to 10 iteration / sec. With 9600 baud. stopListening is the must.
#if not defined (__AVR_ATtiny85__)
#error "Unknown processor, can run on ATtiny85"
#endif
#include <SoftwareSerial.h>
#include <TinyWireM.h>
#include "INA219_WE.h"
#define TX 3
#define RX 99 //supress listening, earlier 2
#define LED 1
#define I2C_ADDRESS 0x40
SoftwareSerial Serial(RX, TX);
INA219_WE ina219 = INA219_WE(I2C_ADDRESS);
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.stopListening();
TinyWireM.begin();
if (!ina219.init()) {
Serial.println(F("INA219 NOT connected"));
while (1) {}
}
else {
Serial.println(F("INA219 connected"));
}
ina219.setMeasureMode(CONTINUOUS);
ina219.setADCMode(SAMPLE_MODE_16);
ina219.setBusRange(BRNG_16);
ina219.setPGain(PG_40);
}
char output [6 * (4 + 1)] = "";
uint32_t lastOutput = 0;
void loop() {
uint8_t isany = false;
output[0] = '\0';
for (int i = 0; i < 6; i++) {
int16_t mA = (int16_t)ina219.getCurrent_mA();
mA = abs(mA);
if (mA != 0) {
isany = true;
digitalWrite(LED, HIGH);
}
char value[4 + 1];
sprintf(value, "%02X", mA);
strcat (output, value);
delay(10);
}
Serial.println(output);
if (!isany)
digitalWrite(LED, LOW);
delay(10); // to operate LED
}
Thank you for your support!
from ina219_we.
Related Issues (7)
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 ina219_we.