ArduinoGeneral

FIT0186 encoder

userHead daniel.brownell 2021-06-30 04:08:04 892 Views1 Replies
Hi

I'm trying to get the built-in encoder working for the FIT0186.
https://wiki.dfrobot.com/12V_DC_Motor_2 ... __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?


Code: Select all
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++;
}
2021-07-11 05:39:18 I had Hall sensor GND and VCC backwards. Oops. userHeadPic daniel.brownell