Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
INA219 with PI4
#1
hello
I'm having some problem with this guide
https://www.rototron.info/raspberry-pi-ina219-tutorial/

while running this code
from ina219 import INA219, DeviceRangeError
from time import sleep

SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 2.0
ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)
ina.configure(ina.RANGE_16V)

def read_ina219():
    try:
        print('Bus Voltage: {0:0.2f}V'.format(ina.voltage()))
        print('Bus Current: {0:0.2f}mA'.format(ina.current()))
        print('Power: {0:0.2f}mW'.format(ina.power()))
        print('Shunt Voltage: {0:0.2f}mV\n'.format(ina.shunt_voltage()))
    except DeviceRangeError as e:
        # Current out of device range with specified shunt resister
        print(e)

while 1:
    read_ina219()
    sleep(1)
I can see the INA219 device , but I get error
line 220, in <module>
ina.configure(ina.RANGE_16V)
File "/usr/local/lib/python3.7/dist-packages/ina219.py", line 165, in configure
self._max_expected_amps)
File "/usr/local/lib/python3.7/dist-packages/ina219.py", line 283, in _calibrate
self._calibration_register(calibration)
File "/usr/local/lib/python3.7/dist-packages/ina219.py", line 329, in _calibration_register
self.__write_register(self.__REG_CALIBRATION, register_value)
File "/usr/local/lib/python3.7/dist-packages/ina219.py", line 361, in __write_register
self._i2c.writeList(register, register_bytes)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO/I2C.py", line 127, in writeList
self._bus.write_i2c_block_data(self._address, register, data)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/smbus.py", line 294, in write_i2c_block_data
self._device.write(data)
OSError: [Errno 121] Remote I/O error


any ideea why?
what is wrong?
Reply
#2
I guess it's a permission error.

# shell, not Python
usermod -G i2c -a USERNAME
# then reboot or open a new login shell with:
# su - USERNAME
You can check your memberships in groups with the command id.

After this action, the user should be able to write to the i2c bus.
If you try next time to access as normal user to a serial port, you'll get again an error.
In this case, you should add the user to the group dialout.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I have only 1 user , default PI
I have try to run the pythin code using sudo
still gettting the same error
as I know I have all permissions - am I wrong?
this is what I get before I run the command you told me
pi@raspberrypi:~ $ id
uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),105(input),109(netdev),997(gpio),998(i2c),999(spi)


if I'm wrong - please tell me what to do , and I want to use it in the PI default user

Thanks ,
Reply
#4
Looks good. Now you should have the permission. The use of sudo is not required, but you have tested it with sudo and it did not work, which gives the hint, that the file/device does not exist.

I guess the i2c module is not loaded. You have to activate i2c on your Raspberry Pi.

Run raspberry-config: https://www.raspberrypi.org/documentatio...-config.md
Then activate i2c in "Interfacing Options"

After activation you could install i2c-tools, if they aren't installed.
Usually it should be already preinstalled.
If not:
apt-get update && apt-get install i2c-tools
Then run:
i2cdetect -y 1
This will show you all connected i2c devices on bus number 1. Bus number 0 is a different one, which is connected internally somewhere.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
everything is OK
pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
as you can see , the PI is "seeing" the INA219
but the code isn't working
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020