Mark Chin
Published

Lithium Ion battery HAT for Raspberry Pi

Run your Pi remotely with a 18650 Lithium Ion battery.

BeginnerProtip15 minutes10,247
Lithium Ion battery HAT for Raspberry Pi

Things used in this project

Story

Read more

Schematics

Pi18650 Block Schematic Diagram

Charging and Power Circuitry for Pi18650 HAT

Code

Pi18650 Code

Python
Python Script to Read Battery Status
def 	fuelguage_check_volt(self):

		#voltage is 14bit (reg's I and J) and temperature 10bit (reg's M and N)resolution 
		#convert I and J to 16bit variable, then (value / 65535) x 6V = Voltage 

		#read registers I and J (8 and 9 sequentially)
		voltage_msb = float(0.0)
		voltage_lsb = float(0.0)
		temp_msb = float(0.0)
		temp_lsb = float(0.0)

		all_regs = bus.read_i2c_block_data(FUELGUAGE_ADDR, FUELGUAGE_REG_ADDR_08, 16)
		status, control, acc_msb, acc_lsb, chrgthhi_msb, chrgethhi_lsb, chrgthlow_msb, chrgthlow_lsb, voltage_msb, voltage_lsb, voltth_msb, voltth_lsb, temp_msb, temp_lsb, tempth_msb, tempth_lsb = all_regs
		#print "all regs1: ", all_regs 

		
		#(voltage_reg_I_J) = bus.read_i2c_block_data(FUELGUAGE_ADDR, FUELGUAGE_REG_ADDR_08, 2)
		#voltage_msb = bus.read_byte_data(FUELGUAGE_ADDR, FUELGUAGE_REG_ADDR_08)
		#voltage_lsb = bus.read_byte_data(FUELGUAGE_ADDR, FUELGUAGE_REG_ADDR_09)

		#print "voltage_reg_I_J: ", voltage_reg_I_J
		#voltage_msb, voltage_lsb = voltage_reg_I_J

		#print "voltage_msb: ", voltage_msb
		#print "voltage_lsb: ", voltage_lsb


		voltage_16bit = float(0.0)
		voltage_msb = voltage_msb << 8
		#print "voltage_msb: ", hex(voltage_msb)
		#print "voltage_lsb: ", hex(voltage_lsb)

		
		voltage_16bit = float(voltage_msb | voltage_lsb)		

		#print "voltage_16bit: ", voltage_16bit


		#voltage at battery
		battery_volt = float(0.0)
		divisor = float(65535.0)
		multiplier = float(6.0)
		battery_volt = ( voltage_16bit / divisor ) * multiplier		

		print "battery_volt: ", battery_volt

		battery_capacity_percent = 0
		

		#battery capacity reference table
		if battery_volt >= 4.20:
			battery_capacity_percent = 100

		elif  battery_volt < 4.20 and battery_volt >= 4.15:
			battery_capacity_percent = 98

		elif  battery_volt < 4.15 and battery_volt >= 4.10:
			battery_capacity_percent = 95

		elif  battery_volt < 4.10 and battery_volt >= 4.00:
			battery_capacity_percent = 85

		elif  battery_volt < 4.00 and battery_volt >= 3.95:
			battery_capacity_percent = 75

		elif  battery_volt < 3.95 and battery_volt >= 3.90:
			battery_capacity_percent = 65

		elif  battery_volt < 3.90 and battery_volt >= 3.85:
			battery_capacity_percent = 55

		elif  battery_volt < 3.85 and battery_volt >= 3.80:
			battery_capacity_percent = 50

		elif  battery_volt < 3.80 and battery_volt >= 3.75:
			battery_capacity_percent = 48

		elif  battery_volt < 3.75 and battery_volt >= 3.70:
			battery_capacity_percent = 44

		elif  battery_volt < 3.70 and battery_volt >= 3.65:
			battery_capacity_percent = 38

		elif  battery_volt < 3.65 and battery_volt >= 3.60:
			battery_capacity_percent = 36

		elif  battery_volt < 3.60 and battery_volt >= 3.55:
			battery_capacity_percent = 34

		elif  battery_volt < 3.55 and battery_volt >= 3.50:
			battery_capacity_percent = 31

		elif  battery_volt < 3.50 and battery_volt >= 3.45:
			battery_capacity_percent = 29

		elif  battery_volt < 3.45 and battery_volt >= 3.40:
			battery_capacity_percent = 26

		elif  battery_volt < 3.40 and battery_volt >= 3.35:
			battery_capacity_percent = 24

		elif  battery_volt < 3.35 and battery_volt >= 3.30:
			battery_capacity_percent = 23

		elif  battery_volt < 3.30 and battery_volt >= 3.25:
			battery_capacity_percent = 21

		elif  battery_volt < 3.25 and battery_volt >= 3.20:
			battery_capacity_percent = 18

		elif  battery_volt < 3.20 and battery_volt >= 3.15:
			battery_capacity_percent = 16

		elif  battery_volt < 3.15 and battery_volt >= 3.10:
			battery_capacity_percent = 15

		elif  battery_volt < 3.10 and battery_volt >= 3.05:
			battery_capacity_percent = 11

		elif  battery_volt < 3.05 and battery_volt >= 3.00:
			battery_capacity_percent = 10
		else:
		      battery_capacity_percent = 0
		

		print "battery_capacity_percent: ", battery_capacity_percent


		#temperature conversion to celcius ------------------------------------------------

		#print "temp_msb: ", temp_msb

		#print "temp_lsb: ", temp_lsb

		temperature_16bit = float(0.0)
		temp_msb = temp_msb << 8

		#print "temp_msb: ", hex(temp_msb)

		temperature_16bit = float(temp_msb | temp_lsb)	

		#print "temperature_16bit: ", temperature_16bit

		#voltage at battery
		temperature_kelvin = float(0.0)
		temperature_multiplier = float(600.0)
		temperature_kelvin = ( temperature_16bit / divisor ) * temperature_multiplier	

		print "temperature_kelvin: ", temperature_kelvin

		temperature_celcius = float(0.0)

		temperature_celcius = temperature_kelvin - 273.0

		print "temperature_celcius: ", temperature_celcius

		

		return battery_capacity_percent, temperature_celcius, battery_volt

Credits

Mark Chin

Mark Chin

5 projects • 10 followers
Entrepreneur and Innovator of embedded systems, RFID and IoT.

Comments