INA219 Bi-directional DC Current Power

Give it a try, it costs you nothing !
Post Reply
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

INA219 Bi-directional DC Current Power

Post by f1test »

Hi

Is there support for the INA219 chip ?
image.png
You do not have the required permissions to view the files attached to this post.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: INA219 Bi-directional DC Current Power

Post by cicciocb »

No, but should be easy to interface using i2c commands
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 47 times
Been thanked: 50 times

Re: INA219 Bi-directional DC Current Power

Post by bugs »

I may have posted this before - not used the device for a few years.
Maybe give you some ideas.

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
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: INA219 Bi-directional DC Current Power

Post by f1test »

This is gold.
I will receive the module and I will test it

thank's.
lyizb
Posts: 97
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 40 times
Been thanked: 24 times

Re: INA219 Bi-directional DC Current Power

Post by lyizb »

"This is gold", indeed--that's what I thought. Here's my lash-up: on a dual D1Mini base, OLED on the right and on the left, a D1Mini sea-of-holes board wired to plug in the INA219, with an ESP8266-D1Mini plugged in on top of it.
INA219_D1Mini.jpg
I love the little panel-mount voltage/amperage/wattage displays, but need something to monitor 12V and 24V batteries and send updates via wifi--UDP or MQTT. I had some INA219s which I had never tried to use, so this looked like a good starting point.

I have an ESP32-S2Mini controlling a 6-relay module turning on 4 12V LEDs (which also work on 9V). I connected the 9V power source to V+, V- to the load (supplying the LEDs via the relays). I changed the code so that it only reports the reading if the change in wattage is greater than 20 milliwatts.

I got changing values, but was baffled because the voltage reading made no sense. After some time scrolling through INA219 examples, which mostly used the supply of the microprocessor as the supply for the load, I came across one saying that if the load had a separate power supply, you needed to connect 0V between it and the micro 0V. Bingo! Now it made sense as one LED after another came on and went off:

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
Here's the code (the OLED display is fudged--I haven't gotten to the point of plugging in the actual readings).

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
My actual need is to be able to read more than 3A, so I've ordered some INI226 modules--50A current monitor.
You do not have the required permissions to view the files attached to this post.
Post Reply