Read Voltage from Brickpi?

How can I read the voltage of the battery that is connected to the BrickPi using python? (I have the new model)

I’m using LiPo batteries and I ruined one, because I drained it too far. I want to program an emergency shutdown to prevent that from happening anymore.

Hey Anton, let me check. We wrote some python code for this and I can’t remember if we moved it to production or not.

Ok, so we never fully developed this code out or added it to Github.

I wrote this test program a while back to connect with the MCP3021 chip that measures the voltage (http://ww1.microchip.com/downloads/en/DeviceDoc/21805B.pdf). The chip is connected on the I2C bus of the BrickPi+, and measures the voltage through a voltage divider circuit.


# Voltage reading on BrickPi+
# I2C Connection Using the MCP3021
# Future work: 
#  	Convert the raw data to something meaningful.
#	?

import smbus
import time

bus = smbus.SMBus(1)		# SMBUS 1 because we're using greater than V1.
address = 0x48

def main():
	 time.sleep(0.1)
 	try:
  		i2c_return = bus.read_i2c_block_data(address, 0)
  		print "Result: " + str(i2c_return)

 	except:
  		print " Failed."

#if __name__=='__main__':

main()

Thanks! I puzzled trough the chip documentation and created this full working python code.

1 Like

Anton, thanks so much for this, really beautifully done! I have put it into our Github account. I linked your github account as attribution, but if you want me to change anything, please let me know!

https://github.com/DexterInd/BrickPi_Python/blob/master/Sensor_Examples/BrickPi%2B_Read_Battery_Voltage.py

I later moved this:
bus = smbus.SMBus(1)
address = 0x48
Within the try: statement. It’s safer.

Ok, great, just made the changes. Thanks!

Thanks for your read voltage code. I have added it to my programs for the same reason that you wrote it.

I believe this code is part of ev3dev now. (I contributed it) that might
make it easier for you to write cross platform code.

1 Like