Arduino simulink gets wrong timing with pulse generator

5 views (last 30 days)
Hi, I'm facing a problem with the arduino package for simulink. I set up the model to fixed step discrete with a step size of 0.001 s. In the model I'm trying to do 2 things: blink an led each second(for 1 s the led is on and for 1 s is off); Print to the serial a number coming from a constant block of simulink. I tried setting the pulse generator pulse type time based, specifying the 2 seconds period with 50% pulse width, and also sample based, specifiyng the sample time of 0.001s (as the model) and imposing period of 2000 samples and pulse width of 1000 samples, to get the correct timing. Both ways i cannot get the led to blink with the correct timing, it seems like it is taking much longer, while i can see that the arduino is continuously printing the number on the serial, through the serial monitor of arduino ide after i deployed the code. Interestingly, if i delete the serial print block, the led blinks correctly, with both pulse types (time based and sample based). What am i not getting right? Thanks
I leave attached the simulink model
  2 Comments
Monalisha
Monalisha on 29 Apr 2024
What is the reason behind setting the solver as discrete (non continuous state), with a step size of 0.001s.
Can you please try updating the solver as auto(Atomatic Solver selection).
Requirement is to toggle a LED pin every one second, you can configure the pulse generator with
Pulse Type:Sample Based
Period: 2
Pulse Width:1
Sample time :1
Francesco Brescia
Francesco Brescia on 30 Apr 2024
Thank you for your answer. I used the 0.001 s to specify the loop time of the arduino. I tried to set the solver to "type = fixed-step" and "solver = auto(automatic solver selection)". I think i need to leave type as fixed step otherwise i get an error saying to change the setting to fixed-step. I also set the pulse generator as you suggested, while the constant block outputting to serial is set with a sample time of 0.01s, to publish to serial every 0.01s. With this configuration the Led is taking approximately 1.40 s to blink, not 1. If I delete the serial communication block, I can see the led blinking with pulses of 1 second as it should, but i would like to do both blink and serial communication. Any suggestions? Thank you

Sign in to comment.

Answers (1)

Abhas
Abhas on 4 Sep 2024
Hi Francesco,
It sounds like you're encountering a timing issue when trying to perform both LED blinking and serial communication simultaneously on your Arduino using Simulink. This issue can possibly happen due to high frequency. Here are a few suggestions to help resolve the problem:
  1. Adjust Sample Times: First, let's tweak the sample times. Serial communication can be a bit slow, so try increasing the sample time for the Serial Transmit block. Instead of every 0.01 seconds, maybe bump it up to 0.1 or even 0.5 seconds. This should reduce the frequency of data transmission and might help with the timing. Ensure the Pulse Generator block is configured correctly for your desired blinking rate. For a 1-second on/off cycle, the sample-based configuration should have a period of 2000 samples and a pulse width of 1000 samples, matching your current setup.
  2. Consider Hardware Limitations: Remember, the Arduino board has limited processing power. Running high-frequency tasks at the same time can cause timing issues. The following documentation suggests using longer sample times for PWM and serial communication to avoid overruns: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/pwmcommon.html#:~:text=If%20you%20use%20the%20PWM%20block%20along%20with%20the%20Serial%20Receive%20and%20Serial%20Transmit%20blocks%20in%20a%20Simulink%20model%2C%20use%20longer%20sample%20times%20to%20avoid%20
  3. Verify Pin configurations: Review the provided documentation link to ensure you are not exceeding PWM frequency limits or encountering conflicting pin configurations: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/pwmcommon.html#:~:text=to%20avoid%20overruns.-,This%20table%20specifies%20the%20upper%20limit%20of%20the%20output%20frequency%20that,980.4,-Ports
Hope this helps!

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!