The first reading
Four jumpers from the header to an ESP32-S3, then the rail through the two terminals, and a sketch that prints five columns twice a second: the two voltages the chip measures, the current and power it works out, and the supply's own voltage. The one wire to get right is SCL, the third pin.
Six connections, in order
Ground first, then the chip's supply, then the clock and the data, and the rail last. The order is for the screwdriver: the last two connections are made inside a terminal, and the rail should not be live while a tool is in one.
The third wire is the one to watch. SCL goes to GPIO 9 and it is the third pin; SDA, the fourth, goes to GPIO 8. Those are the ESP32-S3's default I²C pins, and the ones every TinkerBlock I²C book here uses.
For a first load, anything with a known current is ideal: a resistor across a 5 V supply, an LED strip, a small fan. It makes the numbers easy to check.
What it prints
Five columns, twice a second: the shunt voltage in millivolts, the bus voltage in volts, the current in milliamps, the power in milliwatts, and the supply's voltage.
The first two are the chip's measurements and the next two its arithmetic. The last is the sketch's own: the bus voltage is read after the shunt, so adding the shunt's drop back on gives what POWER + is receiving.
Watching them together is the quickest way to see what the block does. Turn a load on and the shunt voltage rises, the bus voltage falls by the same amount, and the supply column stays where it was.
Before the first load
Run it once with nothing in LOAD. The current column will not read exactly
zero: the chip's zero error is up to 2 mA on this range, and usually far less.
Put the number into ZERO_MA and upload again.
That is the only correction that makes sense to do by hand. The rest are percentages, and How far off a reading is explains them.
The code
Prints the shunt voltage, the bus voltage, the current, the power and the supply voltage, twice a second. The first two are measured, the next two are worked out by the chip, and the last is the bus voltage with the shunt's drop added back.
// TK119 INA219: the first reading, on an ESP32-S3.
//
// Wiring, TK119 header left to right (parts up, pins down):
// GND -> ESP32 GND
// 3V3 -> ESP32 3V3
// SCL -> GPIO9 (the third pin: the clock comes first)
// SDA -> GPIO8
// Then the rail, last: supply + to POWER +, supply - to
// POWER -, LOAD + to the load's +, LOAD - to the load's -.
//
// Arduino IDE: Tools > Board > esp32 > ESP32S3 Dev Module,
// Tools > USB CDC On Boot > Enabled, then Tools > Port.
// Library Manager: install "Adafruit INA219" (it brings
// Adafruit BusIO). Serial Monitor at 115200.
#include <Wire.h>
#include <Adafruit_INA219.h>
const int SDA_PIN = 8, SCL_PIN = 9;
// 0x40 with both pads open; 0x41 A0, 0x44 A1, 0x45 both.
Adafruit_INA219 ina(0x40);
// The current with nothing in LOAD, in mA: the chip's zero
// error. Read it once and put it here.
const float ZERO_MA = 0.0;
void setup() {
Serial.begin(115200);
delay(500);
Wire.begin(SDA_PIN, SCL_PIN);
if (!ina.begin(&Wire)) {
Serial.println("No INA219 at 0x40: is SCL the third pin?");
while (true) delay(1000);
}
// Gain /8: up to 3.2 A, 0.1 mA a count. The default.
ina.setCalibration_32V_2A();
Serial.println("shunt_mV bus_V mA mW supply_V");
}
void loop() {
float shunt = ina.getShuntVoltage_mV(); // measured
float bus = ina.getBusVoltage_V(); // measured, LOAD +
float ma = ina.getCurrent_mA() - ZERO_MA; // worked out
float mw = ina.getPower_mW(); // worked out
float supply = bus + shunt / 1000; // at POWER +
Serial.printf("%.2f %.3f %.1f %.0f %.3f\n",
shunt, bus, ma, mw, supply);
delay(500);
}If it stops at No INA219 at 0x40, swap SCL and SDA before anything else: on this block the clock is the third pin. Then check the red LED is lit. On a classic ESP32, change the two pins to 21 and 22; the rest is the same.
When it does not work
Check SCL and SDA first: SCL is the third pin on this block and goes to GPIO 9, SDA the fourth and goes to GPIO 8. Then check the red LED, which proves GND and 3V3. If a pad on the back has been bridged, change the address in the sketch.
The bus is fine and the rail is not. Either the supply is not in POWER, or a screw has closed on the wire's insulation rather than its copper, or the load has another path back to the supply that misses the block. Tug each wire in its terminal before looking further.
That is the chip's zero error, up to 2 mA on this range. Leave LOAD empty, note the current column, and put that number in ZERO_MA. It is steady, so subtracting it works.
On an ESP32-S3 the sketch prints over the USB port only with Tools > USB CDC On Boot set to Enabled. Set it, upload again, and open the monitor at 115200. On a board with two USB sockets, use the one marked USB rather than UART, or set the option off and use the UART one.
Edit this page — content/books/ina219/first-reading.mdx
Questions about this product
See what other owners have asked, and read their solutions.
TK119 INA219 Current and Voltage Monitor
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.