How to Read Rotary Magnetic Encoder Output When Using Arduino Shield?

Hi everyone,

I am a beginner with Arduino but I did make sure to read and follow How to use this forum guideline and I did an extensive search before asking the question here.

I have Arduino Uno Rev3 and a motor shield that I'm using to run a low power 6V DC micro gearbox motor. I'm using an external power supply and this part of the setup works just fine.

I'm using channel A, so I'm using pin 3 for PWM. I'm learning how to read feedback from the rotary magnetic encoder that comes with the motor. This is where I'm stuck, as most of the sample codes, tutorials, etc. consider the same pin 3 for encoder reading (usually together with pin 2), even on Arduino rotary encoders page. I am not sure how to use pin 3 for both motor PWM and encoder readings.

As I mentioned, I'm just a beginner so I'd like to understand the basics first before I move on to someone else's encoder libraries. Please can anyone share a simplest code or source that would help me write the code myself that will work for my setup.

For an example, I found this tutorial to be fairly good, except it is using information on encoder's pulse per rotation output, which I don't have from the manufacturer. Also, the same tutorial uses an updateEncoder variable in the interrupt method which I was not able to find the reference to. And so on, for different code samples, it is quite difficult to put it all together and make it work for my particular case.

So, just to make sure I provided all the information, please see below the information on the motor, the encoder and the simple code I'm using right now to run the motor. Please let me know if I need to provide any additional information.

Thank you in advance.

1000:1 Micro Metal Gearmotor LP 6V

Magnetic Encoder Pair Kit for Micro Metal Gearmotors, 12 CPR, 2.7-18V (HPCB compatible)

/*
Test code for 6V Low Power gear ratio motor via Shield
*/

const int motorPWM = 3; // pin PWM A
const int motorBrake = 9; // pin BRAKE A
const int motorDIR = 12; // pin DIR A

void setup() {
  
    pinMode(motorPWM, OUTPUT); // set pin PWM A to send signal out
    pinMode(motorBrake, OUTPUT); // set pin BRAKE A to send signal out
    pinMode(motorDIR, OUTPUT); // set pin DIR A to send signal out
    
    Serial.begin(9600); //set the serial speed of communication between Arduino and the PC
    
}

void loop() {
  
  analogWrite(motorPWM, 5); // set motor speed to the maximum
  digitalWrite(motorDIR, HIGH); // set motor direction, where HIGH = clockwise
  /*cdelay(5000); // run for 5 seconds  
  analogWrite(motorPWM, 125); // set motor speed to the maximum
  digitalWrite(motorDIR, LOW); // set motor direction, where HIGH = clockwise
  delay(5000); // run for 5 seconds
  onst int motorDIR = 12; // pin DIR A
  digitalWrite(motorBrake, HIGH); // brake motor
  delay(2000); // wait for 2 seconds
  digitalWrite(motorBrake, LOW); // release brake
  analogWrite(motorPWM, 20); // set motor speed to X
  delay(5000); // run at lower speed for 5 seconds
  digitalWrite(motorBrake, HIGH); // brake motor
  delay(2000); // wait for 2 seconds
  digitalWrite(motorBrake, LOW); // release brake */

}

That is a quadrature encoder. You can determine both speed and direction from it. Do you need direction or just speed?

I am guessing you only want speed and you will ignore channel B. Then the pulses per revolution will be 6.

Thank you for your reply.

May I ask how you figured out the number of pulses (for my reference, so I know what to look for in the future - I didn't find the information on Pololu's resources page)?

Eventually I will need both speed and direction.

Pololu says it is 12 ppr when using both outputs.