Encoder reading on console keeps increasing/decreasing with incorrect readings

Hello everyone, I really need help for my Arduino, I'm trying to read encoder signals from Channel A from my encoder which is attached to my motor to the Arduino, And for some reason, the encoder count keeps increasing rapidly without giving me an accurate value.

The encoder also keeps increasing/decreasing in value when the motor is stationary or not rotating.

I've attached a screenshot of my Console Window showing the values I keep getting

#include <TimerOne.h>
//ports
#define DIRmotor 6 //pin 6 of arduino output will control direction of motor
#define PWMmotor 5 //pin 5 arduino output will control speed of motor and max PWM is 185 since we are using 'h' or half step
#define encChA 18
#define encChB 19
#define enc2ChA 20
#define enc2ChB 21
#define CurrentMPin A1
volatile unsigned int encoder1Pos = 0;
void setup() { //this runs everything in this setup function once
  pinMode(DIRmotor, OUTPUT); //pin 6
  pinMode(PWMmotor, OUTPUT); //pin 5
  pinMode(encChA, INPUT); //pin 18
  pinMode(encChB, INPUT); //pin 19
  pinMode(enc2ChA, INPUT); //pin 20
  pinMode(enc2ChB, INPUT); //pin 21
  
  
  attachInterrupt(digitalPinToInterrupt(encChA), doEncoder, CHANGE);  // encoder pin on interrupt 5 - pin 18
  Serial.begin (9600);
  Serial.println("start");                
  
  
  delay(1000);
  driveMotor ('x', 20); //this turns the motor on to rotate clockwise with PWM value of 20
  delay(10000); // wait 10 seconds
  stopMotor (); //this turns the motor off with PWM value of 0
  
    
}
void doEncoder() {
  /* If pinA and pinB are both high or both low, it is spinning
     forward. If they're different, it's going backward.
     For more information on speeding up this process, see
     [Reference/PortManipulation], specifically the PIND register.
  */
  if (digitalRead(encChA) == digitalRead(encChB)) {
    encoder1Pos++;
  } else {
    encoder1Pos--;
  }
   //Serial.println(val, format), Serial: serial port object, format: specifies the number base 
  //(for integral data types) or number of decimal places (for floating point types), println() returns the 
  //number of bytes written, though reading that number is optional
}
void stopMotor(){ //just stops the motor
  analogWrite(PWMmotor, 0); //this turns the motor off with PWM value of 0
}
void driveMotor(char dir, unsigned int vel){ //this function is to drive the motor clockwise or anti-clockwise and dir is a character type variable and vel 
//is an unsigned integer which is PWM value
/*look from motor along shaft:
HIGH = counter clockwise
LOW = clockwise
is controlled via voltage only*/
  if (dir == 'c'){//clockwise, (roll tape measure out)
      digitalWrite(DIRmotor, LOW); //when a low voltage is sent to DIRmotor pin 6, then its turns clockwise
    }
  else if (dir == 'x'){//counter clockwise, (roll tape measure in)
      digitalWrite(DIRmotor, HIGH);
    }
  analogWrite(PWMmotor, vel);//from 0 - 255
}
void loop() { //this runs everything in this loop function on a loop
    Serial.print ("Encoder count: ");
    Serial.println (encoder1Pos);
    delay (200);
    
}

code10.zip (4.68 KB)

Just on this point:

void doEncoder() {
  /* If pinA and pinB are both high or both low, it is spinning
     forward. If they're different, it's going backward.

How are you detecting if it is stationary ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.