Skip to content

Conversation

@tonhuisman
Copy link
Contributor

@tonhuisman tonhuisman commented Dec 26, 2025

Resolves #2622
Resolves #2602
Forum request: https://www.letscontrolit.com/forum/viewtopic.php?t=10746
Forum request: https://www.letscontrolit.com/forum/viewtopic.php?p=74843

Features:

  • [P132] Add support for:
    • INA219 (Is already supported in P027, also included in library)
    • INA226
    • INA228 (no hardware available)
    • INA230 (no hardware available)
    • INA231 (often detected as INA226, but has lower specs)
    • INA260 (built-in shunt resistor)
  • By default only enabled for ESP32, but can be enabled in an ESP8266 Custom build

TODO:

  • Update documentation
  • Testing!
    • INA3221
    • INA219
    • INA226
    • INA228
    • INA230 ?
    • INA231 (is detected as INA226)
    • INA260 (waiting for hardware to arrive)
  • Refactor 'Max. current' and 'Shunt resistor' settings for higher resolution and better match.

@Gonzol999
Copy link

Great job, thank you very much, I'm interested in INA228 support myself. Will there be binaries available for download for the ESP32?

@tonhuisman
Copy link
Contributor Author

tonhuisman commented Dec 28, 2025

Will there be binaries available for download for the ESP32?

Assuming you mean ESP32-Classic, here are the builds that this plugin is included in:

ESP_Easy_mega_20251228_energy_ESP32_4M316k-2.zip for ESP32 with 4MB Flash
ESP_Easy_mega_20251228_max_ESP32_16M8M-2.zip for ESP32 with 16MB Flash

Both include the OTA .bin and the Serial-flashable .factory.bin (address 0x0).

Edit: Re-uploaded the zip files (appended -2) with the adjusted Conversion rate settings I pushed a few minutes ago.

NB: Possibly the INA228 Power setting might not return the expected result, as the code shows a TODO there... 🤔

@Gonzol999
Copy link

Thanks for the binaries. I loaded the 4M316k.factory module, and it boots up, but I doubt it will work correctly. There's no option to select the shunt value. Without checking the documentation, I'm not entirely sure if all the INA3221/INA226/INA228/INA231/INA260 models have the same registers. At first glance, the INA3221 is a 3-channel model, while the others aren't. So how would this work without selecting the INA model in the plugin?

@tonhuisman
Copy link
Contributor Author

tonhuisman commented Dec 28, 2025

So how would this work without selecting the INA model in the plugin?

Not sure what/how you installed the binary, but the P132 plugin ("Energy (DC) - INA3221/INA226/INA228/INA231/INA260") should show an INA type selector, where you can choose what INA chip is used on the board, and ditto for the Shunt resistor value (that selector was already available in the original version of the plugin, and now has 2 extra values added).
Can you show a screenshot of the device configuration for the plugin? Does it show the name of the build near the bottom-right end of the web page? It should be of today, 20251228, as I made a fresh build (also reflected in the name of the .zip files).

@Gonzol999
Copy link

Config ESP32 Devices INA228_1 Devices INA228_2

@tonhuisman
Copy link
Contributor Author

Ah, this is a phenomenon we sometimes see when some other firmware (ESPHome, Espruna, Tasmota?) has been used on the esp, and the flash hasn't been wiped before installing ESPEasy. It tries to re-use an existing file system, but if that's too small it fails in writing the configuration file(s), causing weird behavior and/or crashed like you have shown.
Solution: First wipe the entire flash, you can do that using the web-flasher at https://td-er.nl/ESPEasy/latest, flash the last Energy build on it, connect to WiFi as usual and then install the above build via OTA (Tools/Firmware update) using the .bin file (not the .factory.bin file). Your current configuration is crooked anyway, so starting from scratch is the best way to solve that.

@Gonzol999
Copy link

Thank you again for your involvement and willingness to help. It's possible this is a hardware issue with my ESP32; none of the methods I know of for clearing the memory have yielded positive results. Since I only have one ESP32 board (I have many, but they're ESP8266s), I've ordered another ESP32, but it will probably take some time for them to arrive.

@tonhuisman
Copy link
Contributor Author

I have many, but they're ESP8266s

