pwm synchronize with external event



i am trying to synchronize pwm output with analog input but my pwm output is driffted . can any one help me for this
below is my source code

code :

int i = 3;int led = 13;
int ad,j = 4;
int adstate ,ad1state,previousstate = 0,pad1state;

void setup()
// Serial.begin(9600);
// myTimer.begin();


void loop()

// analogReference(DEFAULT);
adstate = analogRead(A0);
ad1state = analogRead(A1);
// map(adstate,0,1023,0,1023);
// Serial.println(adstate);


if (adstate!= previousstate && adstate > 50)



else if(adstate < 50)
previousstate = adstate;
The Teensy's PWM is generated by one of the internal Flextimer modules which definitively allow syncing PWM signals, i.e. with interrupts or other H/W events, but there is actually no library and no Arduino-ish function which makes use of these features. You'd have to do some low level C or C++ programming with the informations about Flextimer programming found in the Teensy's reference manual. For the Teensy 3.1, it is from page 769.
Yes, if you dig into the low-level hardware programming, you can have the timer trigger the ADC. Or you can make the timer generate an interrupt, which you use to run code.

Without tough low-level programming, a simpler hack might involve just connecting the PWM signal to an unused pin, which you configure in INPUT mode. Then instead of using delay() you can use digitalRead() or attachInterrupt() on that input pin, you make your code respond in sync with the PWM signal.
thank u for reply. but for low level programming require more time & as paul said i can't understand how pwm pin used as input can u explain more
I think it is unclear what you mean by "synchronize pwm output with analog input"

Is the analog input periodic with high and low parts similar to a pwm signal ?

Does the pwm hava a given period or dutycycle, or should it adjust to the analog input ?

Do you want to use the analog signal to trigger the pwm?

Syncronization can also mean that the signals have different frequencies, but they can be phase locked, should the rising edge of the analog always coincide with the start of a pwm pulse?

Perhaps its like a Schmidt trigger: if analog in larger than upper hysterisis set output high, if input less than lower hysterisis set output low. This can work for a nice clean analog input, but you might also need to add minimum times in new state before changing the output.

So I think you must describe your problem in more detail, what is the nature of the analog input and what do you want from the pwm output.
Last edited:
actually my input is AC sine wave & my output is pwm . i want synchronize means my pwm is started at the starting point of AC sine wave . i have to use this for scr firing to fire scr at diffrent pulse.
Keeping the pwm in sync with the input AC means watching it - there may be a better way - but (can somebody confirm this is sane) . . .

When I read p#6 I thought of ADC LIB - doing a continuous read assuming it had a ISR that could be used to monitor for peak/desired ADC value and then act accordingly.

In examples / adc / analogContinuousRead I see: adc0_isr(void)

You could set a flag or perhaps trigger an interrupt and exit that routine.