I have Arduino and Matlab which has hardware support package for Arduino. I want to create SPWM signal (sinusoidal pulse width modulation) to be the output of the Arduino board. I could generate the signal required in Matlab using this code
function spwm = SinWave(frequency) nsamples = 1250 * frequency; t = linspace(0,1,nsamples); sn = sin(2*pi*frequency*t); st = sawtooth(2*pi*frequency*10*t); spwm = abs(sn) > abs(st); plot(t, sn); hold on; plot(t, st); plot(t,spwm); axis([0,1,-1.2,1.2]);
Now SPWM has the samples for the signal, I tried sending it on pin 13 using the following function
function writeSPWM(arduino, spwm) for k=1:length(spwm) writeDigitalPin(arduino, 'D13' ,spwm(k)) end
Then I used the following two lines in command window
a = arduino() writeSPWM(a, SinWave(5))
I getting my signal shape with very low frequency(it is really much bigger) Is there a better way to achieve my goal? using Matlab is necessary but I have no problem if I have to combine coding throw Matlab and Arduino C.