3 added 949 characters in body edited Feb 4 '17 at 16:44 50k4 3,49255 silver badges1919 bronze badges It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. EDIT: Here is a PWM tutorial for the arduino. for SPWM the duty cycle has to be changed frequently. You will not be able to change it for each cycle, but maybe for every 10 cycles. It will be still ok. You could start by calculating a sinus on the arduino using an interrupt that is set for maybe every 2 milliseconds. That interrupt should increment your time variable (make sure that it does not overflow, or it overlfows in a controlled manner and make sure to use some sort of scaling to get seconds). This will be the t from the sinus time signal equation: $$SPWM(t) = A\sin (\omega t + \phi)$$ A (amplitude) I think in this case is irrelevant. probably the phase shift ($$\phi$$) is also irrelevant. $$\omega$$ you can calculate form your frequency. $$\omega = 2 \pi f$$, where $$f$$ is the frequency. You set to constant value at first, afterwards you make function that gets it form the serial port form Matlab (make sure NOT to include this in the interrupt!, because it is slow) At the end of the interrupt you update the PWM duty cycle basted on the SPWM(t) value the interrupt calculated. I am sure there are many other approaches, but for me this seems the simplest one which delivers acceptable performance. Also, please make sure that the cycle time of the interrupt is longer than the runtime of the code in the interrupt. It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. EDIT: Here is a PWM tutorial for the arduino. for SPWM the duty cycle has to be changed frequently. You will not be able to change it for each cycle, but maybe for every 10 cycles. It will be still ok. You could start by calculating a sinus on the arduino using an interrupt that is set for maybe every 2 milliseconds. That interrupt should increment your time variable (make sure that it does not overflow, or it overlfows in a controlled manner and make sure to use some sort of scaling to get seconds). This will be the t from the sinus time signal equation: $$SPWM(t) = A\sin (\omega t + \phi)$$ A (amplitude) I think in this case is irrelevant. probably the phase shift ($$\phi$$) is also irrelevant. $$\omega$$ you can calculate form your frequency. $$\omega = 2 \pi f$$, where $$f$$ is the frequency. You set to constant value at first, afterwards you make function that gets it form the serial port form Matlab (make sure NOT to include this in the interrupt!) At the end of the interrupt you update the PWM duty cycle basted on the SPWM(t) value the interrupt calculated. I am sure there are many other approaches, but for me this seems the simplest one which delivers acceptable performance. It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. EDIT: Here is a PWM tutorial for the arduino. for SPWM the duty cycle has to be changed frequently. You will not be able to change it for each cycle, but maybe for every 10 cycles. It will be still ok. You could start by calculating a sinus on the arduino using an interrupt that is set for maybe every 2 milliseconds. That interrupt should increment your time variable (make sure that it does not overflow, or it overlfows in a controlled manner and make sure to use some sort of scaling to get seconds). This will be the t from the sinus time signal equation: $$SPWM(t) = A\sin (\omega t + \phi)$$ A (amplitude) I think in this case is irrelevant. probably the phase shift ($$\phi$$) is also irrelevant. $$\omega$$ you can calculate form your frequency. $$\omega = 2 \pi f$$, where $$f$$ is the frequency. You set to constant value at first, afterwards you make function that gets it form the serial port form Matlab (make sure NOT to include this in the interrupt, because it is slow) At the end of the interrupt you update the PWM duty cycle basted on the SPWM(t) value the interrupt calculated. I am sure there are many other approaches, but for me this seems the simplest one which delivers acceptable performance. Also, please make sure that the cycle time of the interrupt is longer than the runtime of the code in the interrupt. 2 added 949 characters in body edited Feb 4 '17 at 16:39 50k4 3,49255 silver badges1919 bronze badges It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. EDIT: Here is a PWM tutorial for the arduino. for SPWM the duty cycle has to be changed frequently. You will not be able to change it for each cycle, but maybe for every 10 cycles. It will be still ok. You could start by calculating a sinus on the arduino using an interrupt that is set for maybe every 2 milliseconds. That interrupt should increment your time variable (make sure that it does not overflow, or it overlfows in a controlled manner and make sure to use some sort of scaling to get seconds). This will be the t from the sinus time signal equation: $$SPWM(t) = A\sin (\omega t + \phi)$$ A (amplitude) I think in this case is irrelevant. probably the phase shift ($$\phi$$) is also irrelevant. $$\omega$$ you can calculate form your frequency. $$\omega = 2 \pi f$$, where $$f$$ is the frequency. You set to constant value at first, afterwards you make function that gets it form the serial port form Matlab (make sure NOT to include this in the interrupt!) At the end of the interrupt you update the PWM duty cycle basted on the SPWM(t) value the interrupt calculated. I am sure there are many other approaches, but for me this seems the simplest one which delivers acceptable performance. It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off. EDIT: Here is a PWM tutorial for the arduino. for SPWM the duty cycle has to be changed frequently. You will not be able to change it for each cycle, but maybe for every 10 cycles. It will be still ok. You could start by calculating a sinus on the arduino using an interrupt that is set for maybe every 2 milliseconds. That interrupt should increment your time variable (make sure that it does not overflow, or it overlfows in a controlled manner and make sure to use some sort of scaling to get seconds). This will be the t from the sinus time signal equation: $$SPWM(t) = A\sin (\omega t + \phi)$$ A (amplitude) I think in this case is irrelevant. probably the phase shift ($$\phi$$) is also irrelevant. $$\omega$$ you can calculate form your frequency. $$\omega = 2 \pi f$$, where $$f$$ is the frequency. You set to constant value at first, afterwards you make function that gets it form the serial port form Matlab (make sure NOT to include this in the interrupt!) At the end of the interrupt you update the PWM duty cycle basted on the SPWM(t) value the interrupt calculated. I am sure there are many other approaches, but for me this seems the simplest one which delivers acceptable performance. 1 answered Feb 4 '17 at 16:07 50k4 3,49255 silver badges1919 bronze badges It seems that all the code you wrote is in Matlab, and at each cycle it should communicate via the USB port with arduino to turn the pin on or off. This take a lot of time. The fastest solution would be to write the whole algorithm as an Arduino sketch, and only get the current desired frequency via the Serial (USB) connection from Matlab. Matlab would set the desired frequency, send it via serial to the arduino. From there the latency of the USB (Serial) connection would not intervene. When you do the Arduino programming, make sure to use the PWM pins in PWM mode and not as a simple digital output turned on or off.