Unable to use N20 with encoder

I am using an Arduino Nano to control an N20 motor with an encoder with DRV8833 as the motor driver.

I have used the following code

#include<Encoder.h>
#include<DRV8833.h>
#define timeVar 10

Encoder myEnc(A0,A1);

DRV8833 driver = DRV8833();
long newPosition;

unsigned long currentTime;
const int inputA1 = 2, inputA2 = 3;
volatile int dir, finger, t, pos;

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  driver.attachMotorA(inputA1, inputA2);
  currentTime = millis();
}

long oldPosition  = -999;

void loop() {
  driver.motorAForward();
  delay(500);
  driver.motorAStop();
  
  
  newPosition = myEnc.read();
  if (1) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
  delay(5000);
  
  driver.motorAReverse();
  delay(500);
  driver.motorAStop();
  

  newPosition = myEnc.read();
  if (1) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
  delay(5000);
}

But the output is not consistent with the code.
Please find it attached.
Any help would be appreciated.

Can you point us to the specific libraries you use?

GitHub - TheArduinist/DRV8833: A Arduino-based library for the Pololu DRV8833 dual motor driver carrier for the DRV library.

and

GitHub - PaulStoffregen/Encoder: Quadrature Encoder Library for Arduino for the Encoder

How is the encoder wired? Is the encoder an open collector output? If so, are there pullup resistors on the encoder outputs?

Please post a data sheet for your exact encoder.

Well there's nothing there to suggest any inconsistancy - what the data does show is that the encoder doesn't move very much in 0.5s. Since you've given no details on the motor or encoder or its power source that might
be entirely normal. Or it might be a definite fault - can't tell from here.

Have you though to test the encoder by itself, and the motor drive by itself?

This is the motor as well as the encoder I am using. You'll find all the motor and encoder related specifications there. Also, the wiring has been made as per the mentioned encoder connections.

I am using a combination of 3 of these batteries by regulating is Voltage at 6V.
Yes, I have tested the encoder and the motor separately, both of them works fine independently.

How is the encoder wired? A drawing of your wiring would help.

Is the encoder an open collector output? If so, are there pullup resistors on the encoder outputs? Many encoders have phototransistor or Hall sensor as the output. They may require a pull-up resistor to work.

Find attached the wiring of the circuit.

You've provided 6V to the encoder logic - it needs the Arduino 5V supply I think.

Its worth tracing the wiring if possible, but I strongly suspect that only two wires go to the motor, the others
are for the encoder, and the Vcc wire must therefore be 5V Arduino Vcc.

I have done connections according to this link. It says 6V to VCC.