How to read encoder value from Lego NXT via Arduino

Hello everyone,

I am building a self-balancing robot and I want to use the Lego NXT motors with the Arduino. I want to use the encoders on the motor but I am unsure how to.

The NXT uses RJ-12 cables consisting of 6 wires,

  1. Motor Signal1
  2. Motor Signal2
  3. GND
  4. Power
  5. Encoder signal1
  6. Encoder signal2
  • I have connected the gnd and power to Arduino (GND & 5V)
  • Motor signal 1 & 2 are connected to motor driver (L298N)
    here is my code

int ENA = 9;
int in1 = 5;
int in2 = 8;
int encoder_val = 0;
int encoders1 = 7;
int encoders2 = A0;

void count(void);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //baud rate
pinMode(encoders1, INPUT);
attachInterrupt(encoders1, count, FALLING);
encoder_val = 0;
}

void loop() {
// put your main code here, to run repeatedly:
Serial.print(“Starting\n”);
Serial.print("Encoder Value= "); Serial.println(encoder_val);
}

void count()
{
encoder_val++;
}

9842.png