Build an Arduino Power Meter with an OLED Screen (DC Version)

Controlling the electrical consumption in your home is one of the most important thing you can do, both because of environmental concerns & to reduce the electricity bill at the end of the month. There are countless of electrical power meters out there, but in this article, I’ll show you how to build your own, and to use Arduino to measure how much power a single device is using. Note that this article is about measuring power for DC (Direct Current) devices only.

In several of the articles I published on this website, I show you how to send measurements to a remote server and to have them displayed there. Here, we are going to do something different: we are going to measure the power used by a device, and then display it right next to the Arduino board on a small OLED screen. This way, you’ll be able to build your own power meter based on Arduino, that is completely independent from any external components. Let’s start!

Hardware & Software Requirements

Let’s first see what we need to build this project. As the center of the project, I used an Arduino Uno board, but you can of course use any other Arduino board here.

To measure the voltage & current of the measured device, I used an Adafruit breakout board for the INA219 sensor. To display the data, I used the Adafruit 128×64 OLED screen, as it has a very easy to use Arduino library.

As a test device, I will use a simple LED, along with a 330 Ohm resistor. But this could also be any DC device you are using in your home, for example a strip of LEDs.

Of course, you will need the usual breadboard & jumper wires to make the necessary connections.

This is the list of required components for this project:

On the software side, you will need the latest version of the Arduino IDE, that you can get from:

https://www.arduino.cc/en/Main/Software

You will also need two libraries, that you can install from the Arduino library manager:

  • Adafruit_INA219
  • Adafruit_SSD1306

Hardware Configuration

Let’s now see how to assemble the hardware for this project. As there are a lot of connections to do, I created a schematic to help you out:


For the INA219 sensor, you first need to connect the power (VCC & GND) to the Arduino Uno power. For convenience, I connected the Arduino 5V & GND pins to the two power rails of the breadboard. Then, you need to connect the SCL pin to Arduino pin A5, and the SDA pin to Arduino pin A5. Don’t worry for now about the power connections, we’ll take care of them later.

For the OLED screen, you will also need to connect the screen breakout board to the power rails (Vin & GND pins). Then, for the SDA & SCL pins (sometimes called Data and Clock), you can simply connect them to the same pins on the INA219 board. Finally, connect the CS pin to Arduino pin 4.

Finally, for the LED, we need to make the current of the LED ‘flow’ through the INA219 sensor, so it can be measured. To do so, first connect pin 7 of the Arduino board to the Vin+ pin of the sensor. Then, connect the Vin- pin of the sensor to the resistor, in series with the anode of the LED. Finally, connect the other pin of the LED to the ground.

This is the final result:


Measuring Power Consumption with Arduino

We are now going to see how to configure the Arduino board so it measures the electrical consumption of the LED, and displays it on the OLED screen.

We start by importing the required libraries:

#include 

Then, we create an instance of the INA219 sensor:

Adafruit_INA219 ina219;

Next, we create an instance of the OLED screen:

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

Inside the setup() function of the sketch, we initialise the sensor & the OLED screen:

// Init INA219
ina219.begin();
ina219.setCalibration_16V_400mA();

// Init OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  

// Display welcome message
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,26);
display.println("Ready!");
display.display();
delay(1000);

// LED
pinMode(7, OUTPUT);

Note that we also set pin 7 as an output, as we’ll use it to turn the LED on & off.

Now, inside the loop() function of the sketch, we are going to alternate between setting the LED off & on, and each time we’ll measure the current and the power flowing used by the LED. For example, this is the part to measure the power when the LED is off:

// LED OFF
digitalWrite(7, LOW);

// Measure
current_mA = measureCurrent();
power_mW = measurePower();

// Display data
displayData(current_mA, power_mW);
delay(2000);

As we can see, we are calling some functions here. This is the function used to measure the power consumption of the LED:

float measurePower() {

  // Measure
  float shuntvoltage = ina219.getShuntVoltage_mV();
  float busvoltage = ina219.getBusVoltage_V();
  float current_mA = ina219.getCurrent_mA();
  float loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.println("");

  // If negative, set to zero
  if (current_mA < 0) {
    current_mA = 0.0; 
  }
 
  return current_mA * loadvoltage;
  
}

We also use a function to display the data on the display:

void displayData(float current, float power) {

  // Clear
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);

  // Current
  display.setCursor(0,0);
  display.println("Current: ");
  display.print(current);
  display.println(" mA");

  // Power
  display.println("Power: ");
  display.print(power);
  display.println(" mW");

  // Displays
  display.display();
  
}

You can of course grab the complete code from the GitHub repository of the project:

https://github.com/openhomeautomation/power-meter-oled

It’s now time to finally test the project! Grab the whole code, open it into your Arduino IDE, and upload the code to the board. You should first see that the LED is off, and also see the readings on the OLED screen:


It makes complete sense that the current & power used by the LED are null, as the LED is off.

After a few seconds, the LED should then turn on, and you should instantly see the current & power used by the LED:


Congratulations, you just built a DC power meter using Arduino, that can display measurements on an OLED screen!

How to Go Further

In this article, you learned how to build a power meter based on Arduino, using an OLED screen to display the measured power (and current). You can now of course take what you learned in this article and use it in several other projects. You can for example use this project on several devices in your home, and have them all display the current power used by the device. You can also use this project and add some wireless connectivity to it, for example to send measurements to a cloud platform like aREST.

What do you think about this tutorial? Have you also built a power meter to measure electrical consumption from devices in your home? Don’t hesitate to share below!