How to measure power in PID controlled circuit

Hi,
I am designing a circuit to control microwave oven temperature to set temperature points using PID logic. I am using Arduino UNO and solid-state relay for PID on-off of microwave, MLX90614 infrared temperature sensor, and for electric power measurement PZEM-004T power measurement module. I am facing a problem with power measurement! As at set point relay get on-off and no power is shown by serial monitor Arduino IDE or underrated voltage sometimes in running condition of the circuit.

#include <PZEM004Tv30.h>
#include <PID_v1.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

//Solid state relay on PIN 5 on the Arduino
#define RELAY_PIN 5

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=1500, Ki=.5, Kd=20;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 10000;
unsigned long windowStartTime;

Adafruit_MLX90614 mlx = Adafruit_MLX90614();


PZEM004Tv30 pzem(11, 12);// connection to Power sensor Tx-11 and Rx-12

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  Serial.begin(115200);
  Serial.println("Starting");
  windowStartTime = millis();

  //initialize the variables we're linked to
  Setpoint = 100;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  mlx.begin();
}

void loop() {
  Input = (mlx.readObjectTempC());
  Serial.println(Input);
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if (millis() - windowStartTime > WindowSize)
  {
    //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if (Output < millis() - windowStartTime) 
    digitalWrite(RELAY_PIN, LOW);
  else 
    digitalWrite(RELAY_PIN, HIGH);
  

  Serial.println();
  delay(0);
    float voltage = pzem.voltage();
    if(voltage != NAN){
        Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
    } else {
        Serial.println("Error reading voltage");
    }

    float current = pzem.current();
    if(current != NAN){
        Serial.print("Current: "); Serial.print(current); Serial.println("A");
    } else {
        Serial.println("Error reading current");
    }

    float power = pzem.power();
    if(current != NAN){
        Serial.print("Power: "); Serial.print(power); Serial.println("W");
    } else {
        Serial.println("Error reading power");
    }

    float energy = pzem.energy();
    if(current != NAN){
        Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
    } else {
        Serial.println("Error reading energy");
    }

    Serial.println();
    delay(500);
}

I'm not sure I fully understand; can you please clarify if by 'power measurement', do you mean the actual electrical power that goes into the microwave unit?

Which version of the PZEM-004T do you have? Is it one with a shunt resistor or with a current transformer?

Can you show us a schematic of your project and the code of your Arduino sketch?

1 Like

Post your code.
And a schematic.

Software issues require code
Hardware issues require schematics.

Assuming you get readings of power as you said when it is running you don't get readings. The problem is in code.

If you never get readings, then the problem could be in either software or hardware

As a schematic of how you measure AC voltage is important.
As well as how you measure AC current.

1 Like

My interpretation thus far is that it may be either a hardware or a software problem, with my money being on the latter, so I'd like to see both schematic and code. The schematic to exclude the possibility of a silly connection issue, and the code to look for what's most likely the actual problem. My hypothesis at this point is that there's a problem in setting up RS485 communications with the sensor board, so that's something I'd be particularly alert on.

My first thought is that the reading is only current [ edit: OP does say undervoltage, so probably not a problem] so not real power, and that reading is intermittent, blocked by code in some way.

I'm curious if delay(500) ....is in there somewhere.....

1 Like

Yeah, that's also a possibility. Either way, we'll need to evaluate that mystery code.

#include <PZEM004Tv30.h>
#include <PID_v1.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

//Solid state relay on PIN 5 on the Arduino
#define RELAY_PIN 5

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=1500, Ki=.5, Kd=20;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 10000;
unsigned long windowStartTime;

Adafruit_MLX90614 mlx = Adafruit_MLX90614();


PZEM004Tv30 pzem(11, 12);// connection to Power sensor Tx-11 and Rx-12

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  Serial.begin(115200);
  Serial.println("Starting");
  windowStartTime = millis();

  //initialize the variables we're linked to
  Setpoint = 100;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  mlx.begin();
}

void loop() {
  Input = (mlx.readObjectTempC());
  Serial.println(Input);
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if (millis() - windowStartTime > WindowSize)
  {
    //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if (Output < millis() - windowStartTime) 
    digitalWrite(RELAY_PIN, LOW);
  else 
    digitalWrite(RELAY_PIN, HIGH);
  

  Serial.println();
  delay(0);
    float voltage = pzem.voltage();
    if(voltage != NAN){
        Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
    } else {
        Serial.println("Error reading voltage");
    }

    float current = pzem.current();
    if(current != NAN){
        Serial.print("Current: "); Serial.print(current); Serial.println("A");
    } else {
        Serial.println("Error reading current");
    }

    float power = pzem.power();
    if(current != NAN){
        Serial.print("Power: "); Serial.print(power); Serial.println("W");
    } else {
        Serial.println("Error reading power");
    }

    float energy = pzem.energy();
    if(current != NAN){
        Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
    } else {
        Serial.println("Error reading energy");
    }

    Serial.println();
    delay(500);
}

Hi, @shaan
Welcome to the forum.
To add code please click this link;

This places your code in a scrolling window, making it easier to read.

The easiest power measurement would be to use the labelled max power of the oven, say 1000W.
And calculate with duty cycle of the PID output.
Duty of 10% == 100W
Duty of 50% == 500W
Duty of 100% == 1000W

Which I believe is how most MicroWave Ovens do their power level outputs.

Tom... :smiley: :+1: :coffee: :australia:

2 Likes

I think there is no problem in the schematic connection and hardware is fine as I had tested and getting reading in initial stage of working but problem arises as on-off started in PID control. I think there is timing problem. I want power reading and precisely electrical energy given to microwave to heat the jar.

There is delay of 500 msec in the code.

Hi,

If you want input power, you would be best to measure the mains power input voltage and input current and use the duty cycle to get the average power used.

Tom... :smiley: :+1: :coffee: :australia:

2 Likes

Hello Tom,
Thanks for replying. I am wondering how can I measure varying duty cycle in PID controlled process and measure average power used.

  if (Output < millis() - windowStartTime) 
  {
    digitalWrite(RELAY_PIN, LOW);
    // Read the voltage and current here.
    // Calculate power and multiply by 
    // Output/10000 to get average power.
  }
  else 
1 Like

Thanks Johnwasser, I was dwelling into the code for on time measurement. Thanks a lot!

1 Like

I have an older microwave.
when I set it for 30% power on the keypad, it runs for 10 seconds, then is off for 20 seconds. that is using the clock on the microwave.

when I did it for 50% power it was on for 15 seconds and off for 14 seconds. I watched this for 3 minutes as it was heating some water and it was easy to tell when it was running and when it was not.

as I said, mine is an older microwave. the type that you could not reduce voltage to reduce power and that you had to engage and it would take a moment to develop power to output.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.