chrisb2/pyb_ina219

Measurements not correct from INA219 on a RPi Pico

mebj opened this issue · 9 comments

mebj commented

During test of a custom made data logger I have found that values generated by the Pico is more than 6 times lower than when measured by a multimeter and also from an Arduino Nano using the Adafruit library.
Same INA219 and simple test circuit is used for both the Pico and the Nano tests.

I am waiting for another Pico but the unit used for testing has worked well so far so I doubt the problem is there.

Vscode shows 2 errors for the ina219.py module. One error at line 258 "raise DeviceRangeError(self.__GAIN_VOLTS[self._gain])" and another at line 409 "if logging._level == logging.DEBUG:" but I am not skilled enough to fix these.

First error shows "Argument of type "int | ... | Unknown | None" cannot be assigned to parameter "__s" of type "slice" in function "getitem"
Type "int | ... | Unknown | None" cannot be assigned to type "slice"
"int" is incompatible with "slice"

Second error is reported as "(function) _level: Unknown
"_level" is not a known member of module "logging"

A text file with some more info is attached.
Ina219.txt

So just to confirm despite the 'errors' in vscode the code does execute?

As a first step can you attach a sketch of your circuit? One thing to confirm is that you have a common ground between the load under test and the mcu ground.

Do you have specific plug-ins installed in vscode?

Regards,
Chris

mebj commented

Hi Chris,

Many thanks for a quick answer.

The code does not execute unless the second error is fixed. I made a wild guess and changed "if logging._level == logging.DEBUG:" to "if logging._level_dict == logging.DEBUG:" and then the code executes.

The entire setup is quite complex because a 20x4 lcd, a DS3231 clock and a sd-card reader/writer module are attached. The INA219 part is simple though because it is the only component attached to I2C1 SDA and I2C1 SDB at GP2 and GP3, other I2C components are on I2C0 at GP16 and GP17 and the sd- card is attached to SPI1 at GP10, 11, 12 and 13.

The INA219 has a common ground with the tested circuit. Without that the measurements were all over the scale but are now well aligned as you can see from the recorded values.

I use Pico-W-Go and Pylance extensions for vscode (and Python of course)

There is another INA219 library (https://github.com/robert-hh/INA219) used by this example (https://scruss.com/blog/2022/08/08/ina219-current-sensor-and-micropython/) and when running this exactly the same erroneous result is obtained.

So there seems to be something very specific with the Pico. I will test another unit once I get hold of one but do not expect any change since the current one is working for other things I have tested.

Best regards.

Mats

Thanks for all the info, I have a pico and vscode, so can look into this, but I am not going to get much time until 7th April.

mebj commented

That is OK, will be waiting. As I do not know the code or vscode debugging it is probably beyond my capabilities to trace the calculation of the shunt voltage but I can take a look at it while waiting.

mebj commented

Hi Chris,
I have received new hardware and will start testing, please do not put any work into this until my tests are finished. Preliminary it looks like the problem could be caused by my setup.
Regards
Mats

mebj commented

Testing with new components shows that one of them did not work correctly. The INA219 is now working correctly but will not run until I modified the code as mentioned earlier. This modification is most likely not correct though.

The unfixed problem shown by vscode does not seem to affect the result.

Good to hear, will investigate code issue next weekend.

Chris

Coincidently this PR should fix the issue on L409.

In vscode I am not currently seeing the error at L258. I have vscode v1.77.1, Pico-W-Go v3.0.1, Pylance v2023.4.10 and Python v2023.6.0.

mebj commented

My versions are exactly the same:
vscode 1.77.1
Pico-W-Go 3.0.1
Pylance 2023.04.10
Python 2023.6.0
My Windows is a fully updated Win10.

I enclose a screen copy of the information for the L258 problem. This does not stop the program execution so it cannot be anything significant (if anything at all). Still odd that we do not get the same message even though program versions are identical.

Many thanks for your assistance.

Mats

INA219