MATLAB Answers

1

Using matlab support package for arduino with any motor shield

Asked by Derick Ghoda on 29 Jul 2017
Latest activity Commented on by Madhu Govindarajan on 6 Feb 2018
I have an L298N motor shield that I wish to use to drive a DC motor with the help of the MATLAB support package for arduino (NOT the Simulink support package). Browsing on the internet for an example code, all I've found are examples using the ADAFRUIT MOTOR SHIELD which is not the motor shield I would like to use. So my question is, is it possible to drive a DC motor directly in MATLAB without any DC motor shield on the market other than the ADAFRUIT MOTOR SHIELD ? If yes please help me with an example code.

  0 Comments

Sign in to comment.

1 Answer

Menghan 님의 답변 31 Jul 2017
Menghan 님이 편집함. 31 Jul 2017
 채택된 답변

Hi Derick,
From what I find about this L298N motor shield online, it seems to only need digital IO and PWM signal for direction and speed control. To drive it in MATLAB, simply replace digitalWrite to writeDigitalPin and analogWrite to writePWMVoltage or writePWMFrequency to convert any existing Arduino examples. Please refer to the MATLAB Documentation for correct input parameters to writeDigitalPin/writePWMVoltage/writePWMFrequency.
Hope this helps.
Thanks,
Menghan

  3 Comments

Thanks very much for your help, I've been able to attain my objective. now feel confident using MATLAB in my projects. Below is the code I'm using, inspired by the following tutorial from mathworks: https://www.mathworks.com/videos/plotting-live-data-of-a-temperature-sensor-using-arduino-and-matlab-121317.html
clear;
clc;
close all;
a = arduino('COM5','Uno','Libraries','rotaryEncoder')
encoder1 = rotaryEncoder(a,'D2','D3',64)
writeDigitalPin(a, 'D7', 1), writeDigitalPin(a, 'D8', 0)
writePWMDutyCycle(a,'D9',0.7)
%Création du fichier pour enrégistrer les données
fID = fopen('Demo1.txt','w');
% Acquire and display live data
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
% ax.YLim = [65 85];
stop = false;
startTime = datetime('now');
try
while (1)
rpm = readSpeed(encoder1)
% Get current time
t = datetime('now') - startTime;
%Écriture de données dans un fichier
fprintf(fID, '%f\t%f\n', datenum(t), rpm);
% Add points to animation
addpoints(h,datenum(t),rpm)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
catch
fclose(fID);
end
Hi sir. I'm using the OptoCoupler sensor as a speed sensor. In Arduino, I can calculate the rpm, but in Matlab, I cannot figure out. Can you explain about this coding?
From the above code the ones that are used to read the speed value in rpm are -
clear; clc; close all; a = arduino('COM5','Uno','Libraries','rotaryEncoder') encoder1 = rotaryEncoder(a,'D2','D3',64)
rpm = readSpeed(encoder1)
You should be able to do that as long as the two channels of the quadrature encoder are on pins D2 and D3. Post a separate question if this does not work for your specific sensor or if you get error messages, once you try this out.

Sign in to comment.