Always wondered how to know how much power a given component in your house is using ? This is exactly what I will show you how to do in this tutorial, using the Arduino board and a special sensor. In this tutorial, I will show you the basics of measuring energy consumption of components that uses DC current only (which means a current that doesn’t change over time), and in an upcoming tutorial I will show how to adapt this to measure the energy consumption of any electrical component in your home. We will start with a very simple project: I will show you how to measure the power that goes into a single LED that is powered by your Arduino board.

Hardware Requirements

You need an Arduino board for this tutorial. I will again use the Arduino Uno R3 board. You will also need the INA219 breakout board, which contains the INA219B chip that we will use to measure the energy consumption in this whole tutorial. This is a very convenient chip to use, as it has an I2C interface, and libraries already exist so it is very easy to interface it with the Arduino board.

You will also need a LED, a 470 ohms resistor, a bunch of jumper wires, and a breadboard.

Software Requirements

You need nothing more than the Arduino IDE

Hardware Configuration

The hardware configuration for the INA219 sensor is a bit complicated, so be with me. First, we need to place all components on the breadboard. The idea here is that to measure the current flowing through the led, we need to insert the INA219 sensor in the loop of what would we the “normal” circuit. For this reason, connect the pin number 2 of the Arduino board to one pin of the resistor, like you would normally do to light up a LED. Then, we need to “break” the circuit: connect the other side to the resistor to the Vin+ pin of the INA219, and the Vin- pin to the anode of the LED (the longest pin). Finally, connect the cathode of the LED to the ground.

It’s not over yet, as we still have to connect the INA219 to the power and to the I2C bus. Connect the Vcc and GND pins of the sensor to the 5V and GND pins of the Arduino board, respectively. Also, connect the SDA and SCL pins to the corresponding pins of the Arduino board. Here is the final schematic:

Power monitoring with an Arduino board and the INA219 sensor

Testing individual components

Before measuring anything, we want to make sure that we can access our sensor via the I2C bus. Communicating with an I2C device can be quite tricky, but luckily for us there is a library available that was developed by Adafruit. You can download it at the following address:

https://github.com/adafruit/Adafruit_INA219

Unzip all the files, rename the folder to Adafruit_INA219, and put the folder into the libraries folder of your Arduino folder (or create the libraries folder if it doesn’t exist already). You can then test the following sketch:

#include 
#include 

Adafruit_INA219 ina219;

void setup()
{
 uint32_t currentFrequency;

 Serial.begin(9600);

 Serial.println("Communication test with with INA219 ...");
 ina219.begin();
}

void loop()
{
 float shuntvoltage = 0;

shuntvoltage = ina219.getShuntVoltage_mV();

 Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
 Serial.println("");

delay(100);
}

We first include the right libraries and create the instance corresponding to the sensor:

#include 
#include 

Adafruit_INA219 ina219;

Then, we initialize the sensor with:

ina219.begin();

Finally, we just test the sensor by reading a single value, here the shunt voltage:

float shuntvoltage = 0;
shuntvoltage = ina219.getShuntVoltage_mV();

Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.println("");

You can now upload the sketch to the board, and see what it is doing in the serial monitor. At this point, the LED is not powered, so it should just return 0 as the shunt voltage readout.

Also, please make sure that your LED is working. If you don’t know how to do that, please check my tutorial for beginners in home automation.

Putting it all together

Now that the sensor is working correctly, we can measure more values from the sensor, like the current flowing through it, and the power consumption. To actually measure something different from 0, we need to inject current into the LED via a digitalWrite(). This is the final sketch for this part:

#include 
#include 

Adafruit_INA219 ina219;

void setup()
{
 uint32_t currentFrequency;

 Serial.begin(9600);
 Serial.println("Hello!");

 Serial.println("Measuring voltage and current with INA219 ...");
 ina219.begin();
 pinMode(2,OUTPUT);
}

void loop()
{
 digitalWrite(2,HIGH);
 float shuntvoltage = 0;
 float busvoltage = 0;
 float current_mA = 0;
 float loadvoltage = 0;
 float power = 0;

 shuntvoltage = ina219.getShuntVoltage_mV();
 busvoltage = ina219.getBusVoltage_V();
 current_mA = ina219.getCurrent_mA();
 loadvoltage = busvoltage + (shuntvoltage / 1000);
 power = current_mA * loadvoltage;

 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.print("Power: "); Serial.print(power); Serial.println(" mW");
 Serial.println("");

delay(1000);
}

Let’s go more in details into this code. Notice that we now activate the LED via the digitalWrite() command. The first step is to extract everything we can from the sensor:

shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();

Then, we can calculate the actual voltage on the LED, and the power consumption:

loadvoltage = busvoltage + (shuntvoltage / 1000);
power = current_mA * loadvoltage;

Finally, we display all these values:

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.print("Power: "); Serial.print(power); Serial.println(" mW");
Serial.println("");

You can now upload the sketch, the LED should light up, and this is what you should see on the serial monitor:

Power monitoring with an Arduino board and the INA219 sensor

Which means my LED is using around 10 mW when it is on. Of course, it can vary depending on your own LED and resistor, but it should be in the order of magnitude of mW. You now know how to measure DC voltages, currents and powers using your Arduino board. In a next tutorial I will show you how to actually measure the power consumption from virtually any device in your home !

Here is a summary of all the components you will need for this project: