# Analogue Read in ESP8266 WEMOS D1 MINI is fluctuating

I am trying to read the current consumption of a light bulb (160 W) using a current transformer (5A/5mA) connected to the ESP8266 WEMOS D1 MINI analog pin.

I have put a 240 ohms burden resistor in parallel with the pins of the current transformer. I used this formula to get the resistor: Burden Resistor (ohms) = (AREF * CT TURNS) / (2√2 * max primary current)

I then used a voltage divider to get the mid-point voltage, I inputted the voltage divider to the 3.3V source of the WEMOS. A 10uF capacitor is used at the pin of the current transformer and the ground.

You can check below my installations:

Note that the schematic is from this link. Thanks to it, I was able to install my circuit.

I wrote a very simple program to display the result in volts, but what is being displayed when turning on the light bulb is very confusing. The result is fluctuating from 2.1 ish to 1.4 ish (1.65 being 0V which means the light bulb is off, and 2.10 being the light bulb is on.) 2.1 volts is fair enough because if 3.3 volts is displayed, it means that there is 5A passing, and the light bulb is 160 watts meaning approx. 0.7A, so it means that 2.1 volts should be displayed. You can also check below the printed result.

What does this fluctuation mean?

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

void loop() {

float mean;
float sensorValue[21];
for (int i = 0; i <= 21; i++) {
sensorValue[i] = ((analogRead(A0)) * ((3.3) / (1023)));
mean += sensorValue[i];
delay(1);  //wait 1ms
}

mean = mean / 21;

float sum;
for (int i = 0; i <= 21; i++) {
sum += pow((sensorValue[i] - mean), 2);
}

sum = sqrt(sum);

delay(500);
Serial.printf("%.10f", sum);
Serial.println(" ");
}


Take under interrupt (every 1 ms for 50 Hz mains) 21 samples.
Calculate the "mean" measure.
When done, you can calculate the RMS value of the current.

As in this file for ATmega328N under Arduino. Don't know if it can work for ESP8266 (seems no).
Compiled with version Arduino 1.8.19. Note that some comments are in french.

• Take 21 samples under interrupt (every 1ms, change this value if the main is 60 Hz)
• verify that samples are on a "sinusoid" ...
verify that extreme samples (the 1st and the 21st are the "same" value)
• If they are not on a sinusoid, then the current is distorded.
Verify also that samples are not just as "saturated" (high or low)
You should then change the "sensitivity" ... change the "burden" resistor ( must be lower).
• calculate the mean of all samples
• calculate the distance between every sample and "mean"
• square these distances
• Sum the distances
• square root of the sum
• calibrate this value with the current measured with an RMS ammeter
• Thank you for your reply. For a 240 ohm burden resistor, when the light bulb is off, I am getting a value of 1.63 (keep in mind that I am multiplying by 3.3 and dividing by 1023). But when turning on a 60-watt light bulb (yes I am now temporarily using a 60-watt light bulb), I am getting only 1.65-1.66. Do you think I should go with a much lower-burden resistor?
– P-Em
Commented Nov 28, 2023 at 21:53
• To add to my comment, I did all the steps that you mentioned. I have edited my question and added the edited program.
– P-Em
Commented Nov 28, 2023 at 21:55
• You must absolutely work under interrupt as samples must be really "equidistant". The "for ... loop" is, if I remember well, 4 ms " loop time"without any internal instruction ... It's way too long. Each sample with Arduino takes 140 us. Commented Nov 28, 2023 at 22:21
• @P-Em Your code uses uninitialized variables and indexes the table beyond the last element, so results may be bogus. Do you have compiler warnings enabled and do you read them? They usually prevent most errors. Commented Nov 28, 2023 at 22:21
• I will search "through my applications notes" if I find this peace of program as I did it ... a long time ago. I did it for Atmega328, not for ESP8266. Commented Nov 28, 2023 at 22:24

You are taking one sample per 1 second.

Taking the sample could be from any position in the waveform of current, so taking one sample per 1 second means nothing about the waveform of the current, not the shape or magnitude.

We also don't know the waveform of the current. It might not be a sine wave at all. It might take short 5A spike at each mains cycle but still average out as 0.7A for 160W. So taking the sample once per second can hit 0A or 5A reading.

• Do you suggest taking for example 50 sanples get thwir aversge and display the result
– P-Em
Commented Nov 28, 2023 at 15:23