The previous article mentioned that the “Disco-L053” board includes a complete current measuring circuit, which can handle a range of nearly six decades. So here is a brief description of how to actually use it that way.

First off, on the hardware side, two minor adjustments need to be made to the board:

  1. Connecting the L053’s TX pin to the on-board ST-Link.
  2. Connecting the Device Under Test to the board.

1. Connecting TX

The L053 is going to send its measurements over serial, pin PA9 to be precise. This pin is not connected to the ST-Link by default, but there are two solder jumpers on the board to connect the TX and RX pins:

The essential solder jumper is “SB2”, which can be connected by placing a dab of solder on the two arrow-shaped exposed pads. The top pad is SB2 and connects the L053’s TX output (PA9) to ST-Link’s RX input. The bottom pad can optionally be connected as well, that’s SB1 and it connects the L053’s RX input (PA10) to ST-Link’s TX output.

Another (less permanent) way to make this connection, is to add a 2-pin male header to the boxed “JP1” area, and use a female-to-female solder jumper to connect the top pin to the pin marked “PA9”, which is located on the left-side 25-pin header of the Disco-L053.

Serial output from the L053 will now be sent to USB via the ST-Link. The ePaper display is not very useful here, as it’s quite slow to refresh and constantly flashes when written to.

2. Connecting the DUT

The second step is to attach the circuit we wish to measure. The “JP4” jumper needs to be on the top two pins, while the bottom pin has the metered 3.3V supply:

Simply connect the DUT between this supply pin and ground. For a first test, I hooked up a resistor bank, allowing me to control the current draw and then verify each reading:

This allows me to vary the load down to 11 MΩ, which corresponds to a current of 300 nA. Note that since the maximum current is 50 mA, the resistance has to be at least 66 Ω.

Software

I’ve rewritten the code example from Matt Mielke, as mentioned in the previous article, to work with my own JeeH library in C++. The code can be found on git.jeelabs.org. If you have PlatformIO installed, build + upload is a matter of typing “pio run -t upload”.

This code sets everything up and continuously measures and reports the current draw, in nanoamps. The first number on each line is the last 4 digits of the time, in milliseconds:

#include <jee.h>
#include "mfx.h"

UartBufDev< PinA<9>, PinA<10> > console;

int printf(const char* fmt, ...) {
    va_list ap; va_start(ap, fmt); veprintf(console.putc, fmt, ap); va_end(ap);
    return 0;
}

PinA<5> led;
I2cBus< PinB<9>, PinB<8> > bus;
MFX< decltype(bus) > mfx;

int main() {
    console.init();
    enableSysTick();
    led.mode(Pinmode::out);
    mfx.init();

    while (true) {
        printf("%04d: %d\n", ticks % 10000, mfx.measure());
        led.toggle();
    }
}

The actual current sampling and readout logic is in a separate src/mfx.h header file.

Caveat

The Disco-L053’s current measurement uses auto-ranging, which means that larger shunt resistor values will be used when the current drawn gets lower. This leads to much better accuracy on the low ranges, but also means that the 3.3V supply will “collapse” if the DUT generates sudden current surges. In the case of a µC coming out of low-power sleep, this may result in spurious resets, so be prepared to see occasional current reading glitches.

As I’ve said before, this setup is meant for static current measurements, such as putting your µC into some stable low-power mode for perhaps one second, and then collecting a few consistent readings from the Disco-L053.