0

I've posted to the DFRobot forum, who makes the FIT0186 motors, but no one seems to be replying there. So, excuse the copy pasting. It's probably an issue with the motor/encoder, but perhaps it has to do with the arduino. I'm using a Nano, and a TB6612 based motor driver from DFRobot.

I'm trying to get the built-in encoder working for the FIT0186.

The example wiki code doesn't work, and I've modified it as far as I can using sensible changes. I'm plugging both Hall Sensor A and B outputs to the interrupts.

Currently, when I plug in the 12V, it triggers an interrupt, but then other than that initial pulse, no interrupts are triggerred.

The motor is moving as expected. Attaching my code. Has anyone got the encoder working?

const byte encoder0pinA = 2;
const byte encoder0pinB = 3;
volatile int posA;
volatile int posB;


int DIR1 = 8;
int PWM1 = 9;

void setup()
{
  Serial.begin(115200);
  pinMode(DIR1, OUTPUT);
  pinMode(PWM1, OUTPUT);
  pinMode(encoder0pinA,INPUT);
  pinMode(encoder0pinB,INPUT);    
  attachInterrupt(digitalPinToInterrupt(encoder0pinA), encoderA, RISING);
  attachInterrupt(digitalPinToInterrupt(encoder0pinB), encoderB, RISING);

}

void loop()
{
  Serial.print("Pulse:");
  Serial.println(posA);
  Serial.println(posB);
  
  noInterrupts();
  posA = 0;
  posB = 0;
  interrupts();
  
  delay(100);

  int value;
  digitalWrite(DIR1, HIGH);
  
  for(value = 0 ; value <= 255; value += 5)
  {
    analogWrite(PWM1, value);   //PWM Speed Control
    delay(20);
  }
  
  Serial.print("Pulse:");
  Serial.println(posA);
  Serial.println(posB);

  delay(100);

  for(value = 255 ; value >= 0; value -= 5)
  {
    analogWrite(PWM1, value);   //PWM Speed Control
    delay(20);
  }
  

  Serial.print("Pulse:");
  Serial.println(posA);
  Serial.println(posB);
  delay(100);
  
  digitalWrite(DIR1, LOW);
  for(value = 0 ; value <= 255; value += 5)
  {
    analogWrite(PWM1, value);   //PWM Speed Control
    delay(20);
  }
  

  Serial.print("Pulse:");
  Serial.println(posA);
  Serial.println(posB);

  delay(100);
  for(value = 255 ; value >= 0; value -= 5)
  {
    analogWrite(PWM1, value);   //PWM Speed Control
    delay(20);
  }
  
}


void encoderA()
{
    posA++;
}


void encoderB()
{
    posB++;
}
2
  • that is probably a quadrature encoder ... it has two channels, A and B ... monitor state of A ... at one of the signal edges, check the state of B ... that will tell you the direction of rotation ... for example, check the state of B every time A goes from high to low
    – jsotola
    Jul 7, 2021 at 16:24
  • Hmm so that was interesting. When checking now, B increases. But then when I touch D4, it goes to 0. Oh boy, magic hour.
    – djb
    Jul 7, 2021 at 20:18

1 Answer 1

1

From what you have indicated (being wired correctly) there is a high probability you have a bad connection. It could be one of many things such as a loose wire, broken wire, problem with the module, etc... Swp A & B and see what happens.

1
  • Thanks, I had the hall sensor gnd and vcc backwards.
    – djb
    Jul 9, 2021 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.