question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

INA219 - strange readings

See original GitHub issue

Describe the bug

I have a small circuit involving INA219. At this point, during experimentation it looks the same as an example at https://github.com/dotnet/iot/blob/ca63c979bd8fd9c47acff379f09ff01ab8458abe/src/devices/Ina219/README.md.

I’m using the following code:

public class INA219Driver : IElectricalParametersMonitor, IDisposable
{
    private readonly ILogger _logger;
    private readonly Ina219 _device;
    
    private const byte i2cBus = 0x1;
    private const byte i2cAddress = 0x40;
    
    public INA219Driver(ILogger<INA219Driver> logger)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _device = new Ina219(new I2cConnectionSettings(i2cBus, i2cAddress));
        _device.Reset();
        _device.BusVoltageRange = Ina219BusVoltageRange.Range16v;
        _device.PgaSensitivity = Ina219PgaSensitivity.PlusOrMinus40mv;
        _device.SetCalibration(33574, (float)12.2e-6);
        
        _logger.LogInformation("INA219 chip is initialized");
    }
    
    public double GetCurrent()
    {
        return _device.ReadCurrent().Milliamperes;
    }

    public void Dispose()
    {
        _device?.Dispose();
    }
}

I’m reading the current measurements in a loop every second, like this:

while (true)
{
    _logger.LogInformation($"Current: {_electricalMonitor.GetCurrent()}");
    await Task.Delay(1000);
}

It’s very similar to what you’ve shown in the README.md that I linked to at the top.

I observed that my current readings are very unstable. The results change every second significantly - it varies between 7mA and 15mA, which is not true.

To make sure that my INA219 is not damaged in some way I tried to run the Python sample from https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/python-circuitpython (the “Full Example Code” from the bottom of the page). The results are much better. The current readings are stable, representing reality quite well.

Another weird thing that I observed is that the C# code returns current values even when there is no load at all! The results are then either 0mA or 799mA - it just jumps between these 2 values somewhat randomly. The Python code reports correctly 0 all the time.

Is there something wrong with I2C communication in the library?

Steps to reproduce

Provided

Expected behavior

Measurements of current should be stable.

Actual behavior

Results are not stable at all, and misrepresent reality.

Versions used

Device: RaspberryPi 4B 8GB (used both for build and running) Dotnet:

pi@rpi:~/code/python-ina219 $ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.400
 Commit:    d61950f9bf

Runtime Environment:
 OS Name:     raspbian
 OS Version:  10
 OS Platform: Linux
 RID:         linux-arm
 Base Path:   /home/pi/.dotnet/sdk/5.0.400/

Host (useful for support):
  Version: 5.0.9
  Commit:  208e377a53

.NET SDKs installed:
  5.0.400 [/home/pi/.dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.9 [/home/pi/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.9 [/home/pi/.dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Libraries (Iot.Device.Bindings and System.Device.Gpio) are in version 1.5.0.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
marcinjahncommented, Sep 22, 2022

Hi @krwq, if I recall correctly, eventually I ended up writing a small “driver” for INA219 in Python and invoking that thing from .NET. The Python implementation of INA219 was much more stable.

0reactions
DocBrown101commented, May 19, 2023

Yes, of course something like that always works, but makes the code unnecessarily complicated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird readings with INA219 - Sensors
I have a strange situation with an I2C INA219 current/volt meter module. In front of me, I have a rather scary 16.8V lipo...
Read more >
Weird readings with INA219 Current/volt sensor, Arduino ...
I have a strange situation with an I2C INA219 current/volt meter module (datasheet). In front of me, I have a rather scary 16.8V...
Read more >
INA219 Breakout - Strange Readings
This was a test using a benchtop power supply and a power resistor. The current readings are accurate (verified with an inline multimeter...
Read more >
INA219 responds with strange values over I2C - Amplifiers forum
I am trying to get voltage and current values from the device in question. From what I can tell, the I2C communication is...
Read more >
INA219 strange output - Raspberry Pi Forums
I'm wondering since the 100ohm resistor has a zero reading when I put my meter across it whether it ever really worked. I'm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found