Simple python script to read battery

Hi there,

I’ve been hunting high and low, quite possibly in the wrong places, for a python script that will display the battery level on my pitop laptop 3. Unfortunately I still haven’t found one, what I have found seems to just display 255 over and over.

Any assistance on this would be greatly appreciated, or maybe point me in the right direction where i can find the necessary details, like anything really? I understand the battery is an i2c device, but that’s about it…

#!/usr/bin/env python

from smbus import SMBus
import time

def main():
    '''
    Main program function
    '''
    # Define registers values from datasheet
    GPIOA = 0x12  # Data port A
    GPIOB = 0x13  # Data port B

    i2cbus = SMBus(1)  # Create a new I2C bus
    i2caddress = 0x10  # Address of battery?

    while (True):
        porta = i2cbus.read_byte_data(i2caddress, GPIOA)  # Read the value of Port A
        print("Port A = " + str(porta)) # print the value of Port A
        portb = i2cbus.read_byte_data(i2caddress, GPIOB)  # Read the value of Port B
        print("Port B = " + str(portb)) # print the value of Port B
        time.sleep(2)

if __name__ == "__main__":
    main()

For others looking for the answer, I finally found something that works,

  1. sudo apt-get install pt-device-manager
  2. git clone https://github.com/pi-top/pi-top-Python-SDK
  3. run battery example, although I did modify the sample slightly so that it just displays the status over and over.
1 Like

@firkinfedup apologies, I only recently set up notifications for the whole forum and I missed your initial post. Glad you got it sorted though, nice work! I’m sure this will be of use for many other users in future :+1: