How to test Arduino Fast PWM Signal with Matlab/Simulink

 Here it is demonstrated how to use Matlab/Simulink to test the Fast PWM signal generated by Arduino from the previous tutorial Fast PWM with Arduino Programming & Testing. The previous tutorial explained how Fast PWM works, its mode and provided Arduino program code to generate inverting and non-inverting Fast PWM signal using Arduino. The generated PWM signal will now be acquired through sound card using Matlab/Simulink and the waveform displayed on time scope and the signal frequency displayed on spectrum analyzer in real time. 

The following two videos demonstrates how to use Simulink to display the two inverted and non-inverted Fast PWM wave generated by Arduino.

Non-inverted Fast PWM wave demo:

Inverted Fast PWM wave demo:


 To acquire the PWM signal in Simulink and display on time scope for waveform view and spectrum analyzer to see the frequency information we can use the following Simulink block diagram.

Simulink block diagram to connect Arduino to Simulink

The block program contains of audio device reader from the audio toolbox library, time scope and spectrum analyzer blocks from DSP toolbox library.

In the first example code we generated non-inverted Fast PWM signal on pin 6 of Arduino with frequency of 976Hz and duty cycle of 75%. The hardware connection from Arduino to PC port is shown below.

Arduino Simulink hardware connection through PC

If we run the simulation we can see the 976Hz PWM signal on the time scope in real time which is shown below.

Arduino generated Fast PWM in Simulink

 The following shows the frequency spectrum of the PWM wave in spectrum analyzer. Using the spectrum analyzer we can see the peak is at 975Hz.

Arduino generated PWM wave on spectrum analyzer


Similarly, in the second example in the tutorial we generated a non-inverted Fast PWM wave on Arduino pin 5 with frequency of 244Hz and duty cycle of 65%. The following shows the hardware connection setup between Arduino and Simulink using PC audio jack. This is similar to above setup but the pin has to be changed from 6 to 5 since we are now using OC0B pin to output the PWM which is Arduino pin 5.

The following shows the waveform of this PWM wave obtained in real time in time scope.

Arduino generated Fast PWM in simulink

Using the spectrum analyzer we can view the frequency of the signal which occurs at 234Hz using the peak finder tool.

In this tutorial we have used Matlab/Simulink to acquire PWM signal generated by Arduino into Simulink and analyze the signal. Matlab/Simulink can also be used as function generator and oscilloscope to do the reverse- that is testing a given electrical circuit. The following are some of example tutorial where Matlab/Simulink is used as oscilloscope and function generator to test a circuit.

- How to use Matlab Simulink as Oscilloscope

- Build LM324 inverting amplifier and test with Matlab/Simulink Oscilloscope

- Square wave generator using LM358 

Post a Comment

Previous Post Next Post