Introduction: Xiao Power Meter With INA219

About: 🤖

This instructable is about how to make a tiny power meter based on a INA219 current sensor. which will show the voltage, current, and wattage of connected devices. this meter is built super tiny by utilizing the small size of the Seeed Studio XIAO SAMD21.let's get started

some features of the Xiao power meter

  • Up to +26V target voltage
  • Up to ±3.2A current measurement, with ±0.8mA resolution
  • Senses Bus Voltages from 0 to 26 V

code updated

I just noticed there is a mistake in the OLED UI.it is showing i= in the power reading I just changed it to W=

Supplies

Parts

Tools

  • SMD rework station
  • soldering iron

Step 1: INA 219 Current Sensor

let's talk about how INA 219 Current Sensor works.

The INA219 Current Sensor operates based on the Hall effect principle and integrates a small onboard shunt resistor. When current passes through the shunt resistor, it generates a voltage drop, which the INA219 measures. Simultaneously, it measures the voltage across the shunt resistor. Utilizing these values and calibration, it calculates the current passing through the shunt resistor. The chip communicates these measurements via an I2C or SMBus interface to external devices, enabling accurate real-time monitoring and control of current and voltage in electronic circuits, making it a popular choice for applications requiring precise power consumption measurement.

INA 219 Current Sensor Datasheet: link

Step 2: Seeed Studio XIAO SAMD21

let's talk about Seeed Studio XIAO SAMD21.

The Seeed Studio XIAO SAMD21 is the smallest member of the Seeeduino family. It carries the powerful ATSAMD21G18A-MU which is a low-power microcontroller. On the other hand, this little board has good performance in processing but needs less power. As a matter of fact, it is designed in a tiny size and can be used for wearable devices and small projects.

Seeed Studio XIAO SAMD21 has 14 PINs, which can be used for 11 digital interfaces, 11 mock interfaces, 10 PWM interfaces (d1-d10), 1 DAC output pin D0, 1 SWD pad interface, 1 I2C interface, 1 SPI interface, 1 UART interface, Serial communication indicator (T/R), Blink light (L) through pin multiplexing. The colors of LEDs(Power,L,RX,TX) are green, yellow, blue, and blue. Moreover, Seeed Studio XIAO SAMD21 has a Type-C interface that can supply power and download code. There are two reset buttons, you can short-connect them to reset the board

Step 3: Schematic

The project is not that complicated due to the small number of components thanks to the clever design of INA219 ic . Here is the schematic of this circuit, the communication between ina219 and OLED to Xiao is done through I2c, this circuit is powered by connected USB power from Xiao, and two wire connecting terminals are used for input and output

Step 4: Pcb

This PCB is designed in easyEDA, I designed this PCB to be as small as possible thanks to the small size of Xiao

Download PCB Gerber files

Step 5: Seeed's Fusion Service

I used Seeed Studio Fusion PCBA Service, which offers comprehensive PCB manufacturing, prototype PCB assembly, and more. Their services cover everything from PCB fabrication, parts sourcing, and assembly, to testing, ensuring top-notch quality throughout the process. Furthermore, once you've gauged market interest and validated your prototype, the Seeed Co-create Program can assist you in bringing your product to market with expert guidance and a well-established network of connections.

Step 6: Pcb Assembly

for this project, I sourced all the parts for the INA219 current sensor module, I desolder all the components from that module. and solder all the components to the new PCB, in the end, solder both the OLED and wire connector

Step 7: Programming

Programing Xiao is super simple watch this video from Seeed you will get an idea about how to configure and upload sketches to Xiao.

the code

// INA219 Current Sensor with OLED Display for Arduino Uno
//
// This sketch was modified from the Adafruit INA219 library example
//
// Gadget Reboot
//
// Required Libraries
// https://github.com/adafruit/Adafruit_INA219
// https://github.com/adafruit/Adafruit_SSD1306


#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
Adafruit_INA219 ina219;


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float busvoltage = 0;
float current_mA = 0;
float power_mW = 0;

void setup() {


  // initialize ina219 with default measurement range of 32V, 2A
  ina219.begin();


  // ina219.setCalibration_32V_2A();    // set measurement range to 32V, 2A  (do not exceed 26V!)
  // ina219.setCalibration_32V_1A();    // set measurement range to 32V, 1A  (do not exceed 26V!)
  // ina219.setCalibration_16V_400mA(); // set measurement range to 16V, 400mA


  // initialize OLED display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.display();


}


void loop() {


  // read data from ina219
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();


  // show data on OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print("V");
  display.setCursor(20, 0);
  display.setTextSize(2);
  display.print(busvoltage);
  display.setTextSize(1);
  display.print("V");


  display.setCursor(0, 25);
  display.setTextSize(2);
  display.print("A");
  display.setCursor(20, 25);
  display.setTextSize(2);
  display.print(current_mA);
  display.setTextSize(1);
  display.print("mA");


  display.setCursor(0, 50);
   display.setTextSize(2);
  display.print("W");
  display.setCursor(20, 50);
   display.setTextSize(2);
  display.print(power_mW);
  display.setTextSize(1);
  display.print("mW");


  display.display();


}

Step 8: Final Result

well, it works!!!!!, I just connected a 12v DC to the input power terminal, on the output terminal, I added a 68-ohm resistor as a load. the whole circuit is powered by the USB power input from Xiao. The OLED screen will show you the voltage, current, and power in milliwatts. it is recommended to use this in below 26 volts and 3 amp. applying a higher current or voltage from the rated value will damage the

code updated

I just noticed there is a mistake in the OLED UI.it is showing i= in the power reading I just changed it to W=