This project came about as I needed to measure how much power my Tricoder project was consuming so I could determine how long different batteries would last. I have used the INA219 in the past on a dual power supply that I build so I decided to give it a go on this project. The INA219 can measure voltage from 0 to 26 VDC and current from -3.2A to + 3.2A. This is a good fit for measuring the battery usage as the voltage is between 3.2 and 4.2 volts. The current being drawn will be less than 1.6 amps. The meter itself is powered over USB which also provides information to the PC through the serial interface. Data is transmitted once per second. The data may be imported into a spreadsheet program such as Excel or Numbers so that graphs may be drawn. The meter itself has a display for displaying various information such as the current voltage, current, and power consumption. It may also display the min and max values recorded.
Bill of Materials (BOM)
The cost of the project will be around $62 US but may be more or less depending on what you have on hand. If you need to purchase everything as shown in the list below, the total cost would be $104 US and you will have quite a bit in left over parts for your next project. Another way to save some money on this project is shopping around for some better prices or finding fewer sources for the parts. Just keep in mind that the more suppliers that you purchase from, the more in shipping costs you will incur which may wipe out any savings.
|Line||Part||Source||P/N||Price ea.||Qty||Price Ext.||Notes|
|1||Screw terminal – 2 pins||Adafruit||724||$0.59||2||$1.18||Pack of 5 @ $2.95|
|2||Green (555nm) LED||Adafruit||298||$0.16||1||$0.16||Pack of 25 @ $4.00|
|3||Red (633nm) LED||Adafruit||299||$0.16||1||$0.16||Pack of 25 @ $4.00|
|4||Nokia 5110 LCD||Sparkfun||LCD-10168||$9.95||1||$9.95|
|5||INA219 High Side DC Current Sensor Breakout||Adafruit||904||$9.95||1||$9.95|
|6||220 Ohm Resistor||Adafruit||2780||$0.03||3||$0.09||Pack of 25 @ $0.75|
|7||1.2k Ohm or 1k Ohm Resistor||Sparkfun||COM-13760||$0.05||1||$0.05||Pack of 20 @ $0.95|
|9||Pushbutton||Adafruit||1119||$0.25||1||$0.25||Pack of 10 @ $2.50|
|10||Teensy 3.1 or 3.2||Adafruit||2756||$19.95||1||$19.95|
|11||Perfboard||Adafruit||2670||$0.50||2||$0.99||Pack of 10 @ $4.95|
|12||4-40 3/4 inch machine screw||Amazon||B000H5QIE0||$0.04||4||$0.16||Pack of 100 @ $3.99|
|13||4-40 nut||Amazon||B001B2QP3C||$0.06||8||$0.45||Pack of 100 @ $5.63|
|14||7/16 inch spacers||Amazon||B00137SA9W||$0.09||4||$0.38||Pack of 100 @ $9.47|
|15||22 or 26 AWG Hookup Wire||Adafruit||1311||$15.95||1||$15.95|
|16||0.1″ 36-pin strip female header||Adafruit||598||$0.59||1||$0.59||Pack of 5 @ $2.95|
|17||0.1″ 36-pin strip male header||Adafruit||392||$0.50||1||$0.50||Pack of 10 @ $4.95|
- Links and Prices valid as of 2 October 2016
- All prices are in US Dollars
- The Nokia 5110 display is also available at Adafruit. The part number is 338. I had the Sparkfun unit already so that was the one I used for this project. The pinouts are different so you will need to be careful with hooking up the Adafruit unit as the pinouts do not match the order shown in the images in the Hardware Configuration section.
The project uses a Teensy 3.1 but a Teensy 3.2 will work just fine. Actually any Teensy and even some Arduino boards should work as well. If you do use a different board, make sure that you verify that the pin assignments are correct. You may also need to modify the code to use the correct pins, particularly with the display. The Teensy is a bit overkill for this project so you may want to consider using a Arduino Micro however I see that the pricing for the Arduino Micro is more than the Teensy. You may also have to modify the program if it does not fit into the memory available.
Teensy 3.1 Memory usage
Sketch uses 49,492 bytes (18%) of program storage space. Maximum is 262,144 bytes. Global variables use 5,644 bytes (8%) of dynamic memory, leaving 59,892 bytes for local variables. Maximum is 65,536 bytes.
Below is a layout of the circuit which was done using Fritzing. Fritzing is free but I am not terribly skilled at using it yet so the schematic in the second image is not done well. I have included the Fritzing project in the source code so you are free to modify it. Perhaps someone more skilled with Fritzing can make a better schematic and part for the Nokia display.
For the project, I constructed it using two perfboards connected together with some male and female headers. The top perfboard contains the LCD, terminals, switches, and leds. The bottom perfboard holds the Teensy and the INA219.
In order to verify that your wiring is correct, you should run some example programs for each piece of hardware connected to the Teensy.
- Arduino Software installed
- Teensyduino installed for your Arduino IDE Version (Current Arduino software version 1.6.12, requires installing the Teensyduino beta version 1.31.)
- Arduino Libraries installed
- Open the example “pcdtest” from the Adafruit-PCD8544-Nokia-5110-LCD-library
- Un-comment the following line of code Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
- Upload the code to the Teensy. You should see several patterns displayed on the LCD
- If the display is too light or too dark, modify the following line of code display.setContrast(50);
- Open the 01.Basics > Blink example
- Edit the following line of code by setting the pin to the red led pin, 9. int led = 13;
- Upload the code to the Teensy and verify that the red LED blinks
- Make the same change for the green LED on pin 10
- Open the Adafruit INA219 > getcurrent example
- Upload the code to the Teensy
- Once the code is uploaded, open the serial monitor. If you are seeing garbled text or nothing at all, make certain that the baud rate in the serial monitor is set to 115200 baud
- You should observe the measurement readings in the serial monitor window. If you are seeing all zero values, then there may be an issue communicating to the INA219. You may want to apply a voltage to the V+ and ground to verify that the voltage changes. It is not necessary to connect a load at this time unless you wish to verify the current readings as well
The code for this project is available on GitLab at https://gitlab.com/richteel/INA219Meter/.
The Adafruit_INA219 library provides the following readings
- Volts Bus in Volts (V)
- Volts Shunt in millivolts (mV)
- Current in milliamps (mA)
Each loop in the code, the values are summed together. The code then looks at the last time the display was updated and if at least one second has passed then the average for each is calculated, the sums and count are reset to zero, and the display is updated. Along with the averages for the three measurements, several other values are calculated.
- Volts Load in Volts (V)
- Bus(V) + (Shunt(mV)/1000)
- Power in Watts (W)
- Bus(V) x Current(mA)/1000
- Watt Hours (Wh)
- SUM(Watts(W) * Time(h))
- Time is the interval of time that the average watts was calculated. It approximates the chunk of area under the curve.
- Amp Hours (Ah)
- Watt Hours (Wh) / Nominal Voltage (V)
- Nominal Voltage is defined as 3.7 V for lithium-ion (li-ion) batteries
DISCLAIMER: I am not a fan of how the unit is working from a usability standpoint. I made use of a single push-button to do four types of actions. In order to allow switching displays, changing menu items, selecting a menu item, and turning the back-light on and off, I ended up with something which is not intuitive or easy to use. If you can think of a better behavior to accomplish the same thing, please let me know.
Understanding Bus, Shunt, and Load Voltages
Unfortunately the voltages can be a bit confusing to understand as you may think the names mean something else. This is my understanding of the voltages based on sample code and the Adafruit Learning System pages.
- Bus Voltage: The voltage seen across the load
- Shunt Voltage: The voltage across the shunt and is used to measure the current
- Load Voltage: The total voltage drop seen by the source. Load Voltage is the Bus Voltage plus the Shunt Voltage.
- Short press: Switch is pressed between half a second to less than one second
- Long Press: Switch is pressed longer than one second
- Switching displays
- All displays except the Utility display -> Short press
- Utility display -> select Exit Utility & Long Press
- Changing menu items
- Short press
- Select menu items
- Long Press
- Turn the back-light on and off
- All displays except Utility display -> Long Press
All values are average readings since the last displayed values
- B: Bus Voltage (V)
- S: Shunt voltage (mV)
- L: Load voltage (V)
- C: Current (mA)
- P: Power (W)
- Calculated from Bus Voltage times the Current
All values are based from the Power reading from the Meter display
- Time: The amount of time that the Amp-hours and Watt-hours have been calculated
- Amp Hours (mAh)
- Watt Hours (mWh)
The minimum and maximum values are taken for each reading not the averages. The code is reading the values every time in main loop so there are several reading per second. This is important to understand in the event that you look at the serial output and wonder why you do not see a reading that matches the min or max values displayed.
- MIN: Minimum Bus Voltage observed (V)
- MAX: Maximum Bus Voltage observed (V)
- MIN: Minimum Current observed (mA)
- MAX: Maximum Current observed (mA)
NOTE: Reset options will cause the serial output to send several blank lines followed by a line of text containing the headers for the data.
- Time: The amount of time that the Amp-hours and Watt-hours have been calculated
- Reset Power: Reset the timer, Amp Hours, and Watt Hours to zero
- Reset Min/Max: Reset the minimum and maximum values for bus voltage and current
- Reset Both: Resets both the Power and Min/Max values to zero
- Exit Utility: Move to the About display
Displays miscellaneous information about the software
- Red LED: Indicates that the Teensy is powered up and initialized
- Green LED: Flashes when menu options are selected in the Utility display
Serial output settings are:
- 115,200 baud rate (The rate is really at USB Speed of 12 Mbit/sec. It seems that any baud rate actually works. My guess is that the PC Driver is buffering the data so the baud rate is ignored. Perhaps Paul Stoffregen can comment. REF: Using USB Serial Communication)
- 8 data bits
- no parity
- one stop bit
Here are a few changes that one may want to make.
- Create a PCB to fit everything on one board instead of sandwiching two perfboards together
- Change the way the menu options and button function.
- One option may be to treat each menu item as a different display and simply exit once past the Reset Both option. This would allow another menu item on the screen. The only issue with that is you would need to loop around again if you intended to select a previous item in the menu
- Fit the meter into a case. Perhaps a 3-D printed case
- Add a graph to show the measurements over time. This would require storing more information in memory
- Add a SD Card to store the data so you would not need to have a serial monitor open. This could also help for creating a graphical display. The only downside is accessing an SD Card takes some time so it may not be possible to capture as many data points
- Add an additional button(s) to help make a more usable device