How to control current inverter on grid

 // MakeIdea [Hemant]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <SoftwareSerial.h>
#include <PZEM004Tv30.h> // PZEM

PZEM004Tv30 pzem(&Serial3);

#define SinDivisions (200)

volatile long counterX;
volatile int num;
const int interruptPin = 3;
volatile int phs; 
int tstInt=0;
volatile int newICR3;
volatile  int lockTest;
volatile int lockDet;

static int microMHz = 16;
static int freq = 50;   
static long int period;  
static unsigned int lookUp[SinDivisions];
static char theTCCR3A = 0b10000010;

void setup()
{
  // Serial.begin(115200); //PZEM

  double temp; 
  
  period = microMHz*1e6/freq/SinDivisions;
  
  for(int i = 0; i < SinDivisions/2; i++)
  { 
    temp = sin(i*2*M_PI/SinDivisions)*period;
    lookUp[i] = (int)(temp+0.5);           
  }
  
  TCCR3A = theTCCR3A;   // Konfigurasi register kontrol Timer 3
  TCCR3B = 0b00011001;  // Konfigurasi register kontrol Timer 3
  TIMSK3 = 0b00000001;  // Mengaktifkan interrupt overflow Timer 3
  ICR3 = period;        // Set nilai Input Capture Register 3
  pinMode(interruptPin, INPUT_PULLUP);    // Pin 3 untuk zero crossing detector
  attachInterrupt(digitalPinToInterrupt(interruptPin), phaseDet, FALLING); // untuk pin 3
  sei();                // Mengaktifkan global interrupts
  
  // Mengatur pin sebagai output (PE3 atau PE4)
  DDRE = 0b00011000;   // PE3 sebagai output, sesuaikan dengan kebutuhan proyek
}

void loop(){; 
} 

void phaseDet() {
  phs = digitalRead(interruptPin);
  if (num > 0 && phs == 0) { // Adjust this value according to your specific needs
    num--;
    ICR3 = ICR3 + 1;
  }
  if (num < 99 && phs == 1) { // Adjust this value according to your specific needs
    num++;
    ICR3 = ICR3 - 1;
  }
}

ISR(TIMER3_OVF_vect) {
    static int num;
    static int ph;
    static char trig;
    
    if(ph == 1)
    {
      theTCCR3A ^= 0b10100000;  // Misalnya, XOR dengan nilai 0b11110000
      TCCR3A = theTCCR3A;        // Menetapkan nilai kembali ke register TCCR3A
      ph = 0;   
      // phs = 1;          
    }
    if(num >= SinDivisions/2)
    {
      num = 0;                
      ph++;
    }
    OCR3A = OCR3B = lookUp[num];
    num++; 
}

here my code to synchronize with grid and this waveform in osiloskop

but when i try to grid voltage drop and current so high, any solution?

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184.

and do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).

1 Like

Thanks for the like but please fix your first post.. click on the pencil below your post and edit the content to add code tags around the code portion.

okee

Do you understand that the interrupt will happen on ANY falling? Even noise on the signal causing a falling?

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