Close
0%
0%

Building an Arduino Energy Monitor & Data Logger

How I built an Arduino energy monitor and data logger to collect energy data for different loads and plotted that data using Excel.

Public Chat
Similar projects worth following
In this post, I’m going to show you how I built an Arduino energy monitor and data logger that would help me to gather current and energy data for different AC loads easily, and plot that information using a program like Excel.

Introduction

Dr. Wattson is an energy monitoring board (based on the #Microchip #MCP39F521, a power monitoring IC) along with an #Arduino library and #Python library (for boards like #RaspberryPi and #BeagleBoneBlack). It is intended to allow you to easily integrate quality energy data into your next project! 

If you haven’t heard of it and would like more information, check out https://upbeatlabs-wattson.launchrock.com/ for more information. It is also available on Tindie.

In this project, I created an Arduino based Energy Monitor and Data Logger to help study the energy characteristics of different loads. 

It consists of an Arduino talking to the Dr. Wattson board to get energy data, along with an SD card to write the data to. A button facilitates toggling the data logging, along with an LED indicator to show when data is being logged. The button along with the LED made it really easy to initiate data logging when I had the appropriate load plugged in. 

To facilitate the logging and subsequent analysis, I also implemented a log rotation scheme, so each run will be logged to a different file. The output is in CSV format, which means it can be easily imported to Excel (or Numbers on Mac or other equivalent), and the data plotted.

Arduino Energy Logger with Dr. Wattson Energy Monitoring Breakout

Arduino Energy Logger with Dr. Wattson Energy Monitoring Breakout

Dr. Wattson comes with a companion acrylic enclosure, so that the AC wires are enclosed and you can use it in a safe manner. The enclosure is available on Tindie as well - https://www.tindie.com/products/upbeatlabs/enclosure-for-dr-wattson-energy-monitoring-board/

I have prepared a User Manual showing how the wiring is to be done, which is now available on GitHub, along with the Arduino and Python libraries. 

See the Instructions section on the Setup, the Circuit and a discussion of the Sketch!

Action!

Once I compiled and uploaded the sketch to my Arduino, getting energy data was pretty easy.

I plugged in the load of choice — a CFL lamp. I wanted to see the current and energy consumption from a cold-start perspective, as well as a warm-start. I plugged in the lamp, but didn’t turn it on.

I pressed the button on my circuit to start the energy measurements — when it was logging, the RED LED told me so! I then turned on the CFL Lamp, and waited until the I thought I had gotten enough data — you see, the CFL, when starting off, consumes more energy than advertised (in my case, a 14W CFL bulb), and then settles down eventually to about 14W. I waited until the readings had settled down. I then pressed the button to turn off the reading.

I could now safely eject my SD card and start looking at the data!

PS — To log data from additional devices, all you have to do is reset the Arduino — it will pick the next available DATAXX.CSV to log the data into, so you can easily keep the data separate.

Note: Instead of using the Arduino Serial Monitor, I use the built-in “screen” command on my Mac terminal as a serial terminal. In my Sketch, I print the energy data to Serial, and also issue a “clearscreen” command, so the output doesn’t move

Serial.write("\x1B" "c"); // Clear the screen on a regular terminal
wattson.printMCP39F521Data(&data);

This only works on a regular terminal that can accept terminal commands like the above, and does not work on the Arduino Serial Monitor unfortunately. However, once you try screen, you may never go back to Arduino Serial Monitor!

The command will be something like this:

$ screen /dev/tty.usbmodem1411 9600

where the first parameter is the “serial port”, and the second is the speed you have configured for your Serial connection in your Arduino sketch.

Plotting the data in Excel

Because the file in CSV format, loading it and plotting it in Excel was pretty easy. I shall not go into the...

Read more »

  • 1 × Arduino Uno Arduino Uno
  • 1 × Dr. Wattson Energy Monitoring Board Dr. Wattson Energy Monitoring Board
  • 1 × Enclosure for Dr. Wattson Energy Monitoring Board Enclosure for Dr. Wattson Energy Monitoring Board
  • 1 × SD card breakout board SD card breakout board
  • 1 × LED RED LED

View all 8 components

  • 1
    Setup

    Dr. Wattson comes with a companion acrylic enclosure, so that the AC wires are enclosed and you can use it in a safe manner. The enclosure is available on Tindie as well - https://www.tindie.com/products/upbeatlabs/enclosure-for-dr-wattson-energy-monitoring-board/

    I have prepared a User Manual showing how the wiring is to be done, which is now available on GitHub

    The digital out that is coming from Dr. Wattson is completely isolated from the mains, as the energy measurement is done in an isolated manner using current and voltage transformers, so the digital out is perfectly safe to handle.

    Once in the enclosure, all you need to do is plug the cable into an existing outlet, and plug any device into the outlet on the enclosure . It is calibrated to measure currents up to 4A (so I could be able measure really small currents for measuring standby power — the MCP39F521 chip on which it is based has a 4000:1 dynamic ratio, meaning it can measure from 4A down to 1mA). I have also done a 15A conversion - replacing the burden resistors to suitable values and recalibrating the board to be able to measure currents up to 15A. 

    Arduino Energy Logger with Dr. Wattson — with CFL lamp load plugged in

    Arduino Energy Logger with Dr. Wattson — with CFL lamp load plugged in

  • 2
    Circuit

    Here is the circuit I used:

    I used an SD card breakout similar to the Adafruit one (so I used that in Fritzing, as that was the closest part). The connection is pretty standard — CLK (Arduino Uno pin 13), MISO (Arduino Uno pin 12), MOSI (Arduino Uno pin 11) and CS (Chip select). CS is configurable, though it defaults to pin 10 for hardware CS — I just use that.

    For the button, I used the Button library by Jack Christensen, which provides debouncing and a variety of useful stuff in an easy to use package. (https://github.com/JChristensen/Button). The button is using the Arduino’s internal pull-up, so it doesn’t have any external pull-ups and also uses inverted logic (high is OFF, low is ON) — all of these are configurable as parameters to the Button constructor! I’ve hooked up the button to pin 4, but you can use any available pin you like.

    I really did not have the time and patience to model Dr. Wattson as a new part in Fritzing, so I cheated, and used the advice of Z-HUT to easily whip up a custom part without much ado. I highly recommend watching it! https://www.youtube.com/watch?v=dfxx8wF3Uhs — thanks Z-HUT! :-)

    The only downside is that I have to be content to use the breadboard image of the “generic IC” part that I modified to represent Dr. Wattson. C’est la vie!

    Here is a look at Dr. Wattson’s pins (from left to right):

    • SCL — for I2C communication, goes to Arduino Uno A5
    • SDA — for I2C communication, goes to Arduino Uno A4
    • ZCD — Zero Cross Detection — we are not using this feature in this example
    • Event — Event indicator — we are not using this feature in this example
    • GND — Connect to Arduino GND
    • VIN — Connect to Arduino 5V (or to 3.3v on the RPi) - used for bi-drectional level shifting
    • 3.3v — Connect to Arduino 3.3v (used to power the Dr. Wattson board)
    • GND — Connect to Arduino GND (you only need to connect one of the GND pins)

    Here is my Fritzing diagram showing the circuitry:

    Arduino Energy Logger Circuit with Dr. Wattson

    Arduino Energy Logger Circuit with Dr. Wattson

  • 3
    Sketch

    Ok, now to move on to the actual programming! I have added this sketch as an example to my Dr. Wattson library which is available on GitHub - https://github.com/upbeatlabs/drwattson - simply download and install the Arduino library, and run the WriteEnergyDataToSDCard example there! 

    My data logging is done in files with a name like DATAXX.CSV, where XX is a number (from 00 to 99, so a 100 files). I check the SD card for existing file names, and pick the latest unused file name — for example DATA15.CSV

     char filename[] = "DATA00.CSV";
    ...  
    // create a new file  
    for (uint8_t i = 0; i < 100; i++) {
        filename[4] = i/10 + '0'; 
        filename[5] = i%10 + '0'; 
        if (! SD.exists(filename)) { 
            Serial.print(F("Data file is ")); 
            Serial.println(filename);
            // only open a new file if it doesn't exist 
            break;  // leave the loop! filename will now be the one we desire        }  
    }

    In the loop() function, once the button is pressed, it toggles the readData variable, which controls if I read the energy data.

    bool readData = false;
    ...
    void loop() {
        myBtn.read();                    //Read the button
        if (myBtn.wasReleased()) { 
          //If the button was released, change the LED state 
           readData = !readData; 
           digitalWrite(LED, readData);    
        }
        if (readData) {       ...
    
        }
        ...
    }

    The data read is written out to SD card in a comma-separated format, to make it easy to import in Excel or another spreadsheet program. 

          if (dataFile) {
            dataFile.print(currentMillis); 
            dataFile.print(",");
            dataFile.print(fData.currentRMS); 
            dataFile.print(","); 
            dataFile.print(fData.activePower);  
            dataFile.print(",");  
            dataFile.print(fData.reactivePower); 
            dataFile.print(","); 
            dataFile.println(fData.apparentPower); 
            // print to the serial port too:
            dataFile.close();      
    }
      

View all 3 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates