int current = 0;
int maxCurrent = 0;
int minCurrent = 1000;
for (int i=0 ; i<=200 ; i++) //Monitors and logs the current input for 200 cycles to determine max and min current
current = analogRead(A0); //Reads current input
if(current >= maxCurrent)
maxCurrent = current;
else if(current <= minCurrent)
minCurrent = current;
if (maxCurrent <= 517)
maxCurrent = 516;
double RMSCurrent = ((maxCurrent - 516)*0.707)/11.8337; //Calculates RMS current based on maximum value
int RMSPower = 220*RMSCurrent; //Calculates RMS Power Assuming Voltage 220VAC, change to 110VAC accordingly
if (RMSPower > peakPower)
peakPower = RMSPower;
I’m afraid you’re totally wrong there. That circuit isn’t suitable for the ESP8266, only for something with an analogue input that works over the range 0 - 5 V. From the ESP8266 data sheet:
“The input voltage range is 0 to 1.0 V when TOUT is connected to external
Therefore, you need to set the midpoint voltage to 0.5 V, and you need to change the value of the burden resistor from 33 Ω to a value that gives you about 0.33 V rms (that’s about 1 V peak - peak) at the maximum current you want to measure.
I also have big reservations about the sketch.
Firstly, you need to mark up the code with three backticks on the line before and after. I’ve done that for you.
Secondly, because you’ve used the wrong assumptions, all your numbers are now wrong.
At maximum current (defined by your choice of burden resistor) you’ll have approx 0.5 V peak at the ADC input, so you need to recalculate based on that.
But what’s worse, there’s generally no relationship between the peak (or peak to peak) values of current and the rms value, because the current waveform is rarely a well-behaved sinusoid. Therefore your 0.707 will only apply then.
We get round that problem by properly calculating the rms (root mean square) value of the current directly from the measured values.
As far as I know, we don’t have a standard sketch for the ESP8266, but I think our standard library will work. So I suggest you start with this sketch:
The “111.1” in
depends on your c.t. and the value you choose for the burden resistor.
The number 1480 in
double Irms = emon1.calcIrms(1480);
is the number of sample to average over, and it should be a whole number of mains cycles for best accuracy. I have no idea (and it is not documented) what a suitable number would be for you. I would think start at a high number, and reduce it until the values start to become erratic, then go back a little way.
If you care, at ALL, about accuracy, don’t use the ADC in the esp8266. It is not rated at any accuracy specification and will generally be wrong. It might give reasonably consistent results, but it is not suitable for measuring AC power consumption.
Glad it is working for you. Sometimes just having an indication of how much is being used is all you need. If you only have a single sensor, its stability and resolution become more important than it’s absolute accuracy when you are trying to figure out everything that is making up your base load and how much each thing is using. While the difference between 10W and 20W is a very significant percentage difference, the cost of the electricity for either is not that great. But when you want to figure out if it is worth doing something about, it is pretty important to have accurate data. As an example my old measuring system indicated my dryer was using 11W when it wasn’t on. That costs me about $11/year. Should I buy a new dryer to save that electricity? Should I put in a switch to turn it off? Should I unplug it when it is not in use? Or should I do nothing?
I did the later. My new system indicates the dryer is drawing 4W when it is off. Since the dryer is almost 30 years old, it probably doesn’t make sense to do anything about it now, especially at this lower estimate for the savings possible.