FSHS Robotics

Back to Outline

FSHS Robotics C Course

NXT: Motor Encoder

A motor encoder is a device built in to the motor that converts the position of rotation of the motor into data. Physically, it is a disk with many gaps – and a laser counts the number of gaps that have passed by a point. This determines how many degrees a motor has turned.

The NXT motors let you read the motor encoder values as if it was a sensor or a motor:

nxtDisplayTextLine(1, "%d", nMotorEncoder[motorLeft]); // show on screen the rotation 'count'
nMotorEncoder[motorLeft] = 0; // reset the count

This motor encoder value is very useful!

  • It is more accurate than wait – if you want to traverse half a metre, the rotation count will be constant. If you use wait, it depends on how fast the wheels are moving – which can change if you have low battery.

  • It lets your movements be more precise – you can turn exactly 180 degrees if you wanted to. Motor encoders are generally very accurate.

Sample program

#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorB, motorR, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main() {
        nMotorEncoder[motorL] = 0; // reset the encoder
        while(nMotorEncoder[motorL] < 3000) { // keep running until encoder hits exactly 3000
                motor[motorL] = 100;
                motor[motorR] = -100; // rotate the robot around clockwise
        }
        // stop the robot
        motor[motorL] = 0;
        motor[motorR] = 0;
}

© Fort Street High School Robotics