I've built you a Custom Energy build with this feature enabled for ESP8266 with 4MB Flash:
ESP_Easy_mega_20251229_energy_ESP8266_4M1M.zip
(The .bin.gz is a gzipped version of the .bin file, for faster upload via OTA, allow about 20 seconds after the OTA upload has finished so the data is unpacked in the correct flash location)

@Gonzol999
Copy link

Another huge thank you. The ESP8266 binary is better. Almost everything seems to work, but there's a problem with reading data. Admittedly, I've been experimenting with the INA226 for a while now. I have a question: instead of selecting the internal resistor value, there should be an option to select a shunt, e.g., 30A (75mV), etc., because these built-in resistors are intended for small pads.

@tonhuisman
Copy link
Contributor Author

tonhuisman commented Dec 29, 2025

Selecting a different shunt resistor is mostly a wiring issue, finding the right shunt value (for 30A the 2 mOhm setting should be just right for an INA226), find a 0.002 Ohm resistor that can handle the power dissipation, remove the existing shunt from the board and wire the new shunt to the board.
Currently 0.002 Ohm is the smallest shunt supported, (and previously 0.005 Ohm was the smallest, allowing ca. 16A), but nobody ever asked for smaller or more flexible configuration (the plugin was available in the ESPEasy PluginPlayground before we added it to ESPEasy), so until now there was no reason to change that 🤔

@Gonzol999
Copy link

I don't care about such currents, but this connector allows you to select a current in the range [1-1022 A], but there's still no option to change the shunt, and that's where I see a lack of consistency. A while ago, a similar build for the ESP8266 and INA226 was posted on the forum, with the option to change the shunt to an external one. Again, I don't care about that.

@tonhuisman
Copy link
Contributor Author

Those shunts are for 75mV. Some of the INA chips, like INA219 and INA228, can handle higher shunt voltages, up to 320mV for INA219, and 160mV (fixed) for INA228. But that would still work fine, would just be a resolution-related issue.

@tonhuisman
Copy link
Contributor Author

allows you to select a current in the range [1-1022 A], but there's still no option to change the shunt,

Well, I can limit that max. value to, say, 30A, I just added the max current setting as it is required for the new library I'm using.

@Gonzol999
Copy link

These currents don't bother me at all, so they can stay as they are.

@tonhuisman
Copy link
Contributor Author

I've changed the resolution for the Shunt setting, and added more options for high-current shunts, to better match those to the Max. current setting.
@Gonzol999 Here's a Custom Energy build for ESP8266 for testing:
ESP_Easy_mega_20251230_energy_ESP8266_4M1M.zip

@Gonzol999
Copy link

I don't have a shunt at home because I don't measure such high currents. I downloaded your binary; in my opinion, it offers much more options. I'll get myself a shunt, let's say the smallest one, and run some tests. Thanks again.

@tonhuisman
Copy link
Contributor Author

I've made a few minor adjustments, and built a fresh 2026 build: 😉
ESP_Easy_mega_20260101_energy_ESP8266_4M1M.zip

@Gonzol999
Copy link

I have a 30A 75mV shunt and an INA226. Either I'm configuring something incorrectly or there's something wrong with the binary. Voltage is OK, but current measurement is a gamble in my opinion.

@TD-er
Copy link
Member

TD-er commented Jan 6, 2026

How is it connected?
Not sure how strict that chip is with its voltage levels, so maybe check if there is a voltage measurable between GND of the ESP and either pole of the shunt and especially if there is some voltage negative compared to GND of the ESP.

@tonhuisman
Copy link
Contributor Author

If the INA226 is installed on a board, any on-board/other shunt resistors have to be disconnected (or removed), and only the external shunt should be connected.

@TD-er
Copy link
Member

TD-er commented Jan 6, 2026

If the INA226 is installed on a board, any on-board/other shunt resistors have to be disconnected (or removed), and only the external shunt should be connected.

Still, even if there is an existing shunt (in parallel), then the reported current should show a correlation to the actual current. Only lower.

@Gonzol999
Copy link

It's connected correctly. The original shunt resistor from the board has been removed. I loaded a binary I found on the forum that supports the INA226, and it works fine.

@TD-er
Copy link
Member

TD-er commented Jan 6, 2026

Do you have a link to that binary?

@Gonzol999
Copy link

