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.
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()
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!