There are various methods for PWM generation in matlab simulink. Here we show one way to generate PWM(Pulse Width Modulation) signal for the application of Arduino Simulink co-simulation. The varying magnitude PWM pulses that will be generated will be used to control a LED brightness.
This is continuum of Arduino Simulink Tutorial 3 – PWM where we showed how to create PWM pulses using sine wave generator.
The alternative method to create PWM pulses is to use the Repeating Sequence block to create PWM pulses. The PWM pulses from Repeating Sequence are feds the PWM block.
The simulation model is as shown below.
The setting in the Repeating Sequence block is shown below.
We have provided a vector for time from 0.1 to 1 and corresponding random amplitude at those times between 0 and 255.
Now connect the hardware as shown below which is the same in the previous PWM tutorial.
In the figure, a LED is connected to the PWM pin 5 on the Arduino UNO board. A 220Ohm resistor is used to ground the Pin at its negative terminal. Thus PWM signals from simulink using the repeating sequence block will send to the pin 5 via USB connection.
Once the hardware is setup, connect the Arduino USB to the computer.
The next step is to configure the Simulink to work with Arduino UNO board. How to do this was explained in the previous tutorials so we are not going to explain it again here.
Once the simulation setting are done you are ready to simulate the model. Click on the Run button on the toolbar. Matlab/Simulink will analyze the model, compile/generate the code and send to the Arduino Microcontroller via USB. After this you should see the LED glow according the pulses magnitude strength. The LED will glow brighter and dimmer according to the amplitude of the PWM pulses we have created using the Repeating Sequence block.
See the following video how the LED glows and dims.
So in this way, you can generate PWM signal for PWM block in Simulink for Arduino Simulink co-simulation.
For more Arduino Matlab/Simulink tutorial see Arduino Matlab Simulink Tutorials