MATLAB x Arduino: "writePWMVoltage" not working
5 次查看(过去 30 天)
显示 更早的评论
Hi there, so this question is a bit tricky as it requires that you know a bit of Arduino and MATLAB. The issue is that I have written some code in Arduino that allows me to run a stepper motor between two Hall Effect sensors. This code (below) works beautifully and the hardware does everything that it's supposed to.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/355414/image.png)
However, when I then try to translate everything into MATLAB language (below), instead of performing the same action the stepper motor sort of just clicks softly and doesn't move at all. Looking at the voltage source, however, the current is oscillating up & down (as it should for this driver), but not at the values it does when it's truly operational. Any suggestions? Also the reason why I'd like to use MATLAB (other than the fact that I love it), is because I want to create a GUI that allows user to control the stepper motor and plot some live data simultaneously. I don't believe I can do this in Arduino language.
%% BOARD SETUP
clc, clear , close all
port = 'COM7';
a = arduino(port,'Uno');
configurePin(a,'A0','AnalogInput'); % pressure transducer
configurePin(a,'D4','DigitalInput'); % upper limit switch
configurePin(a,'D5','DigitalInput'); % lower limit switch
configurePin(a,'D8','DigitalOutput'); % stepper motor
configurePin(a,'D9','DigitalOutput'); % stepper motor
writeDigitalPin(a,'D8',0); % stepper motor hold
writePWMVoltage(a,'D9',0); % stepper motor hold
% Valid PWM Pins: "D3", "D5-D6", "D9-D11".
%% RUN
while 1
writePWMVoltage(a,'D9',5);
pause(0.1)
writePWMVoltage(a,'D9',0);
pause(0.1)
while readDigitalPin(a,'D5')==1 && readDigitalPin(a,'D4')==0 && readDigitalPin(a,'D8')==1
writeDigitalPin(a,'D8',1); % travel UP
drawnow
end
while readDigitalPin(a,'D5')==0 && readDigitalPin(a,'D4')==1 && readDigitalPin(a,'D8')==0
writeDigitalPin(a,'D8',0); % travel DOWN
drawnow
end
end
Some things I've tried:
- MATLAB x Arduino connection works: when I put a magnet in front of either sensor and readDigitalPin of that sensor, the value is either 1 or 0 depending of the presence of hte magnet.
- Replacing the two nested "while" functions with "if" functions - no effect
- Removing the configurePin lines - no effect
- Trying writePWMVoltage(a,'D9',5) instead of writeDigitalPin(a,'D8',1) - no effect
Hardware I'm using:
- RattmMotor 23HS6430 Stepper Motor (cheap Nema 23 from Amazon)
- TB6560 Stepper Motor Driver
- Hall Effect Sensors
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/355417/image.jpeg)
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!