MATLAB Answers

How to change PWM Frequency of Arduino USING MATLAB SIMULINK Support package

124 views (last 30 days)
HARIS
HARIS on 11 Jun 2014
Commented: Joseph Humphreys on 26 Feb 2020
Hello,
i have problem with my project. I use Arduino uno and Simulink. For the control i need a PWM signal with 3000 Hz - 5000 Hz but the Simulink support package (PWM) has only 490 Hz and i can change only the duty-cycle.
What can I do?
I would welcome your response

  14 Comments

HARITA UPPAL
HARITA UPPAL on 16 Apr 2019
Facing the same issue, I am not able to generate more than 1 KHz PWM for arduino in simulink as well as in matlab code. Can anyone help in this regard?
Ken Noé
Ken Noé on 13 Aug 2019
I had quite the same problem (needed 5 - 25 kHz for my DC-motor to work), so I wrote my own S-function, which I will attach.
By using a prescaler of 8 for the timer 2, the outcoming frequency at D3 and D11 is approximately 7.8 kHz. You can also use a prescaler of 1 (to get 62.5 kHz) or 64 (to get 980 Hz), if you take a look inside the S-function -> Start --> TCCR2B.
You'll also need the following requirements for the simulink-model in order to work:
  • Arduino Uno
  • Matlab R2019a or newer
  • Add-Ons: Matlab+Simulink Support for Arduino, MinGW-w64 C/C++ Compiler
  • run in "External"-mode, Simulation stop time: "inf"
I hope this will help you with your project.
Best regards
Joseph Humphreys
Joseph Humphreys on 26 Feb 2020
@Ken Noé
Can you use your S-functions for an Arduino Mega 2650? I've tried using it for an Uno and it works but when I switch to the Mega 2650 there is no output from the PWM pin.

Sign in to comment.

Answers (1)

Greg Wolff
Greg Wolff on 15 Aug 2019
You cannot change the PWM frequency of the PWM block found in the Arduino hardware support package.
You can however create custom device driver blocks. There is a good resource here:
that has various examples, all built around supporting Arduino boards.

  0 Comments

Sign in to comment.