Using SoftI2C with INA219 (curent sensor)

Go To Last Post
39 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings,

I'm trying to use felias-fogg excellent Soft I2C library

https://github.com/felias-fogg/SoftI2CMaster

with Adafruits

https://github.com/adafruit/Adafruit_INA219/blob/master/examples/getcurrent/getcurrent.ino

 

I replaced the

#include <Wire.h>

with

#include <SoftWire.h>
SoftWire Wire = SoftWire();

etc as described above, also in the INA libray, but get a bunch of errors.

I tried some combinations of declaring but the compiler always complains.

 

Attached the code I am using

 

Can anybody help me please?

Thanks!!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kontrabass wrote:
I replaced the

#include <Wire.h>

 

Why?

 

Surely Wire.h is well-known, well-tested, and widely-used - why change it?

 

get a bunch of errors

Go on - what errors, exactly, do you get?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Kontrabass wrote:
I replaced the

#include <Wire.h>

 

Why?

<Wire.h> uses the dedicated i2c pins. However, as mentioned, I need to use different pins, thus Softi2c .

 

I just don't know how to use that Softi2c library to work with the INA library.

 

Errors are way too much to post here as text.

Will try to zip them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kontrabass wrote:
I need to use different pins, thus Softi2c .

Always best to use the actual I2C pins if at all possible - what prevents that?

 

I just don't know how to use that Softi2c library to work with the INA library.

Are you still doing it within the Arduino IDE?

 

Errors are way too much to post here as text.

Always address the earliest error first - many (possibly all) of the following errors will just be direct knock-on consequences

 

 

Kontrabass wrote:
I just don't know how to use that Softi2c library to work

There seem to be quite detailed instructions on the Github page - with examples.

 

Have you followed them to get familiar with the library?

 

The facilities of the Wire library are documented:

 

https://www.arduino.cc/en/reference/wire

 

So you're going to have to work out which of those facilities the "INA" code uses, and then  implement the same using the Softi2c library.

 

That's a standard porting exercise.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Jan 17, 2020 - 11:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Kontrabass wrote:

Are you still doing it within the Arduino IDE?

 

Yes, the errors are easily reproduceable by compiling getcurrent-soft.ino (see attached file in my OP).

Errors attached below.

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, as I said, start with the very first error:

 

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

                 from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:90,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

                 from sketch\Adafruit_INA219.cpp:27:

C:\Arduino\libraries\SoftI2CMaster/SoftI2CMaster.h: In function 'void i2c_wait_scl_high()':

C:\Arduino\libraries\SoftI2CMaster/SoftI2CMaster.h:235:31: error: 'SCL_PORT' was not declared in this scope

Now, clearly, every single line which refers to "SCL_PORT" is going to have exactly the same error - so fix this one thing, and you will fix one whole bunch of errors in one fell swoop!

 

And the 2nd error is like it:

 #define SCL_IN  (_SFR_IO_ADDR(SCL_PORT) - 2)

                               ^

C:\Arduino\libraries\SoftI2CMaster/SoftI2CMaster.h:272:23: note: in expansion of macro 'SCL_IN'

      : : [SCLIN] "I" (SCL_IN), [SCLPIN] "I" (SCL_PIN));

                       ^

In file included from C:\Arduino\libraries\SoftI2CMaster/SoftWire.h:23:0,

                 from sketch\Adafruit_INA219.cpp:39:

C:\Arduino\libraries\SoftI2CMaster/SoftI2CMaster.h:272:46: error: 'SCL_PIN' was not declared in this scope

 

Which is basically the same.

 

So it looks like you haven't defined any of your pins or ports - clearly, the code can't build without knowing that!

 

The Github page tells you how & where to define these things - so you need to go back & study that.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

That's a standard porting exercise.

 

 

I tried.

Thus here.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you had looked at "any" of the files I submitted, you could see that I tried all that, resulting in duplicate declaration errors etc.

But it's ok, I hope some knowledged coder can help me out here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kontrabass wrote:
resulting in duplicate declaration errors

