I have another project in progress - who doesn't? - that will be battery operated.
An important part of battery operated things is to verify the actual energy consumption. Of course you plan it, do calculations, but you should verify your theory. And in some cases, you just don't know for sure, because you don't have enough information...
Grabbing your multimeter is what most of us does. But multimeters are not good at capturing short spikes, and if you care about power consumption, you want to send your device to sleep as often as possible.
Or if you have an oscilloscope, you can measure the voltage drop across a resistor to measure the current. But not everybody has a scope, and setup takes some time and effort. And now you are tied to your workbench.
Of course there are specialized instruments, but those are not cheap for a hobbyist, and tied to a computer & workbench as well.
An alternative can be a USB power meter, those are useful, and if operated from a power bank, can be used away from your workbench. Some even have bluetooth and you can log the data. But I'm not sure about their accuracy in low current ranges, and behaviour with varying currents.
The planSo, to the point: let's create an instrument that is good enough for hobby purposes, cheap enough to make, and has some sort of radio so you can move around with the device you developing and your laptop!
PartsSo, now I need some parts to measure the voltage / current / power consumption, do some averaging, and send the values to my computer.
Measurements - INA226I will use an INA226 to measure voltage, current and power. It's pretty similar to the more well known INA219, just slightly better specs.
The nice thing about this IC is that it has configurable conversion time, and can measure voltage and current, calculate power usage, and do averaging by itself!
Slightly more expensive than INA219, but it's available on breakout boards from the usual sellers (Ebay, Aliexpress, etc), so relatively cheap and easy to get one. If you wait a couple of weeks...
I use an Espruino MDBT42Q module as the processor and radio.
Why Espruino? Well, because it's awesome! And this is an east-european awesome, so pump it up a bit, if you live in the US!
It's based on Nordic Semiconductor's nRF52832 chip, so it has built-in Bluetooth Low energy. That means the data transfer is solved. And also I can wirelessly tweak the running program! Iterating on your code interactively in a REPL (read-eval-print-loop) is just really-really cool.
And low power. Can run on two AAs for half eternity...
If you used or heard of a Bus Pirate, well, an Espruino can act a lot like that too: a scriptable tool for exploration & hacking around. That has a built-in radio. And can run code autonomously! So pretty cool!
Communication - Web BluetoothThese Espruinos are based on a Bluetooth Low Energy chip, and with Web Bluetooth you can connect to the directly from your browser!
The circuit just sits on your desk, open an html page, and connect to. That's it. No cables, no ground loops, no need for USB isolator :)
Small hardware modificationThere is one issue with the commonly available INA226 modules: The current sense shunt is usually 0.1 or 0.01 ohm.
Looking at the datasheet, the maximum voltage across the shunt is around +-80mV. Doing a bit of math, that means a 0.1 ohm shunt yields ~800mA range, a 0.01 ohm shunt yields ~8 A range. But that's a bit toooo much. So took my soldering iron, removed the 0.1 ohm shunt, and replaced it with a 1 ohm resistor:
Ok, that resistor was never meant to be surface mounted, but works well enough.
Now with that 1ohm resistor the current range is +-81mA. More than enough for my current needs. And since the modules are so cheap, I can just replace the whole module with one that has 0.1 or 0.01 ohm shunt!
Aside: radio choiceMy criteria for the radio:
- Fast enough - to push voltage, current, power data with 20-50ms resolution
- Low power - don't want to carry mains or a USB power bank to use this tool
- Range - a couple of meters is enough
- Minimal setup
I think Bluetooth Low Energy is pretty good for these needs:
Wifi consumes a lot of energy. This solution needs ~3mA actively transmitting. That's around 100 hours with two AA batteries or an 18650 LiIon battery.
ISM radios or LoRa needs extra hardware on the receiving end.
Circuit - on a breadboardThe circuit is pretty simple:
- Power the Espruino and the INA226 - 2xAA cells
- Connect SDA, SCL, Alert pins of the INA226 to Espruino
- Connect the device you want to measure - VBUS, IN+, IN- pins of the breakout board
And since these INA226 modules are pretty cheap, you can just exchange the module with one that has a different resistance value:
- 1 ohm ~ 80mA range - covers most low-power embedded systems
- 0.1 ohm ~ 800mA range - good for ESP8266, ESP32, Pi0, small robots
- 0.01 ohm ~ 8A range - Almost anything I can come up :)
It was simple to start, just followed the INA226 module sample code, and got results π
A couple lines of configuration:
ina = new INA226(i2c, {
average: 16,// default conversion time: 1.1ms * 2 (Vbus & Shunt) => 454,5455 measurement/sec;
shunt: 1, // 1 ohm shunt
maxCurrent: 0.082
});
The INA has an interrupt output pin, so we can watch that pin (connected to D4
), and read the values and send through Bluetooth continuously as soon as data is available.
I did one "tweak" to the code: it reads more data than I need, and return values in Amperes and Watts. Since I deal with tiny embedded things, miliwats and miliamperes are more adequate, so just added my function:
INA226.prototype.r3 = function() {
return {
u: this.rd(0x02) * 0.00125,
p: this.rd(0x03) * 25000 * this.currentLSB,
i: this.rds(0x04) * 1000 * this.currentLSB,
o: (this.rd(0x06) & 4) !== 0
};
};
It was pretty straight forward, the "serialization" part was a bit tricky, but got help on the Espruino forum. It's just ~50 lines of code if you ignore comments.
Code - Browser sideThis is where the heavy lifting is done and things got a bit more complicated. I was really really happy when I got to the first meaningful graph:
No markers and no average values yet, but immediately showed some useful information. And from now on, could tweak the software real time.
Of course had to jump a couple of hoops, before getting to a good solution. So, here is how the current version looks like:
Measurements are pushed from the Espruino 28 times a second.
Voltage, current and power graph is ploted with Plotly JS, using it's WebGL rendering engine.
The table on top shows the latest - "real time" - value, and the bottom two rows show average values of the currently visible and selected areas.
Zooming, panning, selecting is handled with either Plotly's built-in functionality or using it's events. Plotly's events are not working exactly as I would like them to work, but there must be a good reason to it...
WebGL is needed, because SVG is just not fast enough to display tens of thousands of data points in real time :) But works nicely on my laptop without a dedicated GPU!
Take a look at a "demo" - hosted on Github pages - where you can try it with random data! Or if you have an Espruino and an INA226, you can use it there directly.
Conclusion?It was a fun project, finding out what's possible with a tiny microcontroller that's running javascript + a modern web browser.
Getting updates 28 times a second? Showing tens of thousands of points? Calculating averages on the fly? Logging almost 200000 points because I forgot to stop it? No problem :)
Of course there are dedicated tools with better performance, and would be interesting to compare this with a more expensive tool. I think I already know some possible weak spot, but so far it's been pretty useful!
There are still some rough edges, but the code is up at Github. Take a look at it!
Comments