Phase Shift with digitalWrite

Im using Arduino UNO for controlling a 2 Leg single phase inverter.

i use digitalWrite function to generate the gating pulse for the MOSFET.
i want to give an initial delay before the actual gating signal is generated. that is the delay must occur only once and shouldnt repeat in the loop. i get the initial delay value from an external potentiometer connected to A0 pin.

im attaching the code for reference
the problem is the initial delay gets added up with the gating pulse and changes the ON and OFF timings. Please help me with the right way to get this done

int sensorPin = A0;
float sensorValue = 0; 
void setup() {
  // initialize digital pin 3 6 as  output.
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
  
}

// the loop function runs over and over again forever


void loop()
{
  d(); // initial delay , calling the delay function
  digitalWrite(3, HIGH);
  digitalWrite(6, LOW); 
  delay(10);  //10ms delay for ON time           
  digitalWrite(3, LOW);
  digitalWrite(6, HIGH);  
  delay(10); //10 ms delay for OFF time, total time = 20ms
}

void d()
{
  sensorValue = analogRead(sensorPin);
  delay(sensorValue*5/1023);

}

Hi, welcome to the forum.

Sorry, but I don't understand it. You could write the timings on a piece of paper and make a photo of it.
Do you want to create a PWM signal that varies the time of pin 6 being HIGH and 3 LOW ?
The loop() is repeated over and over again. If you want to do something just once, you should do that in the setup() function.

boolean first=true;

void loop()
{
 if (first)
{

  d(); // initial delay , calling the delay function
first=false;
}
  digitalWrite(3, HIGH);
  digitalWrite(6, LOW); 
  delay(10);  //10ms delay for ON time           
  digitalWrite(3, LOW);
  digitalWrite(6, HIGH);  
  delay(10); //10 ms delay for OFF time, total time = 20ms
}

in reality there is something wrong with the whole idea as its unless you are somehow turning on the Arduino via an external trigger, which would also be pointless as you'd need to wait for the bootloader to finish its delays before even setup() would run, let alone loop

It looks like the whole thing is missing some initial trigger input to start things.

Perhaps you'd like to describe what on earth you are building??????

Hi, can you draw a graph of the two outputs and how you want them phased with respect to your pot setting.

Draw and photograph, jpg or png thanks.

Tom... :slight_smile:

thanks for the reply guys

I’m building a single phase inverter and using arduino for its gating signals.

I need the inverter output to be phase shifted according to the analog signal through A0 pin.
The problem with my code is that instead of phase shift the output frequency is varying. The problem is, the initial delay that i gave for phase shift gets added to the total time.
Im attaching the required pulse for one of the gates.

i modified the code little bit. attaching that too.

boolean first=true;
int sensorPin = A0;
int sensorValue = 0; 
void setup() {
  // initialize digital pin 3 6 as  output.
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
  digitalWrite(3, LOW);
  digitalWrite(6, HIGH);
}

// the loop function runs over and over again forever

void d()
{
  sensorValue = analogRead(sensorPin);
  sensorValue = sensorValue*5/1024 ;
  delay(sensorValue);
boolean first=true;
}


void loop()
{

 if(first)
{
   d();
first=false;
}
  digitalWrite(3, HIGH);
  digitalWrite(6, LOW); 
  delay(10-sensorValue/2);             
  digitalWrite(3, LOW);
  digitalWrite(6, HIGH);  
  delay(10-sensorValue/2); 
 
}

Your code didnt seem to include anything which sensed the gate signal.

There are several ways to achieve your requirement

You can probably just poll in the loop() using a state machine and checking the microsecond time to delay and control the period of your output pulse

Or you can probably do it using an Interrupt Service Routine triggered by your gate signal, and use the timer and timer interrupts (and interrupt service routine) to delay and then control the length of your output pulse, but if you want to use the timer, you will have issues if the output pulse is still high when you receive the next gate signal - in which case you'd need 2 timers, (which AFIK are only available on the 16U2 and 32U4 devices (and probably the mega))

HI, what you want is a pulse to come out of pin3.
The pulse out of pin 6 is the same as 3 but delayed by an amount dependent on your pot input setting.

First, forget about the delay() function.
Look at the examples in the IDE and get familiar with Blink Without Delay example.
Then think about what you want.

Tom.... :slight_smile:

is it possible to use native PWM pins at 50Hz and 50% duty cycle? :confused:
if i remember correctly most pins give out 60Hz by changing the prescalar value.

There have been a lot of inverter questions come through these forums in the last few weeks. Unfortunately most of them don't identify that they're building an inverter and ask a poorly-formed question about synchronising two outputs.

Look in the forums more closely.

Then look for more information on the "waveform mode" of the PWM units. It's not entirely clear from the datasheet but it can control two pins from each timer, because it is intended to do exactly what you want.

Probably a stupid question, but surely using PWM on 50Hz is going to give a terrible output.

Don't most inverters use PWM at much higher frequencies, to attempt to produce an approximation of sine wave output

If you want some code to only execute once then just put it in the void setup().