Customizing EmonLib Current Only, for use with the Arduino UNO, with American voltage Levels

I have read through the EmonLib Current Only program and I’m still confused about many things. When the Current only sketch is loaded into the Arduino ide, is there some prompt that opens in the Serial Monitor, that asks the user key questions about their particular application. In looking through that sketch, it looks like the original programmer set this up for use with a certain application with default values filled in, that may not necessarily apply to me.

In particular the line:

emon1.current(1, 111.1); // Current: input pin, calibration
.
For the Arduino UNO, shouldn’t the pin number be A1? I do realize that the 111.1 calibration only applies to the YHDC SCT-013-000, CT. I am using the YHDC SCT-013-010, CT, which has a 0-1 vac output, so if I understand correctly, the calibration starting point for me would be 10. I must be missing something, please point me in the right direction.

“A1” and “1” in this context are synonymous - as far as I know (I can’t find it immediately) there’s an Arduino header file where the translation takes place, and that’s done automatically.
[Edit] There’s an explanation here: arduino - analogRead(0) or analogRead(A0) - Electrical Engineering Stack Exchange

I don’t see that. What questions? There should not be any, if you have set up your Arduino IDE. There are some notes in “Learn” → “Using the Arduino IDE” that might help you.

When I compile that sketch, I see:

“Sketch uses 4186 bytes (12%) of program storage space. Maximum is 32256 bytes.
Global variables use 294 bytes (14%) of dynamic memory, leaving 1754 bytes for local variables. Maximum is 2048 bytes.”
and that’s the normal message,

and then the values read - apparent power (VA) & current (A) in the serial monitor.

That is correct. But note, as your c.t is the voltage-output variety, you must not have the 33 Ω burden resistor. That’s there to convert the 50 mA from the ‘standard’ 100 A : 50 mA c.t. into a voltage - and in your case, there’s a burden resistor inside the case of the c.t.

Thank you Robert Wall. The link that you provided answered the pin number confusion.
I have not tried to run the sketch yet, so I didn’t know if it would harm anything, without some understanding of how the EmonLib handles the fact that I’m connecting to a 120 VAC 60 Hz circuit versus a 230 VAC 50 HZ circuit. How does it learn this.
My input circuit does not have a burden resistor, only the two 470 k ohm resistors to bias the 0-1 vac CT output to a center voltage of 2.5 vdc. I also have the 10 micro-Farad capacitor in parallel with the lower 450 k ohm resistor, to give the ac somewhere to go. I looked at it with an oscilloscope, and it looks good. I tested it with a hand drill. I was smiling.
So that is my understanding now, I only have to set my Analog input pin and change the calibration factor to 10, and I should be good to run the Current Only version of EmonLib. If you see anything in my explanation that doesn’t seem correct, please say something. Stephen

By default, all the pins are defined as inputs. So until you have a sketch where an output is defined, it’s hard to damage anything. But that’s no reason for complacency, it always pays to check. Had a pin been grounded (say) when you powered up and it defaulted to (or was defined as) input, all would be fine. If however you defined it as an output and then drove it high, it would damage the processor - possibly beyond use.

You need to tell it. In the line where apparent power is calculated and printed, you substitute your voltage “120.0” for the constant “230.0”.

  Serial.print(Irms*230.0);	       // Apparent power

No, it seems like you’ve got a handle on it.

Wow! Thanks again Robert Wall. It works like a charm and seems to be as accurate as I’ve been reading here, about.
Now, the next progression would be to add a second CT. Is there a way to modify that sketch to add another. Could it be as simple as:

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include “EmonLib.h” // Include Emon Library
EnergyMonitor emon1; // Create an instance
EnergyMonitor emon2; // Create a 2nd instance

void setup()
{
Serial.begin(9600);

emon1.current(1, 10.0); // Current: input pin, calibration.
emon2.current(2, 10.0); // Current: input pin, calibration.
}

