0% found this document useful (0 votes)
19 views14 pages

Make Your Own Power MeterLogger

This document provides a detailed guide on creating a Power Meter/Logger using an Arduino, INA219 power monitor IC, OLED LCD, and Micro SD Card. It includes steps for ordering parts, creating the circuit, uploading code, and troubleshooting common issues. The project aims to offer more functionality than standard USB power meters and encourages users to modify and enhance the design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Make Your Own Power MeterLogger

This document provides a detailed guide on creating a Power Meter/Logger using an Arduino, INA219 power monitor IC, OLED LCD, and Micro SD Card. It includes steps for ordering parts, creating the circuit, uploading code, and troubleshooting common issues. The project aims to offer more functionality than standard USB power meters and encourages users to modify and enhance the design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

instructables

Make Your Own Power Meter/Logger

by GreatScottLab

In this project I will show you how I combined an Arduino, an INA219 power monitor IC, an OLED LCD and a Micro SD
Card PCB in order to create a power meter/logger that has more functions than the popular USB Power Meter. Let's get
started!

Step 1: Watch the Video!

The video gives you a good overview on how to create your own power meter. In the next steps though I will present
you some additional information to make this project even simpler.

https://youtu.be/lrugreN2K4w

Make Your Own Power Meter/Logger: Page 1


Step 2: Order Your Parts!

Here you can nd a parts list with example seller for the portable version of this project (a liate links).
Aliexpress:
1x LiPo Battery: https://s.click.aliexpress.com/e/_dZhZsnd
1x TP4056 Board: https://s.click.aliexpress.com/e/_dX8n0hp
1x Arduino Pro Mini: https://s.click.aliexpress.com/e/_d62ALdV
1x INA219 Board: https://s.click.aliexpress.com/e/_dUZvYoX
1x OLED LCD: https://s.click.aliexpress.com/e/_dWfFi0n
1x SD Card PCB: https://s.click.aliexpress.com/e/_dUy68bd
1x Switch: http://amzn.to/2gz9ZtW
Ebay:
1x TP4056 Board: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
1x Arduino Pro Mini: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
1x INA219 Board: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
1x OLED LCD: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
1x SD Card PCB: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
1x Switch: http://rover.ebay.com/rover/1/711-53200-19255-0/1?...
Amazon.de:
1x LiPo Battery: http://amzn.to/2gM2vXB
1x TP4056 Board: http://amzn.to/2eUvMNO
1x Arduino Pro Mini: http://amzn.to/2g6Ujjr
1x INA219 Board: http://amzn.to/2gM5M9v
1x OLED LCD: http://amzn.to/2g6Q3Rd
1x SD Card PCB: http://amzn.to/2gM9Zdf

Step 3: Create the Circuit!

Here you can nd the schematic for the Arduino Nano version and the portable version of this project. You can also nd
those schematic on the EasyEDA website:
https://easyeda.com/GreatScott/PowerMeter-b6051723...
https://easyeda.com/GreatScott/PortablePowerMeter-...
You can also use the pictures of my nished board as a reference for your own.

Make Your Own Power Meter/Logger: Page 2


Make Your Own Power Meter/Logger: Page 3
Make Your Own Power Meter/Logger: Page 4
Make Your Own Power Meter/Logger: Page 5
Step 4: Upload the Code!

Now that your circuit is complete, it is time to upload the code. You can download it here. But don't forget to download
and include the following libraries before uploading:
https://github.com/adafruit/Adafruit_INA219
https://github.com/adafruit/Adafruit_SSD1306
https://github.com/greiman/SdFat
You can nd an improved version of my code here: https://github.com/gilleshenrard/datalogger

Make Your Own Power Meter/Logger: Page 6


Download
https://www.instructables.com/ORIG/F29/4WER/IVO5MF59/F294WERIVO5MF59.ino

Step 5: Success!

You did it! You just created your own Power Meter/Logger

