Arduino simulink gets wrong timing with pulse generator
4 views (last 30 days)
Show older comments
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
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
Answers (1)
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:
- 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.
- 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
- 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!
0 Comments
See Also
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!