# Real-Time Energy Monitor with Arduino and LabVIEW

By on August 19, 2011

We present the candidature of Mr. Michele Mancini for the TiDiGino Contest. He proposes us a recent application with Arduino: Real-Time Energy Monitor This is a simple power meter to analize the current consuming in a house using the led indicator of a house energy meter.

Reading the red led of a home energy counters the system detects the corrent consumption in a house.

It is a noninvasive method, not cut wire, no current disconnects, so a very interesting method…
The system consists of two parts: the Arduino board that detects the led pulses and sends the data via the XBee module, and a PC that recive the data through a USB/Xbee module and processes the data with LabVIEW so you can prepare and study the consumption in a very instant.
Arduino sends two datas to the PC:
1 – Real time datas
2 – Average consumption measured in a time of 5 minutes. From the picture we see that the LED that indicates the current consumption is the red LED near to the display. In particular is the LED superior.

Arduino module
To detect the LED blinking you have to apply a simple photoresistor above the led and covered it with black tape. To read the analog voltage using the Arduino you have to use a resistive divider as shown in diagram: The LED blinking causes the voltage drop down and this value is read by Arduino and compared with the voltage acquired by the potentiometer connected in the channel A0. This potentiometer has the task to adjust the sensitivity threshold.
The read data are processed from the sketch and then sends to the PC via Xbee module.

The Arduino code acquisition is as follows:

```
delay(10); //10ms
delay(10); //10ms
if((val_sensore > val_pot)&(flag_acquire == 0)){
flag_acquire = 1;
digitalWrite(LED, LOW);```

With this code Arduino acquires the two voltages, the photoresistor voltage compared with the voltage of the potentiometer, if the value is greater than the sketch actives the flag “flag_acquire = 1”, then read how much time has passed to another flash.
To do this use arduino to read a statement in an internal counter that returns the milliseconds since power on. The instruction is “millis ()” code here:

```pre_tmS = cur_tmS;
cur_tmS = millis();
if( cur_tmS > pre_tmS ) {
tm_diffS = cur_tmS – pre_tmS;
}```

There are 2 variables pre_tmS and cur_tmS, “cur_tmS” needs to read the current value of the internal counter: cur_tmS = millis (); Then if the condition (cur_tmS> pre_tmS) is true, I note the elapsed time between cycles, ie between the LED ON and the next cycle of the LED ON and write it on the variable “pre_tmS”. Now we have to send to the PC via the serial port using XBee with these instructions:

```Serial.print(“S”);
Serial.println(tm_diffS);
delay(10); //10ms```

First Arduino sends a marker “S” used to labview to recognize that this value is part of the reading that real-time instant, then send the value in milliseconds elapsed. Now you have to reset the flag when the LED goes OFF using this statement:

```  if((val_sensore < val_pot)&(flag_acquire == 1)){
flag_acquire = 0;
digitalWrite(LED, HIGH);
impulsi++;
delay(10); //10ms
}```

You have to check the voltage acquired by the sensor, if it falls below the potentiometer value and the flag is active “flag_acquire == 1” then you have to reset the flag. Note that the statement “impulsi++;” is simply a counter that counts the cycles of the LED power meter, this is to make an average energy consumed every 5 minutes. The third task is similar to the first task, wait 1 second, and increments the counter “time_flag++,” which is the fourth task of counting 300secondi (5 minutes). In addition, this task blinks the LED connected to pin 12 at a rate of 1 second.
The fourth task as mentioned above waiting 5 minutes and then sent via serial marker “L” is used to program labview to recognize that the data sent is the data on the average found in 5 minutes.

The sketch

```#define POT 0
#define SENSORE 1
#define LED 13
#define LED1sec 12

// definire il tempo casuale che varia tra 40ms e 250ms
#define TimeAcquire  1000      //1sec
#define Time5Minuts  300      //5 minuti = 300sec

unsigned long cur_tm = millis();
unsigned long pre_tm = cur_tm;
unsigned int tm_diff = 0;

unsigned long cur_tmS = millis();
unsigned long pre_tmS = cur_tmS;
unsigned int tm_diffS = 0;

unsigned int time_flag=0;

unsigned int impulsi=0;
unsigned int val_pot=0;
unsigned int val_sensore=0;
char flag_acquire=0;
char flag_time=0;

void setup() {

pinMode(LED, OUTPUT);
pinMode(LED1sec, OUTPUT);
Serial.begin(115200);          //  setup serial 115200

}

void loop() {

//Acquisisco
delay(10); //10ms
delay(10); //10ms

if((val_sensore > val_pot)&(flag_acquire == 0)){
flag_acquire = 1;
digitalWrite(LED, LOW);

pre_tmS = cur_tmS;
cur_tmS = millis();
if( cur_tmS > pre_tmS ) {
tm_diffS = cur_tmS - pre_tmS;
}
Serial.print("S");
Serial.println(tm_diffS);

delay(10); //10ms

}
if((val_sensore < val_pot)&(flag_acquire == 1)){
flag_acquire = 0;
digitalWrite(LED, HIGH);
impulsi++;          //Incrementa impulsi
delay(10); //10ms
}

//------Ogni 5 minuti invia "impulsi" ----------
pre_tm = cur_tm;
cur_tm = millis();
if( cur_tm > pre_tm ) {
tm_diff += cur_tm - pre_tm; //+=
}
if( tm_diff >= TimeAcquire ) { //Task ogni secondo
tm_diff = 0;

time_flag++;

if(flag_time==0){
digitalWrite(LED1sec, HIGH);
flag_time = 1;
}else{
digitalWrite(LED1sec, LOW);
flag_time = 0;
}

}

if(time_flag>=Time5Minuts){```
```//Sono passati 5 minuti ? ..... Invia "impulsi" rilevati dal contatore
Serial.print("L");
Serial.println(impulsi);
impulsi = 0;
time_flag = 0;
}

//--------------------------------------------------

}```

LABVIEW interface
To recive the data we use the XBee UartSbee V3.1 module from Futura Elettronica. The interface created in LabVIEW is simple to use: As can be seen from this figure, the top graph shows the average consumption every 5 minutes, while the bottom graph shows the real-time energy consumption. It ‘can save the data using the device as a data logger.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7409972,"math_prob":0.9712385,"size":7084,"snap":"2021-43-2021-49","text_gpt3_token_len":1813,"char_repetition_ratio":0.11822034,"word_repetition_ratio":0.056637168,"special_character_ratio":0.26693958,"punctuation_ratio":0.11722488,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9721825,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"im_url_duplicate_count":[null,null,null,3,null,3,null,3,null,3,null,3,null,null,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-17T01:07:34Z\",\"WARC-Record-ID\":\"<urn:uuid:a02d1030-3711-4c3e-b23f-b7b3d22b632d>\",\"Content-Length\":\"114797\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c0c24039-6d17-493f-b9f5-fc1684e7e1f1>\",\"WARC-Concurrent-To\":\"<urn:uuid:6bd98d09-2582-493f-b920-2fc9317123d0>\",\"WARC-IP-Address\":\"93.95.216.36\",\"WARC-Target-URI\":\"https://www.open-electronics.org/real-time-energy-monitor-with-arduino-and-labview/\",\"WARC-Payload-Digest\":\"sha1:ZRL5VMCFCEJKZUMELYQ3P5YMD3EJSX46\",\"WARC-Block-Digest\":\"sha1:O5GZJQANLFFUKFZT5BGI7WJEAEYDOESI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585045.2_warc_CC-MAIN-20211016231019-20211017021019-00454.warc.gz\"}"}