Software
Once the hardware is assembled you're ready to install the Arduino sketch that drives the power meter. Make sure you've setup the Arduino IDE to program Pro Trinket and can successfully write sketches (like the blink sketch) to the device.
Next make sure you have the following Arduino libraries installed:
It's easiest to install these libraries using the library manager in the latest versions of Arduino (1.6.4+), however you can also manually install libraries too.
Once the libraries are installed download the power meter Arduino sketch from its home on GitHub by clicking the button below:
Open the zip file to extract the Pro_Trinket_Power_Meter Arduino sketch and open it in the Arduino IDE.
At the top of the sketch are a few #define configuration values you can change:
- OLED_RESET - This controls what pin is connected to the reset/RST pin on the OLED display. Keep this the default 4 if you've wired your hardware as described in this guide.
- ROTATION - This controls the rotation of the OLED display. By default the value is 2 which means the display is rotated 180 degrees so that it's readable when the display is upside down.
- MODE_SWITCH - This control how long the meter will display a set of measurements and defaults to 5 seconds. You can increase or decrease this to change how long the meter displays measurements.
One other part of the sketch you might consider changing is the calibration of the INA219 sensor. Scroll down to the setup function and notice this commented note:
// Setup the INA219. ina219.begin(); // By default the INA219 will be calibrated with a range of 32V, 2A. // However uncomment one of the below to change the range. A smaller // range can't measure as large of values but will measure with slightly // better precision. //ina219.setCalibration_32V_1A(); //ina219.setCalibration_16V_400mA();
Like the comments mention the INA219 is configured by default to accept a range of 32 volts & 2 amps of power. You can uncomment one of the setCalibration lines to change this range, like changing to 32 volts & 1 amp or 16 volts & 400mA. When the range of values is reduced the precision of the meter increases so it's a trade-off of a wider range vs. more precision in the measurements.
Once the sketch is configured you're ready to program the Pro Trinket. Follow the same steps as programming the Blink example sketch to program the Pro Trinket with the power meter sketch.
After the sketch is programmed you should see the OLED display power on and display readings like the photo below shows.
If you don't see the OLED turn on, carefully check all of the components are wired together correctly. Also try troubleshooting each component individually by loading examples from its library (for example try running an Adafruit SSD1306 library example to make sure the OLED is wired correctly and working).
Usage
To use the power meter you'll need to wire the INA219 in series ahead of the power supplied to a device. Make sure to read the INA219 guide as it describes the basic usage of the sensor and how it is wired to a device to measure its current and voltage.
For example to measure the power usage of a LED connected to batteries see the diagram below:
In this diagram a small battery holder (4x AA) has its positive power output connected to the Vin+ connection on the INA219. Then the Vin- connection on the INA219 is connected to the anode (longer leg) of the LED. In this way the INA219 is in series with the power to the LED and it can measure how much current is consumed by the LED.
In addition to being in series with the LED a wire connects the battery ground to the power meter ground. This allows the power meter to read the voltage from the batteries.
Finally to complete the circuit a small resistor (~560 ohm) is connected from the cathode (shorter leg) of the LED to battery ground.
Try wiring up a LED and battery as shown, then turn on the power to the batteries. Make sure the power meter's Pro Trinket has power too as it will not power itself from the measured device's power source (you can instead plug in the Pro Trinket's micro USB connector to a power supply/computer, or even power it with batteries).
You should see the meter display different power measurements. Every 5 seconds the display will change to show a new set of values, and in total there are 4 values that are displayed:
- Volts & Amps - This mode shows the volts (or millivolts) of the power supply at the top, and the current consumed by the device in amps (or milliamps) at the bottom. For example for an LED and 4 batteries you might see:
- V: 6.00
- mA: 7.10
- Watts & Amp-hours - This mode shows the power at the top in watts (or milliwatts), and the observed amp-hours (or milliamp-hours) since the power meter has been powered on. For the LED you might see values to start like:
- mW: 42.86
- mAh: 0.0
The amp-hour measurement is interesting because it's showing how much current has been consumed by the device since the power meter was turned on. To start the amp-hours will be around 0, but over time they will grow based on how much current a device uses.
This is a very useful measurement to understand the battery life of a device since batteries are measured in amp-hours (or more typically milliamp-hours). If you observe the millamp-hours of current used by a device you can compare it to the milliamp-hour capacities of batteries and estimate the battery life of the device. Check out this guide on batteries for more great information.
Since a LED draws a constant amount of current, after an hour you should see a milliamp-hour value that's about equal to the average current usage of the LED. In this example we'd expect to see about 7.1 milliamp-hours after one hour of measurements.
Note that all the measurements are updated once a second, so devices with quickly changing power demands might draw spikes of current that the meter does not detect.
That's all there is to using the power meter! You can use this project to measure the power consumption of devices you build, and even estimate how long they might run on batteries. Some interesting ways you might extend the project include:
- Design and build a small 3D printed enclosure to hold the Pro Trinket, OLED display, INA219, and a battery to power them.
- Add buttons to allow toggling the displayed measurements.
- Switch to a larger display that can show more measurements on screen at a time.
- Add new modes to show measurements like the minimum, maximum, and average observed voltage & current values.
Text editor powered by tinymce.