void loop()
{
double Irms = emon1.calcIrms(1480); // Calculate Irms only
double Irms2 = emon2.calcIrms(1480); // Calculate Irms only

Serial.print(Irms*118.0); // Apparent power line1
Serial.print(" “);
Serial.print(Irms); // Irms
Serial.print(” ");

Serial.print(Irms2*118.0); // Apparent power line 2
Serial.print(" ");
Serial.println(Irms2); // Irms2

or is there more that I would need?
Stephen

Indeed it is.

A refinement: 1480 is no longer the optimum number (since emonLib had been tweaked to speed up sampling). The sampling rates, measured with a standard emonTx V3.4 (and the emonPi is the same processor) are:
calcIrms( ) - approx 5588 current samples per second.
calcVI( ) - approx 2535 pairs of voltage & current samples per second.
and the details:
Sampling rate of emonLib

(BTW - There’s no need for the heading-size text.)

Thanks once again Robert Wall. I don’t recall in reading where the 1480 was derived, so this is a bit confusing to me at this point. So I’m not clear on why it is no longer an optimum number. So rather than bothering you with silly questions, I think I need to read some more and think on this, so that when I follow up, hopefully time spent will be more meaningful. Is there another source in addition to the link provided. Such as a something to put this sampling business in context. Perhaps a developmental discussion. Stephen

I can’t think of a relevant discussion to point you towards.

If you’re averaging samples of a periodic wave to get the average value, and you don’t have an exact number of whole cycles, then you’ll have an error, the magnitude of which depends on where in the cycle the ‘overhang’ lands. If it’s near the zero crossing, the error is small, if it’s near a peak, the error will be large. The closer you can get to sampling for a whole number of cycles, the smaller the error.

“1480” is empirical. You time how long it takes to get a specific number of samples, then you know the rate. But since it was first measured, emonLib has been refined and speeded up, so that number is now not the best.

It wasn’t. The silly question is the one that assumes I know what you’re thinking but you didn’t write down. :grin:

The oversized text happens when there’s not an empty line above and below the text
and the insert horizontal line characters. (one or more dashes, which the OP inserted as a divider between text sections, not realizing the effect it would have)

Like this:

add another. Could it be as simple as:

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

When carriage returns are present in both places, it looks like this:

add another. Could it be as simple as:


// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

Thanks again Robert Wall. My head is sore this morning with all that wall banging. You had said the value 1480 was derived emperically and that, a refinement: 1480 is no longer the optimum number (since emonLib had been tweaked to speed up sampling).
So does this mean that the 1480 value is a sampling rate of the number of cycles that originally seemed provide results that have been proven to be accurate for most practical intents and purposes? You mentioned, calcIrms( ) - approx 5588 current samples per second, but isn’t that for a Raspberry PI based Energy Monitor. I guess what I’m coming to is:
I would like to eventually get to full voltage and current sampling for two circuits. I’m starting out with this current only version, just to get my feet wet. I have successfully been able to read the current fairly accurrately on one circuit. Now I’d like to add a second current reading. You had said that my modification of the sketch was on point, but that the 1480 sampling rate is no longer optimum. What would be an optimum sample rate considering I’m using an Arduino UNO. Is this number limited by other factors including other modifications to the sketch that may affect it’s accuracy? If there is no other modifications to the sketch, other than adding a second current reading, what would be a good sampling rate for the UNO? Is it arbitrary, such as 2000, 3000, 4000, 5000? Stephen

The Uno is the same processor, operating I think at the same clock speed as the emonTx, so I believe the performance is pretty much the same. So I would suggest the integer that is closest
to n/50 × 5588 is a good number - n being the number of mains cycles. You get 93.133 samples per cycle, so 1397 samples will almost exactly fit 15 cycles of your 60 Hz mains, giving you a sample of length of ¼ s.

No, because the sketch hands over to the library and waits until the library comes back with the answer.

You have no control over that. It samples as fast as it can, doing the minimum amount of maths, until it’s got the number of samples you specified.

Here is the working part. It reads the input, filters the standing bias out, squares it and accumulates the sum:

  for (unsigned int n = 0; n < Number_of_Samples; n++)
  {
    sampleI = analogRead(inPinI);
	
    // Digital low pass filter extracts the 2.5 V or 1.65 V dc offset, 
	  //  then subtract this - signal is now centered on 0 counts.
    offsetI += (sampleI-offsetI)/1024;
	  filteredI = sampleI - offsetI;

    // Root-mean-square method current
    // 1) sum squared current values

    sumI += filteredI * filteredI;
  }

It needs the filter to remove the 2.5 V bias offset that those 470 kΩ resistors introduced.
Then it calculates the average – the root mean square average – and scales it to Amps:

  double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
  Irms = I_RATIO * sqrt(sumI / Number_of_Samples); 

  //Reset accumulators
  sumI = 0;
//--------------------------------------------------------------------------------------       
 
  return Irms;

When you do that, all this becomes irrelevant! The calcVI( ) method watches for zero crossings of the voltage and counts mains cycles, so the problem no longer exists.

Thanks again Robert Wall. In your previous post you said:

So I would suggest the integer that is closest
to n/50 × 5588 is a good number - n being the number of mains cycles. You get 93.133 samples per cycle, so 1397 samples will almost exactly fit 15 cycles of your 60 Hz mains, giving you a sample of length of ¼ s.

Did you mean n/60 x 5580? Because 15/60 x 5588 = 1397 samples?

Leading me to believe that I should replace the 1480 with 1397. Earlier I had thought that the 1480 would be replaced with a higher value sampling rate, such as was mentioned earlier as:

calcIrms( ) - approx 5588 current samples per second.”

Bill.Thomson was right, I didn’t intentionally set the font larger and bolded.

Robert Wall, you also said:

You have no control over that. It samples as fast as it can, doing the minimum amount of maths, until it’s got the number of samples you specified.

This leads me back to thinking that the number of samples is almost arbitrary.

In your discussion of the working part, you said:

Here is the working part. It reads the input, filters the standing bias out, squares it and accumulates the sum:”

// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,

// then subtract this - signal is now centered on 0 counts.

Does the sketch extract the 2.5 v, as a hard value or does the sketch sense the value of the bias that I have set. I ask this because I didn’t use precision resistors, so my bias is a little less than 2.5 vdc. Could that affect performance as well?

I had mentioned that I would like to eventually get to measuring both voltage and current. You said:

When you do that, all this becomes irrelevant! The calcVI( ) method watches for zero crossings of the voltage and counts mains cycles, so the problem no longer exists.

Hopefully by then, when I switch over to the EmonLib for voltage and current, I’ll have much of this tried and true. Stephen

You seem to be missing the big picture regarding the number of samples. What you want is a sampling period that is an exact whole number of mains cycles. You want this so that the measurements are optimal accuracy and repeatable. But it’s not necessary that you have that condition – if you don’t achieve that, it just means that under steady-state conditions, you might see jitter between readings, that otherwise ought to be constant. With a longer sapling period is, the relative jitter will be less.

It adjusts automatically to the bias it finds (As it is, it’s hard enough to get accurate low-value readings, it would actually be impossible otherwise.)

I did - I live in a 50 Hz world. I did the right sum, typed the wrong number. Sorry for the confusion.

Thank you so much Robert Wall. I’m happy to report that I have the sketch reading and displaying two separate currents(I). I left the sampling rate alone. As a test, I made a short 4 foot long extension cord, made of 12 gage Romax, house wiring. I stripped about 4 inches of the outer sheathing exposing the Black L1 and White L2, in two places, about a foot apart, and clamped onto L1, with the arrow on the top, pointing toward the load end. With no load, both read .02 amps. Next, I tested using my hand drill, which I knew draws around 2.50 Amps. They both read a fluctuating 2.40 to 2.60 amps. I then connected a shop vacuum, which I knew drew 9.4 Amps, and both of the CT read fluctuating values around 9.30 to 9.60 Amps. Thank you so much for your help. Now I need to get a small transformer so I can get ready for reading both Voltage and Current. I’ll be back, I’m sure. Stephen

1 Like

Hello Stephen,

If you have your 12 gauge Romex connected across both hot legs, then one wire is Leg 1
and the other is Leg 2. If not, then the Black wire is a hot leg and the white wire is neutral.

Bill.Thomsom, I highly value your input. Thank you for thinking of this point. In my test setup I am essentially measuring the same current twice. Using one CT around the single L1 at one point of the test extension cord, and the other CT around the same single L1, except one foot down the line. So both CT are reading the same current. Stephen