INA219 High Side DC Current Sensor - Spark Library

I have a few of these voltage & current sensors that I bought from Adafruit and I want to get them working with the Spark Core. I have not tried to get this up and running yet but I’m assuming some code modification will be needed.

Can somebody give the code a look over and see if anything stands out at needing to be changed to get this working with the Spark Core? I’ll then run the modified code and verify its working.

Here are links to data on this sensor.

This breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can just use the handy INA219B chip on this breakout to both measure both the high side voltage and DC current draw over I2C with 1% precision.

Most current-measuring devices such as our current panel meter are only good for low side measuring. That means that unless you want to get a battery involved, you have to stick the measurement resistor between the target ground and true ground. This can cause problems with circuits since electronics tend to not like it when the ground references change and move with varying current draw. This chip is much smarter - it can handle high side current measuring, up to +26VDC, even though it is powered with 3 or 5V. It will also report back that high side voltage, which is great for tracking battery life or solar panels.

A precision amplifier measures the voltage across the 0.1 ohm, 1% sense resistor. Since the amplifier maximum input difference is ±320mV this means it can measure up to ±3.2 Amps. With the internal 12 bit ADC, the resolution at ±3.2A range is 0.8mA. With the internal gain set at the minimum of div8, the max current is ±400mA and the resolution is 0.1mA. Advanced hackers can remove the 0.1 ohm current sense resistor and replace it with their own to change the range (say a 0.01 ohm to measure up 32 Amps with a resolution of 8mA)

We include a 6-pin header (so you can easily attach this sensor to a breadboard) as well as a 3.5mm terminal plug so you can easily attach and detach your load. Usage is simple. Power the sensor itself with 3 to 5VDC and connect the two I2C pins up to your microcontroller. Then connect your target power supply to VIN+ and the load to ground to VIN-.

Here is Arduino library that will do all the gain, range and math for you - just plug and go!

Uses the INA219B chip, see datasheet for detailed specifications
0.1 ohm 1% 2W current sense resistor
Up to +26V target voltage
Up to ±3.2A current measurement, with ±0.8mA resolution
0.9" x 0.8" PCB
This board/chip uses I2C 7-bit addresses 0x40, 0x41, 0x44, 0x45, selectable with jumpers

Adafruit Setup Guide for Arduino

RWB, I looked at the code and it won’t work, even on an arduino!! It clearly was not tested because they did not initialize their code with a device address in the demo sketch! Apart from that, there are some ARDUINO conditional statement that need to be removed. If you want, I can clean it up and get it to compile on the web IDE and then post it on github for you.

I like those breakouts! Gonna have to get me some. :koala:

The Arduino code worked perfectly for me on a Arduino Micro. They are very nice boards indeed.

I build this nice app with it.

I also found this that is using this chip to provide very accurate power data:

That looks greast RWB! I finally found the code that sets the correct U2C address (!) :smile:

Great! Lets see it :smile:

RWB, if you mean where the address is initialized, it’s in Adafruit_INA219.h in the class definition where the constructor is defined, line 114 of the file.

I mean if you fix up the code to where it should work I will give it a try and report back.

I would love to get this working and then forward live data to Google Drive Spreadsheet to show power consumption data in a graph format. Never even thought I could ever do something like that easily before the Spark Core came out!

RWB, I adapted the code to compile for the Spark and it should work, assuming your device address is the default 0x40 (A0,A1 to GND on breakout). You can get it on my github here.

Strangely, the code does NOT compile on the web IDE but it compiles fine locally. I get error “Wire was not declared in this scope” even though it should be intrinsic on the web IDE. If you can’t compile locally, let me know and I can post the firmware.bin as well. :smile:

1 Like

Hi @peekay123

The web IDE changed today to not preprocess included libraries and lots of things now need to

#include "application.h" 

Could that be what is happening here?

1 Like

@peekay123 @bko

Yep thats the error message I get:

In file included from ina129Spark.cpp:21:0:
ina129Spark.h:23:54: fatal error: Application.h: No such file or directory
compilation terminated.
make: *** [ina129Spark.o] Error 1

How do we fix it?

RWB, my bad! I mis-wrote the #include “application.h” in the .h file. DOH! I fixed it in the github files. I removed these 2 includes as well.

//#include <inttypes.h>
//#include <stdint.h>

After the fix, it compiles fine. Give it a shot and let me know how it works. :smile:

@peekay123 @bko @timb @BDub Got it compiled and loaded on the Spark Core successfully.

But something is going on that will not allow me to access the Serial port on my PC. If I load the SHT-15 temp sensor sketch the serial output works fine though with the same serial code in setup.

Any ideas on what area of code that could be wrong and cause the USB serial port to lock up or not work?

My loop is running fine, i added code to blink the LED every loop.

RWB, I’m not sure what you mean by:

If I load the SHT-15 temp sensor sketch the serial output works fine though with the same serial code in setup.

This line in the sketch:


sets the USB serial is set to 115,200 baud so your PC’s serial port needs to be listening at that speed. Then I put a 3 second delay to give you a chance to open the serial port program you use to see the first line of test that prints. It is not crucial but convenient. Nothing else affects the serial port behavior. :smile:

Serial output does not work with this code.
I set the Serial.begin(115200); and then tried setting the Serial.begin(9600); and get the same problem. I can’t open the com port with a serial terminal.

If I load my sketch for the SHT-15 temp sensor that has the same Serial.begin(9600); in it then the COM port works just fine and supplies the data to the serial terminal window.

Weird, something is going on that disables the com port. I assume it the code since other code does not cause this issue.

RWB, which code for the SHT-15 (which works) are you referring to?

OK, it took a factory rest to get it working! The Spark Core froze up with the solid blue LED on flashing it which caused me to do a factory reset and now the COM port opens up!

I’ll do some more testing before I verify that everything is working 100%.

Thanks so much guys! :smiley:

Weird but great RWB! Now, when you get your digole displays… :smile:

1 Like

Yea weird indeed.

I’ll get this rigged up and come back with something cool.

1 Like