So handle them with  exactly the same approach!

 

The errors tell you where both the "original" and the duplicate were found - so you need to decide which to keep, and which is not required ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The SDA/SCL PORT/PIN values are set to the default values for TWI (AVR term for I2C).  PORTC PIN4 is Arduino's A4.   

 

Change these values in the .ino file to the two pins that you are using for softI2C.

 

The only problem that I have found with always using the TWI default pins (Port C:bit 4 for SDA, and Port C:bit 5 for SCL) is that the common TFT color displays (320x240 pixels, with touch-screen) use A4 as a dedicated reset to the TFT.   To use TWI (I2C, wire.h) with these displays, I have to cut trace that goes from AVR reset\ to A4.  If I don't do this, then the AVR resets whenever TWI functions are used.

  I add a few cm of thin wire-wrap (30 gauge) wire between the Arduino reset switch and the trace that goes to the TFT's reset ( which used to also go to A4).  Now the TFT reset\ is activated by the AVR's reset\. 

Last Edited: Fri. Jan 17, 2020 - 06:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As an alternative current sensor, you may want to look at the ACS723, no current sense resistor is needed, if it fits your measurement requirements.

Comes in 5,10,20 and 40 amp max versions: https://www.allegromicro.com/~/m...

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since we’re talking current sensors, our friends at Microchip have a part similar in function to the INA219, but four channel. Based on Digikey prices, marginally more expensive, but for four channels. i2C interface and can calculate power and average as well.

[edit] the part is PAC1934.

Last Edited: Fri. Jan 17, 2020 - 10:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a three-channel version of the INA219 called the INA3221.  Ebay has a cheap module board: https://www.ebay.com/itm/I2C-INA...

 

However this particular module board has all three channels connected to the same V+ input, so a little Dremel tool trace cutting and wire rework is needed to get it to be three fully independent channels.  Google INA3221 Arduino for the procedure and details.   Accurate up to 26 volts DC.   The PAC1934 is a ball-grid array device, which means you aren't going to get it onto your protoboard design without special equipment or expensive robotic pic'n'place custom manufacturing.

 

Banggood,com is having a sale on demo sensor-module boards.  They have a collection of 37 different sensors on sale for less than $10.  Get them all working correctly and call yourself an embedded-systems technician.   Save yourself tens of thousands of dollars worth of student loans.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

The SDA/SCL PORT/PIN values are set to the default values for TWI (AVR term for I2C).  PORTC PIN4 is Arduino's A4.   

 

Change these values in the .ino file to the two pins that you are using for softI2C.

 

Excellent catch, thank you!

I had used different pins, but to post here started with the clean examples (getcurrent from INA219), same with the softi2c example (thus the default values).

Attached please find the version with actual needed pins, sorry about that!

 

I use all those three files in one folder, if that's of some importance here.

 

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

 

Same errors still, it's like whack-a-mole, if I uncomment the defines in one of the files(.cpp, .h or .ino) the others complain.

 

Thanks!

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Pac1934 is QFN16 package. I used a normal soldering iron and flux to solder by hand.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Adafruit INA219 library example uses the default .begin() method, which assumes the hardware TWI peripheral.

 

You need to call this method explicitly with your software I2C object.

 

e.g. (not tested)

 

// #define pins and ports

SoftWire Wire = SoftWire();
Adafruit_INA219 ina219;

void setup()
  Wire.begin();
  ina219.begin(&Wire);

 

Last Edited: Sat. Jan 18, 2020 - 08:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried what you suggested, and it again spits out a bunch of reds, stops and says in line 146 of Adafruit_INA219.h

 

void begin(TwoWire *theWire = &Wire);

                      ^

'TwoWire' has not been declared
 

It very often lands there, bummer!

Thanks for the try!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I don't think this is going to work, or it's beyond my C++ abilities.

 

The Adafruit_INA219 library expects an object derived from the TwoWire class (the default hardware I2C peripheral), whereas the SoftWire class is derived from the Stream class.

 

