Changing PWM frequency of Arduino board in a .m script

6 次查看(过去 30 天)
Hi all,
As you might know, the PWM frequency of an Arduino board can be changed by adding code to a S-function builder block in Simulink. However, I prefer using a .m script when possible since it runs faster than Simulink. When I check the website: https://au.mathworks.com/help/supportpkg/arduinoio/ref/writepwmvoltage.html, it seems there is no way to specify the PWM frequency. Is there a function in the MATLAB Arduino library which can control the PWM frequency? Thanks in advance.

采纳的回答

Sarvesh Kale
Sarvesh Kale 2022-12-14
Hello Matthew,
As per my understanding you are trying to change to PWM frequency on the Arduino Uno board and looking for a MATLAB built-in function to do that. There is no built-in function to change the frequency however you have the following functionality available to you.
  • Using writePWMDutyCycle, you can change the PWM duty cycle. More information can be obtained by executing help writePWMDutyCycle on MATLAB prompt. The help information displays the inputs to writePWNDutyCycle function.
  • To get a list of available function use help arduino
  2 个评论
Matthew Tieu
Matthew Tieu 2022-12-14
Hi Sarvesh,
Thank you for responding back. Since there is no explicit function, can pieces of code from other languages be included in a .m script (similar to C Function Builder in Simulink)? I know in the Arduino language, the code below changes the PWM frequency from 490 Hz to 7812.5 Hz.
TCCR0B = (TCCR0B & 0b11111000) | 0b00000010; // Sets clock divisor to 8 on register B

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by