Synchronization between two outputs

I am working on a project that requires synchronization between two outputs.

So my program is like

digitalWrite(2,HIGH);
analogWrite(3,20);

something like that.

However, the two outputs have a different frequency so they cannot synchronize. The difference is about twenty-something Hz. Is there anything I can do to synchronize the two outputs or to minimize the difference? Thank you.

Post the whole code ...

jasone111:
digitalWrite(2,HIGH);

That'd be about 0 Hz.

analogWrite(3,20);

That'd be about 490 Hz.

Now if you'd use pin 3 and 11 and use analogWrite on both, they'd be perfectly in sync.

This is the simplified version of my program.

void setup() {
pinMode(5,OUTPUT);
pinMode(3,OUTPUT);
}

void loop() {
digitalWrite(3,LOW);
analogWrite(5,30);
digitalWrite(3,HIGH);
}

If the signal is connected to an oscilloscope, there will be 11us difference between the two outputs. How can I correct it?

Step back a bit and explain what you are actually trying to do, this sounds to be an xyproblem. xyproblem.info

MarkT:
explain what you are actually trying to do,

That's what I said in the OP's other thread here, which may or may not be related.

hannah_mackinlay:
I think the OP should say what they're trying to do.

If you want two outputs to change state at the exact same state, make sure they're on the same PORT and use a register call setting or clearing both bit in the same instruction.

Of course it's hard to do this when one of the signals is PWM.

As you apparently have a steady delay between the two, make me wonder: does analogWrite() reset the counter related to that output (TCNT2 in case of pin 3)? If not, the delay should vary.