Hello,
I have two 4.4:1 Metal Gearmotor 25Dx63L mm MP 12V with 48 CPR Encoder with a DualMC33926MotorShield and Arduino Uno. One encoder works perfectly fine. The other one was working fine normally (I think) but now has major issues during acceleration of the motor the encoder directions are going back and forth between clockwise (CW) and counterclockwise (CCW). The data sample below is of me rotating and accelerating the motor shaft CCW and you can see the numbers bounce between +1 (CCW - the correct direction) and -1 (CW). I was powering this motor with an 18V power source so maybe that caused some issues? Not sure if that makes sense since the other encoder is working fine and the encoder itself is powered from 5V from the Arduino.
Any advice to change my code or to repair the encoder? My code is below for reference.
19:03:26.309 -> right_Encoder_Direction_Now: 1
19:03:26.309 -> right_Encoder_Direction_Now: 1
19:03:26.309 -> right_Encoder_Direction_Now: 1
19:03:26.309 -> right_Encoder_Direction_Now: 1
19:03:26.309 -> right_Encoder_Direction_Now: -1
19:03:26.342 -> right_Encoder_Direction_Now: 1
19:03:26.342 -> right_Encoder_Direction_Now: 1
19:03:26.342 -> right_Encoder_Direction_Now: 1
19:03:26.342 -> right_Encoder_Direction_Now: 1
19:03:26.342 -> right_Encoder_Direction_Now: -1
19:03:26.342 -> right_Encoder_Direction_Now: -1
19:03:26.342 -> right_Encoder_Direction_Now: 1
19:03:26.376 -> right_Encoder_Direction_Now: 1
19:03:26.376 -> right_Encoder_Direction_Now: 1
19:03:26.376 -> right_Encoder_Direction_Now: 1
19:03:26.376 -> right_Encoder_Direction_Now: 1
19:03:26.409 -> right_Encoder_Direction_Now: 1
19:03:26.409 -> right_Encoder_Direction_Now: 1
19:03:26.409 -> right_Encoder_Direction_Now: 1
19:03:26.409 -> right_Encoder_Direction_Now: -1
19:03:26.442 -> right_Encoder_Direction_Now: 1
19:03:26.442 -> right_Encoder_Direction_Now: 1
19:03:26.442 -> right_Encoder_Direction_Now: 1
19:03:26.475 -> right_Encoder_Direction_Now: 1
19:03:26.475 -> right_Encoder_Direction_Now: 1
19:03:26.508 -> right_Encoder_Direction_Now: 1
19:03:26.541 -> right_Encoder_Direction_Now: 1
19:03:26.541 -> right_Encoder_Direction_Now: 1
19:03:26.574 -> right_Encoder_Direction_Now: 1
19:03:26.706 -> right_Encoder_Direction_Now: -1
19:03:28.296 -> right_Encoder_Direction_Now: -1
////////////////////////////////////////////////// CODE //////////////////////////////////////////
#include "DualMC33926MotorShield.h"
#include <Wire.h> // IMU IC2 Communication
DualMC33926MotorShield md;
#define encoder0PinA 2
#define encoder0PinB 12
volatile unsigned int encoder0Pos = 0;
int right_Encoder_Direction_Now = 1;
void stopIfFault()
{
if (md.getFault())
{
Serial.println("fault");
while(1);
}
}
void setup()
{
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(digitalPinToInterrupt(2), doEncoderA, CHANGE);
Serial.begin(115200);
Serial.println("Dual MC33926 Motor Shield");
md.init();
}
void loop()
{
}
void doEncoderA() {
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos++; // CCW
right_Encoder_Direction_Now = 1;
}
else {
encoder0Pos--; // CW
right_Encoder_Direction_Now = -1;
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos++; // CCW
right_Encoder_Direction_Now = 1;
}
else {
encoder0Pos--; // CW
right_Encoder_Direction_Now = -1;
}
}
Serial.print(" right_Encoder_Direction_Now: ");
Serial.println(right_Encoder_Direction_Now);
//Serial.print(" Encoder A: ");
//Serial.print(digitalRead(encoder0PinA));
//Serial.print(" Encoder B: ");
//Serial.println(digitalRead(encoder0PinB));
// use for debugging - remember to comment out
}