Arduino and Matlab - Motor PWM and Encoder Channel Digital Pin 3 (D3) Conflict

Hi all,

So I will try to ask here as I didn't get any answers on Mathworks forums.

I have an Arduino Uno Rev3 board and also Arduino motor shield. I'm trying to run a 6V DC motor with magnetic rotary encoder and get the signals from it.

With Arduino Create or local IDE I am able to control PWM on pin 3 for motor channel A and to set pins 6 and 7 to read outputs from encoder channels A and B (not to be confused with motor channel A, obviously).

Now, when it comes to doing the same task in Matlab something strange happens. Matlab rotaryEncoder connection accepts only digital pins 2 and 3 to be used with interruption, i.e. to read the signals off of encoder. And trying to set these two to some other pins will result in an error explicitly stating that D2 and D3 pins must be used for this purpose.

The issue is, this is in conflict with motor channel A PWM speed control, or even before speed control with starting the motor at all. If I write D3 pin as encoder input I obviously cannot use it as motor output signal for start/stop output. Writing any other free digital pin to be PWM does nothing, which indicates that this has got something to do with board internals Matlab cannot get through to.

I bypassed this by connecting the motor to channel B (PWM B on pin D11 and DIR B on pin D13) and connecting encoder channels A and B to pins D2 and D3, respectively, as specified in the documentation linked above. But this left me wondering if anyone else encountered the same problem? I understand this is more a question for Mathworks and how they built their libraries but as I said I got no answer over there.

On a side note but perhaps also related, I cannot make Matlab's dcmotor connection to work. I am able to initialize it without any errors and I even get correct readings in the console window but it cannot actually get the motor to move. At the same time, I am able to start and stop the motor simply by sending writeDigitalPin(arduinoObject,'D3',1) or 0 from Matlab to the motor. I'm pretty baffled by Matlab's implementation of Arduino connection libraries.

I'd appreciate any thoughts or experiences being shared here.

Thanks.

When using MATLAB you are stuck with whatever decisions Mathworks made.

My approach would be to write Arduino code to read the encoder, control the motor, and if data need to be sent back and forth from MATLAB, just use a serial connection.