At this point, I would just hack out the functions I needed from the Adafruit library. All the code you need is in there and it's just reading and writing to device registers. Best way to learn :)

 

I'm not impressed with that I2c library. It doesn't have any include guards and it definitely isn't the drop-in replacement it claims to be.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

obdevel wrote:

At this point, I would just hack out the functions I needed from the Adafruit library. All the code you need is in there and it's just reading and writing to device registers.

I wish I had the knowledge to do that, but I don't, that's why I ask the forums.

Meanwhile, I tried another (much better) INA library, ending with the exact same error message,

 

void begin(TwoWire *theWire = &Wire);

                      ^

'TwoWire' has not been declared

 

Thanks for your kind support!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You were on the right path. In the ina219 library all references to TwoWire needed to be changed to SoftWire. You made the mistake of creating multiple instances on the same SoftWire object. I've not tried compiling the code, but hopefully my changes are a bit closer to having it work.

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Kartman,

thank you very much for this, the errors became significantly less!

However, that dreaded "TwoWire Wire;" error is still there, and a bunch of others but again, your edits reduced the errors dramatically!

Attached, thanks!!!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's look at the first two errors:

 

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src/Wire.h:82:16: error: conflicting declaration 'TwoWire Wire'

 extern TwoWire Wire;

                ^

C:\ASSD\getcurrent-soft-2\getcurrent-soft\getcurrent-soft.ino:10:10: note: previous declaration as 'SoftWire Wire'

 SoftWire Wire = SoftWire();

 

          ^

 

This is pretty obvious what it is complaining about - we've declared Wire twice - but different types. The solution is:

 

 

SoftWire SWire = SoftWire();

 

then we need to pass this to the ina219 constructor.

rather than ina219() we need ina219(&SWire);

 

 

 

In file included from C:\ASSD\getcurrent-soft-2\getcurrent-soft\getcurrent-soft.ino:12:0:

C:\Arduino\libraries\Adafruit_INA219/Adafruit_INA219.h:133:34: error: could not convert '& Wire' from 'SoftWire*' to 'TwoWire*'

   void begin(TwoWire *theWire = &Wire);

 

Did you update the h file? I think I fixed this - check it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did fix the .h file - did you copy my one to the right location?

 

These are the fixes in your ino file:

#include <SoftWire.h>
SoftWire SWire = SoftWire();

#include <Adafruit_INA219.h>

Adafruit_INA219 ina219(&SWire);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I put all the files in one folder and (Arduino IDE) compiled it from there.

 

I unzipped the getcurrent-soft-2_km.zip again and cannot see

SoftWire SWire = SoftWire();

in either .h or .ino

where do I put those please?

 

I exchanged

Adafruit_INA219 ina219();

Adafruit_INA219 ina219(&SWire);

and

SoftWire Wire = SoftWire();
SoftWire SWire = SoftWire();

in the .ino file as you wrote above, but now the erros are more than before, soory.

Could you please attach the files you sent me again?

Thank you for your patience!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What are the errors telling you?

 

 

zip up your files and error list. I deleted my copy of the files.

 

As for where to put the library files - Arduino is expecting to find them here as the error tells you:

 

C:\Arduino\libraries\Adafruit_INA219/Adafruit_INA219.h:133:34: error: could not convert '& Wire' from 'SoftWire*' to 'TwoWire*'

 

 

Last Edited: Sun. Jan 19, 2020 - 10:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Meanwhile I moved all files from your getcurrent-soft-2_km.zip

(getcurrent-soft-2.ino (I renamed this one), Adafruit_INA219.h. Adafruit_INA219.cpp)

 to

C:\Arduino\getcurrent-soft-2\

 

Then added some of the &SWire stuff you mentioned above and that generated the

getcurrent-soft-2a_errors.txt file

All that attached.

Thanks!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

IMHO this is a fool's errand.

 

In order to use the soft I2C object in the Adafruit library, you have to #include the libraries and set the pin #defines in both your program and the library source. But ... the soft I2C library has no include guards, so you can't #include it more than once without throwing even more errors.

 

