INA219 Bi-directional DC Current Power
Posted: Sat Jan 20, 2024 7:49 pm
Hi
Is there support for the INA219 chip ?
Is there support for the INA219 chip ?
All around Annex RDS for ESP8266 and ESP32
https://cicciocb.com/forum/
Code: [Local Link Removed for Guests]
' Simplest possible use of INA219 module
' uses power-on defaults of chip i.e 32 volt scale, 3.2 Amps max and single sample
'some hardware-specific settings
I2C.SETUP 4, 5
dim BUFF_IN(2) ' buffer used for read/write to module
devid=&h40
RSHUNT = 0.1 'Resistor value in ohms - shunt resistor on the module
REG_CONFIG = &h00
REG_SVOLTS = &h01 ' shunt voltage
REG_BVOLTS = &h02 ' bus voltage
busvolts=0
shuntvolts=0
pwr=0
buscurrent=0
config=0
rd_config config
print hex$(config),config
do
getbusvolts busvolts
print busvolts, "mVbus"
getshuntvolts shuntvolts
print shuntvolts, "mVshunt"
buscurrent = shuntvolts/RSHUNT
print buscurrent,"mA"
pwr = busvolts * buscurrent/1000
print pwr,"mW"
print "____________"
pause 3000
loop
sub getshuntvolts(smv)
' Returns voltage across shunt in millivolts
rd_reg REG_SVOLTS, smv
smv = smv * 0.01 'register value * 10uV-per-bit ( for default 320mV FSD )
end sub
sub getbusvolts(bmv)
' Returns bus voltage in millivolts - returns 0 if bad reading (not ready)
rd_reg REG_BVOLTS,bmv
if not (bmv and 1) then ' if good reading (bit 1 = conversion ready, bit 0=overflow)
bmv = bmv >> 3 ' extract millivolts (skip bits 0,1 and 2)
bmv = bmv * 4 ' multiply by 4mv-per-bit
else
bmv=0
endif
end sub
sub rd_reg(reg,regval)
' Read 16-bit value from register
i2c.read_iobuff(0), devid, reg, 2
regval = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
if regval > 32768 then regval = regval - 65536 'hopefully deals with sign bit 15
end sub
sub rd_config(regval)
' Read 16-bit value from register
i2c.read_iobuff(0), devid, REG_CONFIG, 2
regval = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
end sub
Code: [Local Link Removed for Guests]
VBus mVshunt A W
9.3 -0.01 -0.000 -0.001
9.3 4.87 0.049 0.451
9.2 8.26 0.083 0.762
9.2 11.81 0.118 1.085
9.2 14.29 0.143 1.310
9.2 11.74 0.117 1.079
9.2 8.94 0.089 0.824
9.3 4.82 0.048 0.446
9.3 -0.03 -0.000 -0.003
Code: [Local Link Removed for Guests]
' ina219.bas
' uses power-on defaults of chip i.e 32 volt scale, 3.2 Amps max and single sample
'some hardware-specific settings
I2C.SETUP 4, 5
OLED.INIT 1 ' 128.64 upside down (below 4-pin header)
OLED.CLS
OLED.FONT 2 ' ARIAL MT10, ARIAL MT16, ARIAL MT24
OLED.PRINT 35,25, "9.3V" ' WORLD"
OLED.PRINT 35,40, "1.310A" ' WORLD"
' OPTION WLOG 1 ' default: printing
' print "Starting"
' wlog "Starting"
dim BUFF_IN(2) ' buffer used for read/write to module
devid=&h40
last_pwr=-99
RSHUNT = 0.1 'Resistor value in ohms - shunt resistor on the module
REG_CONFIG = &h00
REG_SVOLTS = &h01 ' shunt voltage
REG_BVOLTS = &h02 ' bus voltage
busvolts=0
shuntvolts=0
pwr=0
buscurrent=0
config=0
' wlog "Before rd_config"
pause 1000
rd_config config
wlog hex$(config),config
wlog "VBus mVshunt A W"
do
getbusvolts busvolts
getshuntvolts shuntvolts
buscurrent = shuntvolts/RSHUNT
pwr = busvolts * buscurrent/1000
diff=abs(last_pwr-pwr)
' if last_pwr<>pwr then
if diff>20 then
last_pwr=pwr
' wlog busvolts, "mVbus"
' wlog shuntvolts, "mVshunt"
' wlog buscurrent,"mA"
' wlog pwr,"mW"
' wlog "____________"
a$=str$(busvolts/1000,"%02.1f")+" "+str$(shuntvolts,"%02.2f")+" "
a$=a$+str$(buscurrent/1000,"%2.3f")+" "+str$(pwr/1000,"%3.3f")
wlog a$
endif
pause 1000
loop
sub getshuntvolts(smv)
' Returns voltage across shunt in millivolts
rd_reg REG_SVOLTS, smv
smv = smv * 0.01 'register value * 10uV-per-bit ( for default 320mV FSD )
end sub
sub getbusvolts(bmv)
' Returns bus voltage in millivolts - returns 0 if bad reading (not ready)
rd_reg REG_BVOLTS,bmv
if not (bmv and 1) then ' if good reading (bit 1 = conversion ready, bit 0=overflow)
bmv = bmv >> 3 ' extract millivolts (skip bits 0,1 and 2)
bmv = bmv * 4 ' multiply by 4mv-per-bit
else
bmv=0
endif
end sub
sub rd_reg(reg,regval)
' Read 16-bit value from register
i2c.read_iobuff(0), devid, reg, 2
regval = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
if regval > 32768 then regval = regval - 65536 'hopefully deals with sign bit 15
end sub
sub rd_config(regval)
' Read 16-bit value from register
i2c.read_iobuff(0), devid, REG_CONFIG, 2
regval = iobuff.read(0, 0) << 8 + iobuff.read(0, 1)
end sub