Encoder variable only increasing regardles of what direction the motor drives on a-star boarsd

I have a motor (not sure what model but it is 12V) with encoders. The motor works perfectly fine and I have it running both directions easily with a DRV8825 motor driver board. I have all my other peripherals working, except the encoders.

Basically I have had them working before, but I moved from an arduino uno to an a-star 328pb board. Before what was happening was the encoder variable would go both directions depending on which direction the motor was turning (just like it should). But now with the same setup and code when I run it on the a-star, the encoder variable only goes up, instead of up and down, regardless of what direction the motor is running.

I can’t figure out how to fix it.

Here is my code for the motor encoder section.

int enc1 = 3;
int enc2 = 2;


setup() {
  // initialize encoder pins as an input:
  pinMode(enc1, INPUT);
  pinMode(enc2, INPUT);
  attachInterrupt(digitalPinToInterrupt(enc1), testISR, RISING);
  
}

void testISR() {
  int b = digitalRead(enc2);
  if (b > 0) {
    encValue++;
  }
  else {
    encValue--;
  }
  //Serial.println(encValue);
}

The motor encoders are connected to digital pins 2 and 3 on the a-star board.

What can I do to fix this?

Hello.

I do not see anything obviously problematic with your code. Have you tried measuring the signal on your enc2 encoder channel to verify that it is toggling between high and low when you move the motor shaft, as expected? You might also double check the connection to the A-Star 328PB, and that you have a solid common ground connection between the two. Also, could you post pictures of your setup showing all of your connections?

Brandon

Hey Brandon, thanks for always being here to support me with my tech problems. I ended up going out, coming home and it just started working again. I genuinely have no idea why, but I can’t seem to replicate the issue now. So I have no idea. :frowning:

1 Like

Hi, have you checked your power supply? Is it giving sufficient current and voltage to the motor driver board?

The power supply is a benchtop power supply, I did check it and it seemed to be having plenty of power to supply. So I don’t think that was it. I still can’t replicate the issue though, so I’m still unsure.

Is there any problem with the a-star 328pb board? Checked that separately?

1 Like

I don’t think there is an issue, but it hasn’t happened again so if/when it does I will be trying to debug it as much as I can and will revisit this thread! :slight_smile:

1 Like