BS2 code request: INA 219 Current Sensor

Ken GraceyKen Gracey Posts: 6,550
edited 2015-05-28 - 13:41:14 in BASIC Stamp
Hello there,

This month I've been running an "Educator Hotline" where we can talk about anything of interest to instructors who use Parallax in their courses. The hotline was really busy yesterday with a half-dozen calls after lunch. One call from a university professor in Illinois was that we produce some BASIC Stamp 2 code for this sensor https://www.parallax.com/product/29130. We're booked internally, so I'm going to reach out to our customers for this code.

I see that it's got a few complexities: I2C and floating point. We can get by without the floating point, and I think there's some helpful I2C examples from Jon McPhalen here in Nuts and Volts of BASIC Stamps Column 115 https://www.parallax.com/downloads/nuts-and-volts-basic-stamps-volume-5.

I'd be happy to provide some of the #29130 current sensors to our community members to help achieve this goal. I'll send out about five of them to those who request. All I ask is that you contribute to this thread and see that we produce a functional, useful code example that I can post on our product page. If you request one, please e-mail me your customer information so I can handle the requests quickly internally.

Who's in?

Thanks,

Ken Gracey
kgracey@parallax.com
«1

Comments

  • Duane DegnDuane Degn Posts: 10,013
    edited 2015-05-07 - 13:47:45
    I don't use the BS2 much but it looks like Jon has done most of the work already with his I2C articles.

    I'll hook up a INA 219 Current Sensor to my BS2 and see if I can adapt Jon's code to read from it.

    I'll send you an email with my address.
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-07 - 15:24:48
    Thanks Duane!

    We could use another developer on this little project. Anybody?

    Thanks,

    Ken Gracey
  • PublisonPublison Posts: 11,156
    edited 2015-05-07 - 16:07:37
    Ken Gracey wrote: »
    Thanks Duane!

    We could use another developer on this little project. Anybody?

    Thanks,

    Ken Gracey

    Ken ,

    Anything in particular the professor wants to monitor?

    I have some time to code.


    Jim
  • stamptrolstamptrol Posts: 1,731
    edited 2015-05-08 - 06:47:20
    Hi Ken,

    Happy to help out.

    Tom Sisk
    63 Bliss Carman Drive
    Fredericton, NB E3B 9P2
    Canada
    Tom Sisk
    Sisk Consulting Inc.
    www.siskconsult.com
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-08 - 08:20:46
    Stamptrol - part is on the way.

    Jim - e-mail me your address please (I know we have it, but send again if you don't mind just to make sure we get it right).

    Thanks,

    Ken Gracey
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-08 - 08:34:04
    I'm between projects and need an excuse not to mow the lawn. I'll help.

    Tom Crawford
    1301 Hamblen Way
    Cool, CA 95614

    Edit:
    1. Are we limited to BS2-vanilla or can we use BS2p with its fancy I2C instructions?
    2. Just elementary voltage and current measurements or also PROGRAMMING THE INA219 POWER MEASUREMENT ENGINE ?
    EndEdit
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-08 - 09:27:35
    Tom, great. I'll put yours in the mail today.

    Your Qs:

    1. BS2 is required; BS2p is the bonus round and we'd be happy to have that too.
    2. Voltage and current. Bonus round would deb the power measurement engine (first I've read of it).

    Usually, the instructors just want their students to be able to identify the variable holding these values and to use it for another purpose. Sometimes it's just as a lab diagnostic tool where they record values as part of their experiments.

    Thanks,

    Ken Gracey
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-08 - 09:29:44
    Ken and collaborators, count me in too. The i2c and the math don't look too hard, but a decision has to be made about which features to expose in a demo program, set in the configuration register. For example, there is the selection of 16 or 32 volt full scale. There are 4 possible settings for the programmable gain for sensitivity to the shunt voltage. At one simple extreme you just leave it at the default 320mV. Or fancier, you implement an autoranging algorithm in order to extend the dynamic range. I'll echo Jim's question, what is the professor looking for?
  • PublisonPublison Posts: 11,156
    edited 2015-05-08 - 10:07:12
    Ken Gracey wrote: »
    Stamptrol - part is on the way.

    Jim - e-mail me your address please (I know we have it, but send again if you don't mind just to make sure we get it right).

    Thanks,

    Ken Gracey

    Ken, I'm putting together an order, so you can include the INA219 in that order at no shipping cost to you.

    Will advise.

    Jim
  • JonnyMacJonnyMac Posts: 6,381
    edited 2015-05-08 - 12:50:19
    Since my name was bandied about (grin) I've attached my Spin code for the INA219 (which I have tested and deployed in client projects) -- might be a useful reference. I2C on a standard BS2 is possible with SHIFTOUT and SHIFTIN and a little extra work. Most of us have done it. Not saying that porting the INA219 code to the BS2 will be easy, but it should be possible.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-08 - 15:28:48
    Oh my, we certainly have a complete team. I think we're all set with the latest additions.

    In a mere 24 hours we have attracted the King of Stamps JonnyMac. And in the same day we acquired the integer math guru, Tracy Allen. As JonnyMac is probably busy I'll be sure that Tracy has some hardware for the project. As for what to expose from this module - everything we can! But since this is for the limited-memory BS2, it's probably best to have a few code variations we can package up so the first-timer student tasked with a project can simply measure current and the power user (with a BS2!) can access more features.

    Thanks for the teamwork. Should be a nice project we can use to take a break from our day jobs!

    Ken Gracey
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-08 - 15:31:06
    Publison wrote: »
    Ken, I'm putting together an order, so you can include the INA219 in that order at no shipping cost to you.

    Will advise.

    Jim

    Thanks Jim. In your order comments, please ask that they include one of these sensors "no charge per Ken".

    Hope all is going well in your world. I'll be seeing you again in D.C. in about 11 months.

    Ken Gracey
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-08 - 16:23:40
    While we're at it, we might as well do a version using the native i2c commands available in the 'p series Stamps.
  • ercoerco Posts: 19,584
    edited 2015-05-08 - 19:14:47
    Sorry I'm missing out! I'm buried for the time being, but ordinarily I love a good BS2 challenge. Heck, I love a good BS1 challenge too.

    Have fun, guys!
    "When you make a thing, a thing that is new, it is so complicated making it that it is bound to be ugly. But those that make it after you, they don’t have to worry about making it. And they can make it pretty, and so everybody can like it when others make it after you."

    - Pablo Picasso
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-10 - 09:57:12
    USPS delivered the INA219 on Saturday (Mailed on Friday).

    Here is an elementary demo that resets the INA219, and then continuously reads and displays the bus voltage and the shunt voltage and current. I think it should be enough to allow the professor to decide what she really wants.

    It is for vanilla BS2; I use the i2c routines from Nuts and Bolts column 219, modified to handle 16-bit registers. I thought about tuning the routes specifically for the 219, but left them general in case other stuff is on the i2c bus. A little shoutout to the Saleae protocol analyzers; they are very cool.

    I used a Board of Education; the 219 is connected as follows:

    Vcc: BofA Vdd
    Gnd: BofA Vss
    SCL: BofA Pin1
    SDA: BofA PIn0
    Vin- 329 ohm (according to my multimeter) resistor in series with random LED to Vss
    Vin+ BofA Vin

    The Board is powered with a precision variable power supply, typically set to 7.90 Volts

    Results:
    The 219 returns bus Voltage pretty much dead on. With the Vin set to 7.90, readings vary around 7.87.
    The shunt voltage is consistently high. Measuring drop across the 329 ohm resistor with a multimeter, I get 5.75 V, suggesting 17.48 mA current. The 219 says 1.81 to 1.87 mV over a 1 minute period (18.1 to 18.7 mA). No load current (disconnecting Vin-), we measure 50 to 100 uV (corresponding to 0.5 to 1 mA). I am at a loss to explain this.

    Next:
    I think I'll use a Bs2P-something with built-in i2C primitives.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Duane DegnDuane Degn Posts: 10,013
    edited 2015-05-10 - 10:02:27
    That was fast!

    My sensor passed through Salt Lake City this morning. It should be here tomorrow.

    I'll make sure and try out your code once the sensor arrives.
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-10 - 15:22:23
    Duane Degn wrote: »

    I'll make sure and try out your code once the sensor arrives.

    I will be very interested if you confirm the issue with no load current. I tried mine with a 9-volt battery to eliminate power supply issues and the no load current remained.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-10 - 19:19:58
    Nice quick work Tom! I don't have one here quite yet either, only a data sheet. The Vos spec is ±200µV max on the PGA/8 range, so your measurement isn't exactly out of spec. ±40µV typical. The full scale is ±3.2A, so the offset is a small fraction of that.

    At first I was thinking there would be a possibility for auto-ranging, that is, to extend the dynamic range by fiddling with the the bus voltage scale and shunt PGA gain. But that is not the way it works. Both of the two bus voltage ranges return 4mV per bit, and all four of the shunt voltage ranges return 10µV per bit. It is only the number of bits that changes (12 or 13 bits for bus voltage, and 12, 13, 14 or 15 bits (+ sign) for shunt voltage). It is done that way in the INA219 so that the "power measurement engine" can be configured to keep the calculated current and power registers from overflowing, and within that constraint to optimize the resolution of small currents. The internal multiplications for current and power happen automatically in the background along with the measurements. Once configured as to scale and resolution, the program can simply read out voltage, current, and power.

    Tom has shown, given the 0.1Ω shunt, current is 100µA per bit, so display the raw value as xxx.x mA. Voltage easy too, xx.xxx V. Classroom lesson #1 is done!

    In a classroom setting, do you want power? If done on the Stamp it will take a double precision calculation or tricks. E.g.16788 millivolts from a solar panel times 234.5 milliamps (as 2345) should come out as 3936786, 3.94+W, but that simple multiply overflows 16 bits. Do you want to teach how to handle that math on the Stamp, or do you want to dig into the the INA219 power engine and let it do it for you? I feel that understanding how to do it on the Stamp could be almost a prerequisite to understanding what has to happen in the INA219. Texas Instruments offers a PC tool that leads you through the calculations cookbook style, but I feel the underlying logic of the program is opaque about what and why, not a good teaching tool.
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-10 - 22:53:39
    Nice quick work Tom! I don't have one here quite yet either, only a data sheet.

    Your hardware is coming soon, too. If it didn't leave Parallax Friday it'll be on the way by Monday.

    Ken Gracey
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-11 - 09:57:24
    Here is pretty much the same program (INA219Demo), except it is for the BS-2P and uses the native I2C instructions.

    It takes 13% vs 27% of the code space and reads one register in 700-odd uSec vs 22 mSec (including setting the pointer). I kind of expected lots of improvement in those two areas. What I hadn't thought about was the savings in register space: 1 word vs 3 6/16 words.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-11 - 18:38:16
    No worries Ken. Tom is having all the fun! I see that mine is in the mail.

    Tom, I think the debug statements should not have the // operator after the decimal point...
    DEBUG "Bus: ", DEC i2cData/1000, ".", DEC3 i2cData//1000, "V"

    The DEC3 automatically picks off the low three digits.

    Shunt V and I may go either + or - for something like a battery, so it may need to display either polarity:
    DEBUG " Shunt: ", REP "-"\i2cData.bit15, DEC ABS i2cData/100, ".", DEC2 ABS i2cData, "mV"
    DEBUG " ", REP "-"\i2cData.bit15, ABS i2cData/10, ".", DEC1 ABS i2cData, "mA", CR
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-12 - 08:45:09
    No worries Ken. Tom is having all the fun! I see that mine is in the mail.

    Tom, I think the debug statements should not have the // operator after the decimal point...
    DEBUG "Bus: ", DEC i2cData/1000, ".", DEC3 i2cData//1000, "V"

    The DEC3 automatically picks off the low three digits.

    Shunt V and I may go either + or - for something like a battery, so it may need to display either polarity:
    DEBUG " Shunt: ", REP "-"\i2cData.bit15, DEC ABS i2cData/100, ".", DEC2 ABS i2cData, "mV"
    DEBUG " ", REP "-"\i2cData.bit15, ABS i2cData/10, ".", DEC1 ABS i2cData, "mA", CR

    "Until Daddy takes the T-Bird away..."

    I agree with what Tracy says, except to point out that the //1000 is merely redundant and (IMO) *does* show more about what is intended.

    If you have, say, a solar cell, a chargeable battery, and a load, you would put the solar cell *and* the load on the Vin+ side and the battery on the Vin- side. Then positive shunt voltage (and current) would show the charge rate and negative would show the discharge rate. The bus voltage minus the shunt voltage would show the battery voltage.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-12 - 10:43:28
    I should have said, "does not need" instead of "should not". It's my knee-jerk reaction to cut redundant code out of BS2 programs, always running up against the 2k limit.

    Yes about the charging current. For the solar panel + battery + load, it may take an additional INA219 to separate out the solar panel current from the load current, if needed. For example, in an MPPT scheme, solar panel V & I will be different from battery+load V & I.
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-17 - 00:48:02
    I had a chance to play with this some. I tried it with Tom's code and it worked fine with a BS2pe. I've added math to calculate and display power in watts, resolved to 1mW. It also shows the busP+ voltage, which is higher than busM- by the voltage across the 0.1Ω shunt.
    Here is output with a 22Ω 10W load connected from bus- to ground, and 15V input.
    attachment.php?attachmentid=114217&d=1431848640

    attachment.php?attachmentid=114218&d=1431848658
    542 x 102 - 25K
    640 x 480 - 51K
  • tomcrawfordtomcrawford Posts: 1,075
    edited 2015-05-17 - 14:52:17
    I put Tracy's power calculations and output routines into BS-2 Vanilla version. It's a pretty leisurely printout. ;-)
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Ken GraceyKen Gracey Posts: 6,550
    edited 2015-05-17 - 14:59:48
    This is great - thanks to everybody.

    I've asked Chris Savage to cruise this thread and do a code harvest for our web site. He'll check in on behalf of our team shortly, I'm sure.

    Sure appreciate this kind of community support for Parallax!

    Sincerely,

    Ken Gracey
  • Tracy AllenTracy Allen Posts: 6,412
    edited 2015-05-17 - 17:36:40
    Here is another use case, the one that Tom brought up in post #23. The INA219 monitors the charge/discharge current of a battery tied to a load and solar panel. The current can be either positive or negative so the PBASIC has to handle the negative numbers gracefully for display and the math.

    In the example printout below,
    -- 6V SLA from Vbus- to ground
    -- 22 ohm resistor load from Vbus+ to ground
    -- BS2pe in parallel with the resistor
    -- Power source (solar panel) to Vbus+ via a blocking diode, other side to ground.

    attachment.php?attachmentid=114225&d=1431909350

    The printout starts with the battery supplying the load, then the solar panel is connected. Note that the external current from the panel will be the sum of the battery current shown plus the continuing load current, which is not measured directly by this INA219.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-05-18 - 08:45:36
    Ken Gracey wrote: »
    I've asked Chris Savage to cruise this thread and do a code harvest for our web site. He'll check in on behalf of our team shortly, I'm sure. Sure appreciate this kind of community support for Parallax!

    I did download the original code that was posted for testing, however it looks like there have been some updates since then. I have the updated code for testing now. Was a little busy learning DIPTrace. I could use a break from that for a day though. =)
    Chris Savage | Engineering Tech | Main Office: (916) 624-8333 | Direct to Tech Support: (888) 997-8267 | Website | Twitter | Google+
  • PublisonPublison Posts: 11,156
    edited 2015-05-18 - 09:00:35
    Ken Gracey wrote: »
    This is great - thanks to everybody.

    I've asked Chris Savage to cruise this thread and do a code harvest for our web site. He'll check in on behalf of our team shortly, I'm sure.

    Sure appreciate this kind of community support for Parallax!

    Sincerely,

    Ken Gracey

    Ken,

    I'm sorry I did not get in on this. My contribution would have been WAY overshadowed by the likes of Tom and Tracy.

    I do want to do, someday, something with this arrangement with Cam's uM-FPU:

    https://www.parallax.com/product/604-00050
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-05-18 - 12:49:53
    I put Tracy's power calculations and output routines into BS-2 Vanilla version. It's a pretty leisurely printout. ;-)

    Tom,

    When I run this code I get the proper current draw, however the bus voltage seems to be wrong and the power seems to be off (this time it is closer). I have a 12V automotive bulb running on my Agilent P/S drawing 0.264A, but this is what I am seeing on the DEBUG screen. Oddly Tracy Allen's BS2pe version seems to be doing the same thing. Hmmm...

    attachment.php?attachmentid=114232&d=1431978565
    883 x 604 - 124K
    Chris Savage | Engineering Tech | Main Office: (916) 624-8333 | Direct to Tech Support: (888) 997-8267 | Website | Twitter | Google+
Sign In or Register to comment.