I am just branching out into the world of I2C devices that don't have libraries associated with them. I am trying to use the INA228 from Texas instruments to read bus voltage and my code is coming back with either 255, 4, -1, Here is what I have so far
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
Wire.beginTransmission(0x45); //Starts the I2C bus
Wire.write(0x5); //Writes to register 5 (Bus Voltage)
Wire.requestFrom(0x45,3);//Reads register 5 contents
byte c = Wire.read(); //Put contents into a variable
Wire.endTransmission(); //Ends the I2C Bus
Serial.print(c); //Prints viariable to serial port
delay(500);
}
Here is the datasheet for INA228 Page 21 has the register list INA228 Datasheet
It looks like you are on the right track. Your best friend when working with I2C is the I2C scanner. This checks which addresses are active on the bus. You can use this to determine if it actually sees your hardware device. I expect you will not get a stable reason for several reasons but mainly I do not see the pull up resistors on your schematic. Oops sorry I do not see the schematic either. Please post the schematic, not a frizzy picture as we need to see all of the connections. Draw it as you have it wired and do not assume we can guess what is there we cannot. It is also best to keep the wires as short as reasonably possible. Your second best friend would be a logic analyzer, you can get a 24MHz one for about $5.00 from your favorite china supplier. Third is us! You could download some of the libraries for the INA228 and look at the files contained therein and see how they do it.
I see a potential problem, the pull up resistors are a bit light, try 4.7K but that is application dependent. Thanks for the fast update and the Schematic. Be sure the A0 and A1 lines are connected to VS or Ground, if left floating you can get intermittent errors. I purchased several of the logic analyzers and gave a few to friend. It was worth it for the look on there faces. I keep a spare but so far I have not needed it.
I did as you recommended with changing the resistors to 4.7K and the A0/A1tied to ground. It stabilized the reads to only be outputting "4" at least this is progress to stability!
There are functions to read from a I2C sensors and there are functions to write to a I2C sensor. You mix those functions. That is not allowed.
See my "alternative explanation".
Show us your new sketch.