Cannot include library

I have bought this item:

I download it this library:

I try to import as .zip file from the libraries section in Arduino IDE but it shows that the library file does not contain a correct library file. After searching it, the downloaded file contains the same subfolder twice: for example:

USB-Power-Gauge-master -> USB-Power-Gauge-master -> components of the library.

I tried to configure it with winrar/winzip but again it does make this 2-subfolder hieracrchy, I do not know why. Is there any other way to include a library on Arduino IDE?

Copy and rename the leaf (furthest nested) folder to remove the '-master'.

aarg:
Copy and rename the leaf (furthest nested) folder to remove the '-master'.

I did it, nothing worked... The same error message..

alex5678:
I download it this library:
GitHub - adafruit/USB-Power-Gauge: Code for the Adafruit USB Power Gauge mini kit

That's not a library. It's a sketch. You might have been confused by seeing the .cpp files, because you don't often see them in Arduino sketches, but they are fully supported.

Using this sketch is a little tricky because the Arduino IDE requires every sketch to be in a folder that matches the primary sketch filename, and this is not the case with this sketch. Here's how to do it:

Download the sketch: https://github.com/adafruit/USB-Power-Gauge/archive/master.zip

Unzip the downloaded file. You should now have a folder structure that looks like this:

USB-Power-Gauge-master
|_ adafruit_usbpowergauge.ino
|_ analog.cpp
|_ charlie.cpp

... and so on.

You need to rename the folder USB-Power-Gauge-master to adafruit_usbpowergauge.

Start the Arduino IDE.

File > Open

From the renamed folder, select adafruit_usbpowergauge.ino

Click the "Open" button.

You'll also need to install Adafruit AVR Boards and select the correct board from the Tools > Board menu.

I did the above procedure, and the output I get is this:

Arduino: 1.8.12 (Windows 10), Board:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino: In function 'void setup()':

adafruit_usbpowergauge:71:3: error: 'TCCR1' was not declared in this scope

   TCCR1 = _BV(CS10);

   ^~~~~

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:71:3: note: suggested alternative: 'TCCR1A'

   TCCR1 = _BV(CS10);

   ^~~~~

   TCCR1A

adafruit_usbpowergauge:73:3: error: 'TIMSK' was not declared in this scope

   TIMSK |= _BV(OCIE1A);

   ^~~~~

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:73:3: note: suggested alternative: 'TIMSK0'

   TIMSK |= _BV(OCIE1A);

   ^~~~~

   TIMSK0

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino: In function 'void loop()':

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:141:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

     printStringDelay("\n\rV: "); //ss.println(vcc);

                               ^

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:145:28: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

     printStringDelay(" I: ");

                            ^

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:149:28: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

     printStringDelay(" mA ");

                            ^

D:\ARDUINO_LIBRARIES\USB-Power-Gauge-master to adafruit_usbpowergauge\adafruit_usbpowergauge\adafruit_usbpowergauge.ino:151:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

     printStringDelay("Watts: ");

                               ^

exit status 1
'TCCR1' was not declared in this scope

Post your complete code that won't compile.

alex5678:
I did the above procedure, and the output I get is this:

Arduino: 1.8.12 (Windows 10), Board:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

You forgot to select the right board. The Adafruit USB Power Gauge uses an ATtiny85 microcontroller. The sketch is specifically written for the ATtiny85. You're trying to compile it for the ATmega2560. That will never work.

I think it would be a good idea for you to explain what you're trying to accomplish. The kit comes with the sketch pre-loaded on the ATtiny85, so the only reason you even need to mess with that sketch is if you want to customize the behavior of the power gauge. Most people will have no need for that. If you do want to customize your power gauge, you're going to need an ISP programmer (which could be a spare Arduino used as an "Arduino as ISP" if you don't own a dedicated programmer). Many people find that using an ISP programmer is very challenging.

gfvalvo:
Post your complete code that won't compile.

This is the code:

pert:
You forgot to select the right board. The Adafruit USB Power Gauge uses an ATtiny85 microcontroller. The sketch is specifically written for the ATtiny85. You're trying to compile it for the ATmega2560. That will never work.

I think it would be a good idea for you to explain what you're trying to accomplish. The kit comes with the sketch pre-loaded on the ATtiny85, so the only reason you even need to mess with that sketch is if you want to customize the behavior of the power gauge. Most people will have no need for that. If you do want to customize your power gauge, you're going to need an ISP programmer (which could be a spare Arduino used as an "Arduino as ISP" if you don't own a dedicated programmer). Many people find that using an ISP programmer is very challenging.

But it has Serial output. I want to connect the gauge's serial output to the UART of the Arduino MEGA 2560 in order to read measurements of a device connected to a powerbank + USB gauge.

You don't need that sketch to read serial output from the Adafruit USB Power Gauge. The sketch you're trying to run is the firmware on the ATtiny85 of the Adafruit USB Power Gauge.

For what you are trying to accomplish, you just write your own sketch for the Mega and leave the Adafruit USB Power Gauge as it is.

Ok,I understood. Something else can I use the USB gauge to measure the Arduino MEGA 2560's power consumption while powering the Arduino from the same USB?