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);
}
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??????
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.
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.