What subset of the Adafruit library do you actually want to use ? It will be so much easier to just hack the required code out of the Adafruit library into your program. Really.

 

And take care editing Arduino libraries. The IDE will happily overwrite your changes the next time the library author issues an update.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did try to compile it and had the same problems. Like obdevel says, you need to put the port pin #defines in the header file. It did seem to have header guards though.

 

It's getting late for me, so I'll leave it here.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, thanks again for helping, we're close.. only one error.

Considering what you guys were talking about (AdafruiT header guards etc), I changed out the INA219 library to a much better (function wise) one, and after researching and fiddeling, ended up with only one error, please see attached.

 

I used

https://platformio.org/lib/show/...

and made the Wire->SWire changes Kartman mentioned, find&replaced all "Wire" to"SWire" etc.

I could not find the above mentioned

rather than ina219() we need ina219(&SWire);

 

However, removing one of the "redefinition" has catastrophic results...

Thanks!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Morning!

I tried all variations of declaring and supressing in the three files (ina219_128samples_Shunt33.zip) above, but that is the one with just one error.

Is there a way to supress the "header guards" you mentioned or are they crutial for the correct outcome?

I read that some compile their programs outside of Arduino and upload the generated HEX file directly.

Would that be a solution?

Also, I tried to unfold the .h and .ccp into the main program, as was suggested, but that generated a host of new errors.

Please help!

We're so close, just one error.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kontrabass wrote:
We're so close, just one error.

And it is......???

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All included in above zip file:

 

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "-IC:\Arduino\libraries\SoftI2CMaster" "C:\Users\w7s\AppData\Local\Temp\arduino_build_786535\sketch\INA219.cpp" -o "C:\Users\w7s\AppData\Local\Temp\arduino_build_786535\sketch\INA219.cpp.o"
INA219.cpp:48: error: redefinition of 'SoftWire SWire'

 SoftWire SWire = SoftWire();   // jjj per  Kartman

          ^

In file included from C:\Users\w7s\AppData\Local\Temp\arduino_build_786535\sketch\INA219.cpp:37:0:

 

 

 

Thanks!

Last Edited: Tue. Jan 21, 2020 - 09:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SWire is defined more than once. You need to resolve this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

God gave you Wire.h

Microchip/Atmel gave you the hardware TWI pins.

 

A bus is designed to carry many passengers e.g. several I2C slaves connected to a single I2C bus (via hardware SDA, SCL pins)

It is possible to mix 5V and 3.3V slaves on the bus (fairly easy)

 

I have not been following this thread religiously.   Is there a good reason for using non-TWI pins?

 

Yes,  you can use SoftWire.h to enable a conventional sketch to use the same Wire.xxx() calls

SoftWire methods() have the same API as Wire methods()

Change include and constructor statements.   Your main sketch logic remains unchanged.

 

If any external libraries expect a Wire.h object they will not know about your SoftWire object.

 

It gets very difficult to adapt third party libraries e.g. INA219 expects Wire.h

What happens when the Library Manager tells you that a new version of INA219 is available?

 

If you can provide a convincing reason for not using hardware pins,  we can show you what to do.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

SWire is defined more than once. You need to resolve this.

I would not ask for your guys and gals time if I knew how to do that correctly.
I tried all variations (of un-defining) but it's like whack a mole, it always complains about something somewhere else.

 

david.prentice wrote:

If you can provide a convincing reason for not using hardware pins,

 

 

There are many resasons, but the most important is the powering down of the other peripherals attached to the i2c bus. Once they are powered down,their pullups drag it all down with them.

We need to use some i2c units while others are powered off.

The two INA219s would be on their own dedicated power pins, which not only powers them, but also their (isolated) pullups.

This is for a very low power application.

The other reason is that one (maybe two) other I2C units don't play well together, something with streching some pulses.

And yes, I tried other i2c libraries, but the combination of these two issues would be resolved with soft i2c easily.

I am using softSPI already, and it works great.

It was suggested to unfold the INA program/header/ccp, so I copy and pasted those parts but that resulted in even more weird errors.

I understand that many want to help by suggesting or pointing to other ways or methods, but that is just diverting from the issue we have.

Other places I turned to and asked for help insisted to unfold the files as bare text, which I had zipped.

They somehow believed (as in believe in religion) that opening a zip file would somehow damage or endanger their computers.

I even did that to satisfy them, but that was it, no help whatsoever.

 

david.prentice wrote:

What happens when the Library Manager tells you that a new version of INA219 is available?

I rename those original libraries, including their .h and .ccp and some other files, so they will never be updated or overwritten.

 

In this thread I just used the most original versions to make it less confusig, since the changes (resistor values etc) have no bearing on the issue.

 

david.prentice wrote:

we can show you what to do.

That would be really great, thank you!!!

 

I don't want to add unneccessary load this fine avrfreaks.net service with yet another copy of the zipped files, they are here:

https://www.avrfreaks.net/comment/2832746#comment-2832746

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have read your "justification".   I am not convinced.

 

It is unwise to "power-down" electronics on individual boards.

You should put items into "low-power" state e.g. SLEEP but with a full VCC supply.    GPIO in 3-state.

 

Of course you can "power-down" ALL the electronics in one go.

 

Yes,  you can reduce current by clocking the AVR slowly.   But it is better to Sleep.

 

Anyway,   I suspect that this will fall on deaf ears.

 

Please connect your INA219s to the hardware bus.    Install the proper INA219 library.    Make sure that your sketch is working 100%.

 

Then attach the working files in a ZIP.

 

I will show you how to make a local class called INA219_SWire.

 

You would just replace

#include <INA219.h>

// Current sensor and shunt used 
INA219 ina219;

with

#include "INA219_SWire.h"

// Current sensor and shunt used 
INA219_SWire ina219;

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

 

Please connect your INA219s to the hardware bus.    Install the proper INA219 library.    Make sure that your sketch is working 100%.

 

Then attach the working files in a ZIP.

I will show you how to make a local class called INA219_SWire.

 

You would just replace

#include <INA219.h>

// Current sensor and shunt used
INA219 ina219;

with

#include "INA219_SWire.h"

// Current sensor and shunt used
INA219_SWire ina219;

 

David.

 

Done!

Both INAs report correctly and were attached to the HW I2C.
 

The program is just the getcurrent example modded for 2xINA219:

https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/arduino-code

Attached that library and the functioning program "get2currents01.ino"

 

Will the "local class" you want to show me help with those dreaded duplicate errors?

Looking forward in excitement!

Thank you!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have attached a sketch.   Unzip into your sketch folder.

 

You can see my edits by searching for ".kbv" with [Search all Sketch Tabs]

 

In "Adafruit_INA219_soft.cpp" tab you should put your own sda, scl pin numbers.   i.e. regular digital# or analog#

 

Untested.  Please let me know how you get on. 

 

The basic principles are:

1.  IDE compiles each C or CPP tab separately.

2.  IDE concatenates INO files.   And converts to a single CPP file.

3.  IDE parses the INO file to determine libraries used and any forward references.

4.  IDE compiles this CPP file as one unit.

5.  The Linker sews it all up.

 

6.  A C or CPP file only knows about external references that is have been read from included H files

7.  TwoWire and SoftWire classes have methods with the same API but the actual TwoWire object and SoftWire object are different.

 

8.  Wire is a global TwoWire object.   Which is why it must only occur once.   And why you can't have a TwoWire pointer to a SoftWire object.

 

I still reckon that you should look at your individual I2C Slaves.    Which ones are current-hungry?   Configure their registers to take less power.  

e.g.  tell the INA219 to powerSave() instead of removing VCC.

e.g.  tell the INA219 to read ADC less often

 

David.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

David, I tried your code, inserted my pins but got many errors.

Tried to fill in the missing defines but that would generate even more errors.

Will post the errors tomorrow.

Meanwhile found a soft i2c library which works so-so, but only with one INA219

Bummer

Thanks!

 

Last Edited: Tue. Jan 28, 2020 - 09:39 AM