Building an Arduino - Multi Input - energy monitor

Hello Everyone,
I am building a multi input energy and cycle monitor. I am building it off the "How to build an Arduino energy monitor - measuring mains voltage and current". This is awesome project for me and I have it working well with one current input sensor and one AC mains input.

I want to add a second CT input and am struggling with the code.

The code I am using for the one current inputs is basically:
#include "EmonLib.h"               // Include Emon Library - Energy Monitoring
EnergyMonitor emon1;             // Create an instance - Energy Monitoring
emon1.current(1, 60.74);         // Current: input pin, calibration factor is 60.74
Irms = emon1.calcIrms(1480);

I see where I can edit 'emon1.current(1,60.74)' to include a second input pin, but I struggle with the last line of code as to how to do a second and separate calculation.

Do I make this up like:
Sensor 1
emon1.current(1, 60.74);             // Current: input pin, calibration factor is 60.74
IrmsA = emon1.calcIrms(1480);
Serial.print("Sensor 1: ");
Serial.print(IrmsA);

And Sensor 2
emon1.current(2, 60.74);
IrmsB = emon1.calcIrms(1480);
Serial.print("Sensor 2: ");
Serial.print(IrmsB);

Meaning do the same Calc call and it will follow whatever sensor proceeded the 'Irms = emon1.calcIrms(1480);'?

Sorry if this is posted elsewhere, I searched and couldn't find an answer to this.

Thank you.

Robert Wall's picture

Re: Building an Arduino - Multi Input - energy monitor

DON'T change the library. What you need to do is create a separate instance of the EnergyMonitor class for each current input - look at the emonTx or the emonShield examples on Github.

So you make two instances of the class

 EnergyMonitor emon1, emon2;     // the syntax is identical to "int a, b;"

then you initialise both - separately

  emon1.current(1, 60.74);
  emon2.current(2, 60.56);    // (The CT calibration can be different!)

and then in the main loop you measure the currents, one after the other

  IrmsA = emon1.calcIrms(1480);
  IrmsB = emon2.calcIrms(1480);

and print as you have it.

If you do as you suggested, the software filters that take out the dc bias won't work properly and you'll have big problems that will totally confuse you.

 

Photonut's picture

Re: Building an Arduino - Multi Input - energy monitor

Hi Robert,

Thank you for your reply.

For clarification I didn't intent to change the library, I meant purely editing my own code.

The following is what I got to work...seemingly fine.........:

 emon1.current(1, 60.74);                                                           
  IrmsSP = emon1.calcIrms(1480);                                                    
  emon1.current(2, 60.74);                                                          
  IrmsFF = emon1.calcIrms(1480);

Where I later use either IrmsSP or IrmsFF for further calculations or display...etc. As I say, this seemingly works fine. Both variables display their corresponding values separately.

However I do see the potential for a mix up. I have since re-edited my code to reflect your suggestion above. Also as you point out this way I can have separate calibration for each sensor....which of course is required.

Thank you Robert.....next step is to add a AC input. I have, for use in Canada a 120 VAC / 9 VAC transformer I plant to as as my AC input.

 

Thanks again,

Photonut

 

 

Robert Wall's picture

Re: Building an Arduino - Multi Input - energy monitor

Although you seem to be getting results from re-initialising the class instance each time, please let me assure you that this is the wrong approach - you'll find out (to your cost) if the two channels have a slightly different mid-point offset voltage because the step as you switch between channels will be read as a pulse of current. It's not only (or not just potentially) a mix-up. Using two separate instances, as you've changed to, will completely remove this problem. You should of course only initialise emon1 and emon2 once in your sketch in setup( ) - you don't do it inside loop( ).

Your 9 V transformer should be fine. Check the output under no load, if it's around 11 V ac that's OK (it's what we expect) and if it's a bit lower it won't make much difference (but if it's a lot lower you can, and probably should, change a resistor in the voltage divider to compensate), but it shouldn't be greater or you'll need to change a resistor in the voltage divider to compensate. You'll need to initialise the voltage calibration - twice (once for each power channel) but with the same numbers and again only inside setup( ).

And of course to calculate real power, you'll use the different library method calcVI(...) - but still using the same two instances emon1 and emon2 of EnergyMonitor.

Photonut's picture

Re: Building an Arduino - Multi Input - energy monitor

Hi Robert,

I really do appreciate your comments. I see how my previous code would be more then just a 'potentially' mix up. Glad I sent my first post on this as I would had been shaking my head for sure somewhere down the road.

I am so happy to now trust the inputs in what they are telling me.

 

Thanks again,

Photonut

 

 

Photonut's picture

Re: Building an Arduino - Multi Input - energy monitor

Opps...to clarify,

I have my :

EnergyMonitor emon1, emon2; statement above my void setup() in the variable declaration section ( where I call  libraries etc) and the following within void Loop()

emon1.current(1, 60.74);                                                       
  IrmsSP = emon1.calcIrms(1480);                                               
  IrmsSPx = IrmsSP;
  emon2.current(2, 60.74);                                                      
  IrmsFF = emon2.calcIrms(1480);

 

Should I be moving the EnergyMonitor emon1, emon2 stement to within the setup section?

 

Thank you

Robert Wall's picture

Re: Building an Arduino - Multi Input - energy monitor

Have you looked at the sketches for the emonTx? I suggest you look at the emonTx V2 (because those are more straightforward), and the best one for you now is probably "emonTx_CT123_Voltage" It has the code for the RFM12B radio in it, but you can remove the function/method calls that begin "RF12_" and "send_rf_data" and, when you change the pin numbers, it should work for you.

[Edit]

You really are getting into a tangle with how you are using emonLib.

Declarations at the top of the sketch:

EnergyMonitor emon1, emon2;   

(or you could call them emonSP, emonFF).

done once only inside setup( )

emon1.voltage(0, 221.5, 1.7) ;   // input pin, voltage calibration, phase calibration
emon1.current(1, 60.74);
emon2.voltage(0, 221.5, 1.7) ;   // the same numbers as emon1
emon2.current(2, 60.74);

done each time the sketch repeats: inside loop(  )

emon1.calcVI(20,2000);           // 10 cycles (20 zero crossings) timeout after 2000 ms

Serial.print("Real Power 1        = "); Serial.print(emon1.realPower);
Serial.print("Apparent Power 1 = "); Serial.print(emon1.apparentPower);
Serial.print("Power Factor 1     = "); Serial.print(emon1.powerFactor);
Serial.print("rms Current 1       = "); Serial.print(emon1.Irms);
Serial.print("rms Voltage         = "); Serial.print(emon1.Vrms);            // Don't need this for emon2 ! ! !

and similarly for emon2.

delay(5000);

You probably will need that delay of several seconds to allow the serial output to complete.

Photonut's picture

Re: Building an Arduino - Multi Input - energy monitor

Thank you, I will look.

Looks like my last post didn't stick.

For clarification...the:

EnergyMonitor emon1, emon2; statement should be in 'void setup ()'. I have it now in the variable section where I call other libraries and declare variables.

and

 emon1.current(1, 60.74);                                                        
  IrmsSP = emon1.calcIrms(1480);                                                    

  emon2.current(2, 60.74);                                                          
  IrmsFF = emon2.calcIrms(1480);

are both in the void Loop().

I will look at the other sketches you have suggested.

 

Thank you for your help.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.