Matlab and Arduino Digital Pin 3 - PWM vs. encoder

8 views (last 30 days)
I'm writing a motor control script for a 6V DC motor run by Arduino and its Shield board. I am able to make it start, stop, change rotation, etc.
I'm now moving on to PWM speed control and geting readings from the attached magnetic rotary encoder and this is where I hit a hurdle. Say I'm looking at running only one motor by Arduino (i.e. only motor supply and control on Arduino channel A is used, channel B is not used).
My Shield board has digital pin 3 designated as PWM A (this is also reinforced on this Arduino tutorials page). I am able to change motor speed in Arduino Create just fine.
Now, when it comes to Matlab rotary encoder code, documentation specifies pins D2 and D3 as the ones allowing interrupts required to read encoder readings. In Arduino Create I was able to read encoder outputs on pins D6 and D7, which are not used for anything else otherwise. But when I try the same thing in Matlab:
channelA = 'D6';
channelB = 'D7';
encoder = rotaryEncoder(a,channelA,channelB)
I get an error saying:
Invalid pin. Valid Interrupt pin numbers for board Uno are "D2-D3".
So this is consistent with the documentation. But this is also where the problem lies as D3 pin is where my PWM A modulation should happen. And also, I need to set this pin as either output (for speed control) or input (for encoder reading), I cannot do both.
On another Matlab page I saw that I can move PWM to pins 5 and 6, so I tried that next. My
a.AvailablePWMPins
code tells me that I have {'D3'} {'D5'} {'D6'} {'D9'} {'D10'} {'D11'} pins available for this purpose. Now, if I connect my encoder channelA (encoder position signal, not Arduino channel A power supply and control) wire to pin D3, motor will start and cannot be stopped, and I'm not quite sure how to start and stop the motor using pin D5 for an example instead of D3.
Needless to say, I'm very confused by this whole situation with pin D3. Arduino tutorial (and finally the physical print on the board) stresses that pin D3 is PWM A output, but Matlab requires D3 for encoder signal reading. Does this mean that I cannot control motor speed/PWM and read signals from the magnetic encoder at the same time? If not, please can someone point me to a resource to understand this better.
Thank you very much in advance, and please let me know if I can provide more info.
For what it's worth, this is the encoder I'm using:

Answers (0)

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!