Send a sine wave out of matlab to arduino

15 次查看(过去 30 天)
I want to output the values from a sine wave to generate pulses for an h-bridge but don't know hot to send them one by one. is it possible for some help?
Code i did till now is attached below:
arduino=serial('COM2','BaudRate',9600);
fopen(arduino);
frequency = 1; % required frequency
period = 1/frequency; % required period
%amplitude = 127 ; % required amplitude
phase = 0; % required phase angle
offset = 127; % required DC offset
t = 0:0.00001:2*period;
output = 127 * sin(2*pi*frequency*t + phase) + offset;

回答(1 个)

Gautam Vallabha
Gautam Vallabha 2012-4-9
I assume you want to output the sinusoid values to an "analog output" (PWM) pin.
Take a look at MATLAB Support Package for Arduino. It allows you to send commands from MATLAB to Arduino, e.g.
a = arduino('COM9');
a.pinMode(13,'output'); % digital output
a.pinMode(9,'output'); % "analog output" (PWM)
% output the digital value (0 or 1) to pin 13
a.digitalWrite(13, 0)
% ouptput value on digital (pwm) pin 5
a.analogWrite(9, 120)
(The above code is from examples/example_io.m in the support package).

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by