Gonzol999 commented Jan 6, 2026

#2622
in this thread there is a link from which I downloaded the binary

This is an old link from November 18, 2021. Your request for the source code is also in this thread.

@tonhuisman
Copy link
Contributor Author

I have used the Zanduino INA library for adding support for the extra INAxxx models, though I haven't compared notes with the wollewald (INA226 explicit) library, as it is quite different.

What did you use for Averaging samples setting? As leaving that at 1 might turn out to be rather unstable. Also the Conversion rate settings may benefit the stability when allowing some more time for the conversion.

@Gonzol999
Copy link

Averaging samples: I tested probably all of them from this list, the closest current readings gave a value of 16. Conversion rate Current: I also tested all values ​​from the selection list, here the closest current consumption readings give a value of 1. Despite all this, the current measurement is unstable and significantly different from the actual one.

@TD-er
Copy link
Member

TD-er commented Jan 6, 2026

Where do you have the shunt? At the GND level or at the + side?
What voltage do you use?
Do you live in a 60Hz net frequency area or 50 Hz?
How long are the wires between the INA226 and the shunt?

In the datasheet on page 14, there is some suggestion of filtering.
image

I'm discussing the code with Ton (via Signal) right now and we already found a few things that can be improved in the code.
So I guess Ton is already either writing this down for tomorrow or busy hacking the code :)

@TD-er
Copy link
Member

TD-er commented Jan 6, 2026

Also what current are you trying to measure?
Is it somewhat in the middle of the range of the shunt?
Or are you using a 30A shunt and try to measure 10 mA? (or some extreme like that)

I noticed a few places in the code of the library where quite a bit of resolution is lost due to careless calculations or completely illogical stripping of bits.

@Gonzol999
Copy link

Gonzol999 commented Jan 7, 2026

ina226-schematic

I've connected the shunt according to the attached diagram. The wires from the shunt to the INA are a few cm long (less than 10 cm). The voltage I'm trying to measure is 28 V, and these are external batteries connected to the UPS. The current from the mains should be 0 A under normal operation, but it can exceed 20 A under load. I live in Poland on a 240V 50 Hz grid.

The measurement results are very unstable, but I can probably check them with a filtering system at night. I'm just wondering why the binary I mentioned yesterday is practically OK.

@tonhuisman
Copy link
Contributor Author

I can't reach the home-garden.org site from my location (time-out), not sure if it still is available, or blocked for some reason, so there is no source-code available for comparison.
As TD-er said, there is room for improvement, so I'm working on that.

@Gonzol999
Copy link

Ok. I'll check with the filtering system. Another question: Has anyone else tested the INA226 with your input?

@tonhuisman
Copy link
Contributor Author

I tested with the on-board shunt and a quite low current. Haven't payed much attention to stable output, other than it stayed constant, as was to be expected, and changed when I changed the 'load'.

@TD-er
Copy link
Member

TD-er commented Jan 7, 2026

I'm personally not a fan of measuring current on the 'high side', but it should work as the voltage doesn't exceed the max. input of the INA226.
As a simple form of filtering, you could try to twist the wires from the shunt to the INA226 chip.
A UPS may add some magnetic interference to the signal and with twisted wires you can prevent that.

There are lots of reasons why the other binary file yielded more stable results.
For example Ton's code does not (yet) wait for a read cycle to be finished. Just assumes it is.
Also the load from the UPS on the batteries can be fluctuating a lot within 20 msec (1 cycle of 50 Hz), meaning the sensor should perform a number of measurements over a multiple of 10 msec (or 20) or maybe even a continuous reading.
Not sure what is possible here as I've not read the entire datasheet.
I know there are several 'integration time' settings and I've also seen some averaging parameters.
Maybe it best to configure the sensor to take some 1 msec readings and then average over 500 or 1000 samples. This would then result in more 'stable' readings, but on the other hand it will also result in some delay as every filtering introduces a delay in the response to changes.
Also it may require some tweaking as the shunt and voltage are read sequentially by the same ADC. So longer integration times can result in completely useless power calculations if the current fluctates a lot during a 50Hz cycle.

Apart from this, there are still some parts of the code that can be optimized as quite a lot of resolution can be lost how it is done right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add INA226 Add INA260

3 participants