Hello forum peeps,
I've built a power controller using a couple INA219 and a couple MCP4725 breakout boards with an Arduino UNO, using the Adafruit libraries for both.
Reaction time is proving to be key with this project and I'd like to make sure I'm talking over the I2C bus to the sensors and DACs as fast as possible.
The MCP4725 tutorial mentions that the Adafruit library bumps up the communication speed to 400 KHz. The INA219 tutorial doesn't talk about the I2C bus speed at all. Looking at the ATmega328p data sheet, it appears that setting TWBR will affect all I2C communications. So that confirms the one library affects all I2C devices.
I guess all I really need to know, then, is whether that 400KHz can be tweaked any higher on the 328p/UNO, and if so, how to do it.
I looked at the library code and I don't really understand how the TWBR line works: TWBR = ((F_CPU / 400000L) - 16) / 2; It appears to be math, but it has a letter in a weird place e.g. 400000L. ;) I'm assuming it's syntax for a non-decimal number base. But I don't know what number base L represents. And the all-knowing Googles haven't revealed to me the answer.
Any hints, guidance or examples will be much appreciated.
Thanks!
2 x INA219 and 2 x MCP4725 communication speed
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- koyaanisqatsi
- Posts: 12
- Joined: Sat Jan 19, 2013 8:27 pm
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: 2 x INA219 and 2 x MCP4725 communication speed
The "L" suffix on a number indicates a "Long" (32 bit) integer.
Some people have successfully tweaked the bus speed up to 500KHz or more.
Make sure that any bus speed adjustments are made after "begin()" has been called on all the devices. Otherwise, the default i2c begin may reset it back to 100KHz.
Some people have successfully tweaked the bus speed up to 500KHz or more.
Make sure that any bus speed adjustments are made after "begin()" has been called on all the devices. Otherwise, the default i2c begin may reset it back to 100KHz.
- koyaanisqatsi
- Posts: 12
- Joined: Sat Jan 19, 2013 8:27 pm
Re: 2 x INA219 and 2 x MCP4725 communication speed
Ah, thank you. I see how it's set up now. I should have just stared at it a little longer. ;)
Thanks!
I suspect this implies I should set TWBR in setup(), after the begin()s? And it looks like I may get better performance dismantling the libraries and organizing their parts in my sketch directly. In the INA219 library, there is a delay(1) I can get rid of by throwing in some other code instead. I've never dismantled a library before, so this will be fun.Make sure that any bus speed adjustments are made after "begin()" has been called on all the devices
Thanks!
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: 2 x INA219 and 2 x MCP4725 communication speed
That is the most reliable way. You don't always know what goes on inside the libraries. But it sounds like you are digging in to that too.I suspect this implies I should set TWBR in setup(), after the begin()s?
Happy hacking!
- koyaanisqatsi
- Posts: 12
- Joined: Sat Jan 19, 2013 8:27 pm
Re: 2 x INA219 and 2 x MCP4725 communication speed
Well, I still have to test everything, but...
I pulled the MCP4725 library apart - it was straight forward. I'm setting TWBR in setup(), after all the begin()s (I may have to edit my default Wire library to assure TWBR stays set). Removed redundant Wire.begin()s, the lines that revert the SCL speed back to default, and other things like that. The INA219 library was a lot more involved, but I pulled it apart and condensed it down for my specific needs. Overall I think I might have improved performance by a small increment. We'll see in testing.
I'm not going to try and push too much past 400KHz, if any, because that would break Fast-Speed mode I2C (which I am now slightly familiar with). And so far it looks like High-Speed mode is not an option on the 328p. But I am at least making sure everything runs at a consistent 400KHz.
BTW: I forked the Adafruit_INA219 repo on GitHub and am preparing to submit a pull request, once I test everything. I updated it with a power function, a bunch of smaller edits and some tidying up. (UNTESTED as of this post).
https://github.com/ChestersGarage/Adafruit_INA219
Thanks for your help! I just needed a little kick-start. :)
I pulled the MCP4725 library apart - it was straight forward. I'm setting TWBR in setup(), after all the begin()s (I may have to edit my default Wire library to assure TWBR stays set). Removed redundant Wire.begin()s, the lines that revert the SCL speed back to default, and other things like that. The INA219 library was a lot more involved, but I pulled it apart and condensed it down for my specific needs. Overall I think I might have improved performance by a small increment. We'll see in testing.
I'm not going to try and push too much past 400KHz, if any, because that would break Fast-Speed mode I2C (which I am now slightly familiar with). And so far it looks like High-Speed mode is not an option on the 328p. But I am at least making sure everything runs at a consistent 400KHz.
BTW: I forked the Adafruit_INA219 repo on GitHub and am preparing to submit a pull request, once I test everything. I updated it with a power function, a bunch of smaller edits and some tidying up. (UNTESTED as of this post).
https://github.com/ChestersGarage/Adafruit_INA219
Thanks for your help! I just needed a little kick-start. :)
- koyaanisqatsi
- Posts: 12
- Joined: Sat Jan 19, 2013 8:27 pm
Re: 2 x INA219 and 2 x MCP4725 communication speed
I fully tested my fork of the INA219 library and it looks good. Pull request created.
And my project is working great too.
Thanks for your help!
And my project is working great too.
Thanks for your help!
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: 2 x INA219 and 2 x MCP4725 communication speed
Excellent! Thanks for the follow-up post.
- ariesboy571
- Posts: 14
- Joined: Sun Nov 09, 2014 5:28 pm
Re: 2 x INA219 and 2 x MCP4725 communication speed
The data sheet for the INA219 specifies the bus speed. That would be on page 6, table 7.6 (Bus Timing Diagram Definitions), item #1: SCL operating frequency. 400KHz.
Although if High-Speed mode is set, you can go as high as 2.56MHz, as per page 17, section 8.5.6.1, although you have to do a double-start to get it to work :)
Although if High-Speed mode is set, you can go as high as 2.56MHz, as per page 17, section 8.5.6.1, although you have to do a double-start to get it to work :)
Please be positive and constructive with your questions and comments.