Madhu here for my blog post after a long vacation! For today's post, I am going to talk about a project that I had worked on as a fun learning project.
I came across this wonderful tutorial on Adafruit's website for controlling different types of motors using the V2 Motor shield. The detailed instructions make it easy to get started and I want to adapt these for the MATLAB Support Package (SP) for Arduino. With the support package, you can connect to your Arduino from a MATLAB session - to acquire data from or to send data to the Arduino. I am using the motor shield here to illustrate the use.
Once I made all the connections as shown in the figure below, I only had a few steps to follow.
The steps -
1) Create an Arduino object in MATLAB
arduinoObject = arduino('com4', 'Due', 'Libraries', 'Adafruit\MotorShieldV2')
2) Create an addOnShield object for the V2 Motor shield
addOnShield = addon(arduinoObject, 'Adafruit\MotorShieldV2')
3) Control the servo motor from port 1
servoMotor = servo(addOnShield, 1)
for angle = 0:0.2:1 % From minimum 0 degrees to maximum 180 degrees
writePosition(servoMotor, angle);
pause(1);
end
4) Control the stepper motor from port 1. Note that the servo port 1 is different from the Motor port 1 in Motor shield.
stepperMotor = stepper(addOnShield, 1, 200) % Example usage of stepper - stepper(shieldObject, portNum, StepsPerRevolution)
stepperMotor.RPM = 10;
move(stepperMotor, 200);
5) Control the DC motor from port 4
dcMotorObject = dcmotor(addOnShield, 4)
dcMotorObject.Speed = 0.2;
start(dcMotorObject);
Hope this will be useful to others as well, who are attempting to understand how to use MATLAB Support package for Arduino to control motors.