INA226 current calculation


I’m using a nice little board for monitoring a 12V solar system by Stephen Rodgers based around ESP8266 and INA226:

the code for this board is very good, but it is complex to set-up the build environment as it need Linux, and some of the dependencies are outdated resulting in need for code update. I thought to give it a try and port it to, but I don’t even know where to start.

I already use several OMG gateways for RTL and BT, and like it very much. As it already have support for the INA226, I gave it try. I’ve got it to work by changing the required I2C pins and hardware address for the chip, and I’ve set the correct shunt resistance. For reasons unknown I get wrong values for the current, voltage is fine.

The board works fine with the example code and libary by Korneliusz Jarzebski. I don’t know if the ZsensorINA226.ino is based on this or not? Also ZsensorINA226.ino seems to do the calculation of current in the code, not by the INA itself, why is this?

I’m planning to use the modified example code by Korneliusz in OMG, and let the INA do the calculation, where inputs are “shunt resistance” and “max current”, ideally configurable trough MQTT, but we will see how far I get…
I have a question; can I just add the dependencies (INA226.cpp and INA226.h) or would it be best to merge it to one file, similar to ZsensorINA226.ino?


You mainly have 2 choices:

  1. A library is available and can be called into ZsensorINA226.ino, which does not seem to be the case
  2. There is no library, in this case you should add the constants into config_INA226.h and the code into ZsensorINA226.ino

So likely number 2)

Thank you for your help Florian!

Is adding the library files (INA226.cpp and INA226.h) an option or does to project structure not allow for this? or maybe there are other good reasons for not doing this?

You could check if the last version of:

Answer your needs.
I see that it has been updated and is already what we use.

This worked.
Version 1.0.0 of the library had error on the calculation of shunt parameters. In 1.1.0 this has been corrected. I will share the config once everything is completed, and tested properly. I will now try to figure out how to enable setting the shunt value and max current from home assistant trough MQTT, I see that sliders has been use for other inputs, but for the shunt resistance value a number input would be most suited.

I have another question.
As the board I’m using have to mosfet outputs I also configured the on off actuator, which works fine, however is it possible to configure two individual outputs?

1 Like

You can choose the gpio from you mqtt payload

Thank you,

I was more thinking about the configuration of the actuator in the enviroments.ini.

Curreclty I have

Above works fine, but I want to add the other output.
If I add another actuator by adding a line:


It does not work.

That’s not possible, your second definition will override the first

Hi Florian,

Correct, I get warning during compile that -DACTUATOR_ONOFF is already defined.
Is there a way to add two “-DACTUATOR_ONOFF”?


Not without modifying the code.

Thanks again Florian!

1 Like

Hello again,

I’m now quite happy with the result and wanted to share what I did in case others may be wanting to to the same.

I’ve made some changes to the “ZsensorINA226.ino” file:

And provided some info here:

I don’t know if this is the correct way of doing this, by generating forks I mean, and making changes?

As mentioned before, I wish to try and implement configuration functionality such as measurement interval and perhaps shunt resistance and range setting, based how configuration is done in the BT gateway, I will report back if I’m successful.

Ideally, once your fork is done and cleaned, you can submit a PR to have it in the OpenMQTTGateway origin repo.
The PR will be reviewed and commented.