Gokux
Published © GPL3+

Xiao Power Meter With INA219

This blog is about how to make a tiny power meter based on a INA219 current sensor. which will show the voltage, current, and wattag

BeginnerProtip4 hours792
Xiao Power Meter With INA219

Things used in this project

Hardware components

Seeed Studio XIAO SAMD21 (Pre-Soldered) - Seeeduino XIAO
Seeed Studio XIAO SAMD21 (Pre-Soldered) - Seeeduino XIAO
×1
oled
×1
INA 219
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Rework Station Kit, SMD
Rework Station Kit, SMD

Story

Read more

Code

Untitled file

Arduino
// 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();


}

Credits

Gokux

Gokux

20 projects • 12 followers

Comments