Feel free to check out my YouTube channel for more awesome projects:
http://www.youtube.com/user/greatscottlab
You can also follow me on Facebook, Twitter and Google+ for news about upcoming projects and behind the scenes
information:
https://twitter.com/GreatScottLab
https://www.facebook.com/greatscottlab

I would like to charge and discharge a battery - I have external constant current load and
constant current charger for my battery cell that I can switch in and out. My confusion is if the cell
is my load it will see current in two directions, one for charge and the other for discharge. How
can this circuit handle + and - current flow. Does the INA 219 care which direction current flows?
Does it report this out as + or = ???
Working Sketch. I spent an hour solving the OLED problem.

#include <Wire.h>
Make Your Own Power Meter/Logger: Page 7
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include "SdFat.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1
#define ROTATION 0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_INA219 ina219;
unsigned long previousMillis = 0;
unsigned long interval = 100;
const int chipSelect = 10;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
float energy = 0;
SdFat SD;
File TimeFile;
File VoltFile;
File CurFile;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay(); // Clear the display
display.display();
display.setRotation(ROTATION); // Set Rotation
display.setTextSize(1); // Set text size
display.setTextColor(WHITE);
delay(2000);
ina219.begin();
SD.begin(chipSelect);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
ina219values();
displaydata(); //subroutines display
TimeFile = SD.open("TIME.txt", FILE_WRITE);
if (TimeFile) {
TimeFile.println(currentMillis);
TimeFile.close();
}
VoltFile = SD.open("VOLT.txt", FILE_WRITE);
if (VoltFile) {
VoltFile.println(loadvoltage);
VoltFile.close();
}
CurFile = SD.open("CUR.txt", FILE_WRITE);
if (CurFile) {
Make Your Own Power Meter/Logger: Page 8
CurFile.println(current_mA);
CurFile.close();
}
}
}
void displaydata() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(loadvoltage);
display.setCursor(35, 0);
display.println("V");
display.setCursor(50, 0);
display.println(current_mA);
display.setCursor(95, 0);
display.println("mA");
display.setCursor(0, 10);
display.println(loadvoltage * current_mA);
display.setCursor(65, 10);
display.println("mW");
display.setCursor(0, 20);
display.println(energy);
display.setCursor(65, 20);
display.println("mWh");
display.display();
}
void ina219values() {
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
power_mW = ina219.getPower_mW();
energy = energy + loadvoltage * current_mA / 3600;
}
Can we use this project for home power measurement which is about 220V?

I made the project and tested with LED. The issue is the LED is blinking. Can anyone tell me
why? Is there any change in the code provided?
Everything else works fine. GreatScott, your videos teach a lot about programming arduino and it
really helps understand the coding. Thanks a ton mate!

Thanks Scott :) This Circuit is very awesome, I did some modifications to the code though,
Accidently bought OLED display with SPI instead of I2C :P

Make Your Own Power Meter/Logger: Page 9


I wanted to ask you where have you connected the V+in and V-in pin of the INA219 ?

I have connected VIN+ to 5V pin of input USB i.e. where you will connect 5V Charger and VIN- is
connected to 5V pin of output USB i.e. where you will connect device or load whose power you
want to measure.
Bro can u help me out how can you use more than greater voltage bettery cell and how u can
power up arduino?
Normally battery is 3.7v you can use DC-DC Boost Converter to Stepup battery voltage which is
3.7v to normal Arduino voltage i.e. 5v, you can find various DC-DC Boost Converters online. I
didnot use any boost circuit because arduino can also work on 3.3V so I used directly 3.7v from
battery to power arduino.
Ho do you guys are getting 5v to power the arduino on a 3.7 volts battery?

Answering myself, I read in the video's comments that Scott is directly powering the arduino from
the 4.2v battery.
Hello Scott
Sorry but I do not know how you manage to power a 5V Arduino with a 3.7 lipo battery. There
should be a boost coverter in the middle right?
Thanks for all your support
The battery, when full, is giving 4.2v I think, I read in the video's comments that Scott is directly
powering the Arduino with it.
There is a deep discharge protection. That is mendatory. The rest isn't

Added USB breakout connectors in order to have it measure consumption on USB devices. Also
added a button to have it increase the measurement number. This way, it can record multiple
measurements on the same SD card. Thank you, GreatScott, for your inspiring project.

I made it.

but ive question

shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
energy = energy + loadvoltage * current_mA / 3600;

why shuntvoltage divide by 1000 dan curren_mA is divided by 3600??

why shuntvoltage divide by 1000 :


In order to convert shuntvoltage in V you have to divide mV by 1000

Make Your Own Power Meter/Logger: Page 10


curren_mA is divided by 3600:
Energy will be calculate integrating the power with 1 sec steps. To convert energy from mWs to
mWh you have to divide by 3600 s/h .
I'm getting a compilation error:

In function 'void loop()':


sketch_oct09a:36:5: error: 'ina219values' was not declared in this scope
ina219values();

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

But, it's taken so long freaking long for get all stuff from ebay shopping :D Thanks for the projet

can this project use the arduino UNO instead of arduino Pro Mini?

I'm a programmer so when it comes to projects like this, I don't like to simply download libraries
and click a few buttons. So I decided to write my own library for my micro-controller family of
choice (PIC).
The INA219 has only 6 16-bit registers. Four of which are read-only. So it's a pretty simple
device. It can be used out-of-the-box like in this video but you lose precision and possibly
resolution since it's not calibrated (precision) and you use the whole 3.2A range (as in this case
with a 0.1Ohm shunt resistor). So if you never plan to read currents above, say 1A, you lose
resolution since the chip can be programmed to handle any range between 0 (I don't know if
there's a minimum) and 3.2A. Think of it as your Vref+ for an ADC.
So, bottom line, yes it can definitely be done with a UNO. The device is just like any I2C devices
out there, i.e. NOT tied to Anyduino in any way.
Edit: I forgot to add that out-of-the-box, the readings fluctuate a lot. But you can change the mode
of operation to include averaging over up to 128 samples. Something also not covered in this
video. Here's a picture showing it and also the OLED display hooked to a PIC measuring the
current of another breadboard.

I'm interested in the code you have to take average values over 128 readins. I like your idea and
am new to Arduino coding.
The average is simply the sum of 128 readings, divided by 128. But if you want the average of
the last 128 readings, then it's a little different. Two key points: First, you need a buffer size that is
a power of 2. So ADC_AVG_BUFFER_SIZE should be 2, 4, 8, 16, 32, 64 etc. Second, the
variable type for the tally and buffer should be adapted to your needs. Use uint16_t for the buffer
if your sample values are larger than 255 and use a uint32_t for the tally if the sum can be larger
than 65535

Variables you'll need:


Make Your Own Power Meter/Logger: Page 11
#define ADC_AVG_BUFFER_SIZE 128
uint8_t ADCAvgBuffer[ADC_AVG_BUFFER_SIZE];
uint8_t ADCAvgBufferIndex;
uint16_t ADCAvgTally;

The code after a read:

ADCAvgTally -= ADCAvgBuffer[ADCAvgBufferIndex];
ADCAvgTally += NewValue;
ADCAvgBuffer[ADCAvgBufferIndex] = NewValue;
ADCAvgBufferIndex++;
ADCAvgBufferIndex &= (ADC_AVG_BUFFER_SIZE - 1); // Make sure the index doesn't go above
our buffer size
AverageValue = ADCAvgTally / ADC_AVG_BUFFER_SIZE;

One last lesson: The division will be optimized by the compiler into a 'shift.' Dividing by a power of
2 in binary is like dividing by a power of 10 in decimal. To divide 1000 by 10 you simply shift to
the right by 1. In binary it is the same. To divide by 128 is simply to shift by 7 positions. That is
why this routine is very efficient. It is all very basic operations (add, subtract, increment, shift,
etc.) that costs very little CPU time and using a larger buffer only increases the memory used.
Using larger variable types will increase CPU time though.

Enjoy!
Hi there, nice project. I'm curious as to what the lowest measured current is possible on this
project and what the accuracy is?
Check the INA219 datasheet.

Done what you've suggested, datasheet gives ADC resolution, and accuracy (fair enough), but
doesn't really answer the first part of my question. I'd like to know with regards to the way this is
setup, what is the lowest current you have managed to measure? A approximate idea would be
very helpful.
Hello,

Full scale shunt voltage is 40mV (for PGA /1). Depending on the Rshunt value you select, you
might get quite low on measured current...
is it normal that i have in voltage 32,72 Volts ??

GreatScottLab Thanks for sharing

shown in the figure, is used to measure the power of the LED light

Hi Great Scott and all,


WHen compiling code I get this error. I am using Pro Mini, Arduino IDE 1.6.13 and the librabies
that are listed on this site.
Any help would be appreciated.
Sketch uses 22,302 bytes (72%) of program storage space. Maximum is 30,720 bytes.
Global variables use 2,216 bytes (108%) of dynamic memory, leaving -168 bytes for local
Make Your Own Power Meter/Logger: Page 12
variables. Maximum is 2,048 bytes.
processing.app.debug.RunnerException: Not enough memory; see
http://www.arduino.cc/en/Guide/Troubleshooting#si... for tips on reducing your footprint.
A bit late, I know, but I was having the same issue and realised that the screen uses up a lot of
memory. I'd previously updated Adafruit_SSD1306.h to select the 128x64 screen, but I notice
Scott has the 128x32 screen configured - this uses half the memory and allowed the program to
compile. With only 2K of SRAM to work with you have tp save space where you can I suppose.
Hi Nathan
All working now, thanks for your support.
Regards
Lofty
If anyone is running out of memory while compiling, make sure to tell the OLED library that you're
using a 128x32 pixel screen - you get the double-size text but it only uses 512 bytes SRAM
compared to 1024 bytes used by the 128x64 configuration.

This is achieved by editing the Adafruit_SSD1306.h library file (assuming you're using that
library) and ensuring it is configured for 128x32 by defining SSD1306_128_32 instead of
SSD1306_128_64.
Hi Nathan,
Brilliant thanks for update.
I'll go back to this one and try again.
Great project! I'm building my one as of this moment.
I just have 3 questions to make:
- Does the circuit functions well when the battery level gets lower? Would it be better too use a
booster to push the voltage to somewhere around 5V?
- How would you implement a battery level checker?
Great Project i've put some extras 9,5 and 3,3volts out im missing some screw terminals and few
regulators but it works like charm :D

Hi Scott,
Thank you very much for your DIY presentations. and can you explain how to convert this for AC
(Home power monitoring)
What should I follow if I use a power consumption of 48V or maybe 72V?

fantastic

WOw! you have read my mind! its just what i will need for a new project, will post my build soon.
Thanks a LOT! keep posting good ideias on Instructables
The video is such a great addition to this build. Congratulations with this instructional article!

If you view your log files how do you know what time a current ( for example ) is logged? Much
better to put all 3 values in one file. What do you think?

Make Your Own Power Meter/Logger: Page 13


LOL! I see you've already published this from your YouTube video. already liked it!

I always favorite my own projects. What would TP ( Twitter President ) do?

Standart charging board is heating too much in beginnig of charging of battery (default current is
1A). In your cinfiguration it's too dangerous:
- big temperature from charging board can demage battery - set lower charging current
- move charging board from battery and add heatsink on IC TP4056

In your board it is Resistor R3.

I love your projects so much it would mean the world to me if you subscribed to my page and
website:) Jackshow2s
Could you please, recheck the .ino file you uploaded. It is completely different. It's how to connect
a led and control it via ir. Thank you

Make Your Own Power Meter/Logger: Page 14

You might also like