Arduino PWM Block, Continously changing PWM Frequency

Hello everyone,
im currently trying to mirror a PWM Signal with a Arduino Nano 33 IoT.
I want to mirror the signal, because I want to be abled to manipulate the PWM duty cycle in certain situations.
Therefore I connected the Arduino to the Simulink environment. This works well .
In order to mirror it I am capturing the PWM duty-cylce and the frequency with the "Arduino Input Capture Block". Thist works reliably.
In order to put out the captured signal I use the "Arduino PWM Block" (because it uses the built in timers). The duty-cycle is fed to the block via the input of the block.
My Problem is:
The PWM Block does not have an input pin for the frequency. How can I feed the continously measured frequency into the PWM Block? Is it even possible to continously change the PWM frequency of the Arduino PWM Block?
It is important, that the frequency is continously updated, because it can change depending on the PWM source. As far as I know "to Workspace Block"s only refresh after the simulation.
Currently I am trying to use a global variable. I need to define the varibale before in the Workspace before running it on the Hardware. I try to write the frequency into the variable with a Matlab Function Block. But this does not work. The frequency of the output PWM does not change at all. And it seems like the variable is not changing as well
The Code is of the "Matlab Function Block" the following:
function y = fcn(u)
y = u;
I hope I made myself clear enough.
Thank you for your help!

 Accepted Answer

Hi @Lukas,
The issue being faced stems from how the standard Arduino PWM Block is architected. In most Simulink Hardware Support Packages, the PWM frequency is treated as a static configuration parameter, not a dynamic signal.
To achieve dynamic frequency mirroring, you must bypass the standard PWM block and interface directly with the hardware timers. Basically, you can create a custom Arduino PWM block. This allows you to wrap your hardware-specific C code into a neat Simulink block with input ports for Frequency and Duty Cycle.
Relevant Documentation for creating custom blocks for arduino:
Hope this is helpful.

1 Comment

I was facing a similar issue and this is really helpful, thank you

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024b

Asked:

on 9 Jan 2026

Commented:

Deo
on 13 Apr 2026 at 14:42

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!