1

I have a Raspberry Pi Zero WH and I'm trying to read the voltage and capacity values in Python from my UPS-Lite v1.2 battery, but they're reporting zeros despite the CLI showing differently.

I've read through these resources:

# Environment

$ uname -a
Linux raspberrypi 5.4.79+ #1373 Mon Nov 23 13:18:15 GMT 2020 armv6l GNU/Linux
$ ls -l $(which python)
lrwxrwxrwx 1 root root 7 Dec 14 19:08 /usr/bin/python -> python3
$ python --version
Python 3.7.3

# I2C Troubleshooting

$ sudo i2cdetect -l
i2c-1   i2c         bcm2835 (i2c@7e804000)              I2C adapter
$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- 36 -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --     
$ sudo i2cget -y 1 0x36 2     # 2 is voltage address
0xd2
$ sudo i2cget -y 1 0x36 4     # 4 is capacity address
0x66

My Python code looks like this.

#!/usr/bin/python
# -*- coding:utf-8 -*-
import smbus
import struct
import logging

logging.basicConfig(level=logging.DEBUG)

ADDRESS = 0x36

def readVoltage(bus):
    try:
        read = bus.read_word_data(ADDRESS, 2)
        logging.info("voltage read: " + str(read))
        swapped = struct.unpack("<H", struct.pack(">H", read))[0]
        logging.info("voltage swapped: " + str(swapped))
        voltage = swapped * 1.25 / 1000 / 16
        logging.info("voltage return: " + str(voltage))
        return voltage
    except:
        return 0.0

def readCapacity(bus):
    try:
        read = bus.read_word_data(ADDRESS, 4)
        logging.info("capacity read: " + str(read))
        swapped = struct.unpack("<H", struct.pack(">H", read))[0]
        logging.info("capacity swapped: " + str(swapped))
        capacity = swapped / 256
        logging.info("capacity return: " + str(capacity))
        return capacity
    except:
        return 0.0

def QuickStart(bus):
    address = 0x36
    bus.write_word_data(address, 0x06, 0x4000)

def PowerOnReset(bus):
    address = 0x36
    bus.write_word_data(address, 0xfe, 0x0054)

bus = smbus.SMBus(1)
PowerOnReset(bus)
QuickStart(bus)

print("Voltage: %5.2fV" % readVoltage(bus))
print("Capacity: %5i%%" % readCapacity(bus))
$ sudo ./test.py
INFO:root:voltage read: 0
INFO:root:voltage swapped: 0
INFO:root:voltage return: 0.0
Voltage:  0.00V
INFO:root:capacity read: 0
INFO:root:capacity swapped: 0
INFO:root:capacity return: 0.0
Capacity:     0%

I've tried python3-smbus from apt-get and SMBus2 from pip3. Both report zero values.

Thanks for the help!!

1
  • Should ADDRESS be defined as a global?
    – user115418
    Dec 15 '20 at 21:12
1

I've figured it out! This is confusing and I do not know why this caused a problem, but the values report properly in python if I don't set PowerOnReset. Comment out that function call and they report properly:

$ sudo ./test.py
INFO:root:voltage read: 16594
INFO:root:voltage swapped: 53824
INFO:root:voltage return: 4.205
Voltage:  4.21V
INFO:root:capacity read: 26726
INFO:root:capacity swapped: 26216
INFO:root:capacity return: 102.40625
Capacity:   102%
1
  • Please mark the answer as the accepted one with a click on the tick on its left side. That prevents your Question from being shown as an unsolved Post to the community and saves them/us a lot of work.
    – Ingo
    Dec 20 '20 at 11:12
0

i2cget is reading one byte.

read_word_data is reading two bytes.

The read_word_data is probably illegal for the device and fails.

Try read_byte_data to get one byte as in the i2cget.

1
  • Interesting observation! Though I did take the code right from the user manual. It works now that I stopped setting the PowerOnReset flag (writing 0x54 to 0xfe) I have no idea why though 🤔 Dec 15 '20 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.