BS2 code request: INA 219 Current Sensor
Ken Gracey
Posts: 7,395
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
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
Comments
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.
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
Happy to help out.
Tom Sisk
63 Bliss Carman Drive
Fredericton, NB E3B 9P2
Canada
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
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
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
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
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
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
Have fun, guys!
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.
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.
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.
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.
Your hardware is coming soon, too. If it didn't leave Parallax Friday it'll be on the way by Monday.
Ken Gracey
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.
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.
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.
Here is output with a 22Ω 10W load connected from bus- to ground, and 15V input.
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
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.
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.
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.
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
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...