Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
ElectronicsLovers
Published © CERN-OHL

DIY Power Meter project by using Arduino Pro Mini

Hello, electronics community! Today I will present you a project which lets you measure the voltage and current of an appliance.

IntermediateFull instructions provided3 hours1,169
DIY Power Meter project by using Arduino Pro Mini

Story

Read more

Code

Code snippet #1

Plain text
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_INA219 ina219;
#include "SdFat.h"
SdFat SD;

File TimeFile;
File VoltFile;
File CurFile;

unsigned long previousMillis = 0;
unsigned long interval = 100;
const int chipSelect = 10;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float energy = 0;



void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  ina219.begin();
  
  SD.begin(chipSelect);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    ina219values();
    TimeFile = SD.open("TIME.txt", FILE_WRITE);
    if (TimeFile) {
      TimeFile.println(currentMillis);
      TimeFile.close();
    }

    VoltFile = SD.open("VOLT.txt", FILE_WRITE);
    if (VoltFile) {
      VoltFile.println(loadvoltage);
      VoltFile.close();
    }

    CurFile = SD.open("CUR.txt", FILE_WRITE);
    if (CurFile) {
      CurFile.println(current_mA);
      CurFile.close();
    }
    displaydata();
  }
}

void displaydata() {
  display.clearDisplay();
  display.setTextColor(WHITE);
//  display.setTextSize(1);
  display.setFont(&FreeMono9pt7b);
  display.setCursor(0, 10);
  display.println(loadvoltage,1);
  display.setCursor(45, 10);
  display.println("V");
  

if(current_mA > 1000){
  display.setCursor(0, 25);
  display.println(current_mA/1000,2);
  display.setCursor(50, 25);
  display.println("A");
}
else{
  display.setCursor(0, 25);
  display.println(current_mA);
  display.setCursor(65, 25);
  display.println("mA");
}

if( ((loadvoltage*current_mA) > (1000-10)) ){
display.setCursor(0, 45);
  display.println(loadvoltage * current_mA/1000);
  display.setCursor(60, 45);
  display.println("W");
}
else{
  display.setCursor(0, 45);
  display.println(loadvoltage * current_mA);
  display.setCursor(85, 45);
  display.println("mW");
}
if(energy >900){
  display.setCursor(0, 60);
  display.println(energy/1000);
  display.setCursor(65, 60);
  display.println("Wh");
}
else{
   display.setCursor(0, 60);
  display.println(energy);
  display.setCursor(65, 60);
  display.println("mWh");
}
  display.display();
}

void ina219values() {
  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  energy = energy + loadvoltage * current_mA / 3600;
}

Github

https://github.com/adafruit/Adafruit_INA219

Github

https://github.com/adafruit/Adafruit-GFX-Library

Github

https://github.com/greiman/SdFat

Credits

ElectronicsLovers
8 projects • 33 followers
Electronicslovers started in July 2011 with the mission to produce ideas to students and hobbyist to learn electronics practically.
Contact

Comments

Please log in or sign up to comment.