dotnet / dotnet/iot

INA219 - strange readings

Open
#1,659 10 comments 0 reactions 0 assignees View on GitHub
bug Priority:3
Dominant language
C#
Stars
2.4k
Forks
630
Avg merge
11d 3h
Merged PRs (30d)
2

Description

**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:

```csharp
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 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:

```csharp
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.