Generate Sine Wave using Arduino PWM and Simulink

 A Sine Wave can be generated using Arduino in number of ways. One way of generating Sine wave is using PWM. Here we will illustrate how to create Sine Wave using PWM signal from Arduino Uno using Matlab Simulink and using RC low pass filter. Most of the Arduino board supports PWM so this is workable for all Arduino board types(Uno, Nano, Due,Mega etc). 


Here Simulink is used to create a 400Hz sine signal sampled at 10,000 samples/sec and to send these sine wave samples to the Arduino Uno PWM pin 9. You can use any PWM pins(3, 5, 6, 9, 10 or 11). The sine PWM signal output from Arduino Uno pin 9 is then low pass filtered using 3rd order RC low pass filter with cutoff frequency of 426.8 Hz. Then we use the same Arduino Uno analog pin A1 to acquire the low pass filtered and display on time scope in Simulink.


Video demonstration of the things done here is provided at the end. 

Simulink Model

Below is the picture of Simulink model used to create sine wave signal samples, to send to Arduino Uno PWM pin 9 and receive the filtered signal(RC filter is external to Simulink) and plot on the time scope.

Generating PWM  sine wave using Arduino simulink

The sine signal generator block is configured with amplitude of 255/2 and bias of 255/2. This is so because the Arduino PWM block accepts PWM values in range 0 to 255 corresponding to duty cycle of 0 to 100%. The bias is required so that sine wave signal is non-negative. The frequency is set to 400 Hz and sampling time is set to 1/10000(for higher resolution). 

The Arduino PWM block is set to 9 and no other setting is required in here. The Arduino analog input block is set for 1 for analog pin 1 and the sampling time is set to 1/10000. The gain block is set to value of 5/(2^10-1) to map the incoming analog value(0 to 1023) to range 0V to 5V. The 2^10-1 is used because Arduino Uno ADC resolution is 10 bits and the total level of quantization is 1023(2^10-1) because counting starts from 0. The scope blocks does not need any settings. 

The configuration for the blocks is shown below.

Hardware Implementation

Below photo shows the hardware connection between Arduino Uno pins and the 3rd order RC filter. 


Sine wave generation using PWM and 3rd order LPF

Below picture shows arrangement of resistor and capacitor with their values.

3rd order RC LPF schematic

Here we have used 3rd order RC LPF with cutoff frequency of 426.8Hz, with three resistors of value 3.3KOhm and three capacitors of value 113nF. 


The cutoff frequency formula is:

\(f_c = \frac{1}{2\pi RC}\)

Observation & Results

After configuring the model and hardware implementation, the simulation can be run in Simulink using the Simulation tab > Run button or using the Hardware tab > Monitor & Tune button. 

When the simulation starts the time scope window will show the captured real time waveform. The following shows filtered analog signal on the time scope display.

Arduino PWM sine wave using simulink

Clearly this is a Sine wave. The staircase look is due to finite sampling instants and due to bit resolution of the Arduino Uno analog input as well. 


We can verify whether the signal frequency is 400Hz or not using the cursor measurement tool available in time scope. Following shows that the measured frequency from the scope is indeed 400Hz.

400Hz PWM sine signal from Arduino

To compare the original signal with the filtered and then acquired sine signal, we can use the following Simulink model.

simulink model for comparing generated sine wave and filtered sine wave

Then if we run the simulation(on hardware and in real time), we can see and compare the generated sine wave and filtered sine wave on the time display as shown below.

simulink model for comparing generated sine wave and filtered sine wave

Effect of Order of RC Low Pass Filter

We can experiment with RC filter order easily with Arduino and  Matlab/Simulink and see the effect of order of RC Low pass filter design in real time. We can acquire not only one analog signal as done before but we can acquire the output from the 1st order, 2nd order and the 3rd order RC filter. After acquiring the sine signal we can compare their performance. This is one advantage of using Arduino with Simulink as kind of DIY Oscilloscope

Shown below is Simulink model to acquire the analog signal from the three stages of RC filter.

analysis of order of RC filter using Arduino and Simulink

 The analog pin1, pin 2 and pin 3 are used to collect the filtered PWM Sine signal from the first order, second order and third order RC Low Pass Filter. Then we use the time scope to display and compare the sine signal filtered from the three stages.


The hardware implementation, that is, the connection of the 1st, 2nd and 3rd order LPF to Arduino Uno analog pins 1, 2 and 3 respectively is shown in the photo below.

signal aquisition from  1st,2nd,3rd order RC filter and arduino

After running the simulation, we can see the signal displayed on the time scope in real time. The picture below shows this. You can watch the video provided below to see this filtering and real time waveform display in action.

effect of order of RC LPF with Arduino/Simulink

As you can see the sine waveform(or signal) becomes smoother(less distortion and noise) as the order of the LPF increases from first to third order.

Video Demonstration

Below video shows how to setup simulink model, how to run the model, how the filtering works and how to observe the waveform for all order filters in real time.


 Summary & Conclusion

Here we have shown how one can generate Sine wave signal by RC low pass filtering of PWM signal generated by Arduino/Simulink. We have used 3rd order RC filter. But we showed how to acquire filtered PWM sine signal from the output of the 1st order, 2nd order and 3rd order RC filter using Arduino and Simulink and showed by comparison that higher order filter yields better sine wave signal.

Another method of generating sine wave is using DAC(Digital to Analog Converter) which was illustrated in last post Generate Sine Wave using Arduino DAC and RC Low Pass Filter. For using DAC of Arduino to generated sine wave, one needs Arduino board which has DAC capability like Arduino Due which has internal DAC.  

However, most of the Arduino board does not have DAC built in. Whereas most of the Arduino board have PWM capability so the technique described here for generating sine wave signal is useful for broader audience.

Post a Comment

Previous Post Next Post