18. Wheel Encoders

The PololuWheelEncoders class and the associated C functions provide an easy interface for using the Pololu Wheel Encoders, which allow a robot to know exactly how far its motors have turned at any point in time.

This section of the library makes uses of pin-change interrupts to quickly detect and record each transition on the encoder. Specifically, it uses the AVR’s PCINT0, PCINT1, and PCINT2 (and PCINT3 on the Orangutan SVP and X2) interrupts, and it will conflict with any other code that uses pin-change interrupts (e.g. OrangutanPulseIn).

For a higher level overview of this library and example programs that show how this library can be used, please see Section 3.l of the Pololu AVR C/C++ Library User’s Guide.


C++ and Arduino methods are shown in red.

C functions are shown in green.

static void PololuWheelEncoders::init(unsigned char m1a, unsigned char m1b, unsigned char m2a, unsigned char m2b)

void encoders_init(unsigned char m1a, unsigned char m1b, unsigned char m2a, unsigned char m2b)

Initializes the wheel encoders. The four arguments are the four pins that the wheel encoders are connected to. Each pin is specified using the IO_* keywords provided by the library (e.g. IO_D1 for an encoder input on pin PD1). The arguments are named m1a, m1b, etc. with the intention that when motor M1 is spinning forward, pin m1a will change before pin m1b. However, it is difficult to get them all correct on the first try, and you might have to experiment.

static int getCountsM1()

static int getCountsM2()

int encoders_get_counts_m1()

int encoders_get_counts_m2()

Returns the number of counts measured on M1 or M2. For the Pololu wheel encoders, the resolution is about 3mm/count, so this allows a maximum distance of 32767 × 3mm or about 100m. For longer distances, you will need to occasionally reset the counts using the functions below.

static int getCountsAndResetM1()

static int getCountsAndResetM2()

int encoders_get_counts_and_reset_m1()

int encoders_get_counts_and_reset_m2()

Returns the number of counts measured on M1 or M2, and resets the stored value to zero.

static unsigned char checkErrorM1()

static unsigned char checkErrorM2()

unsigned char encoders_check_error_m1()

unsigned char encoders_check_error_m2()

These functions check whether there has been an error on M1 or M2; that is, if both m1a/m2b or m2a/m2b changed simultaneously. They return 1 if there was an error, then reset the error flag automatically.

Related Products

Pololu 42×19mm Wheel and Encoder Set
Encoder for Pololu Wheel 42x19mm
Orangutan SVP-1284 Robot Controller (assembled)
Orangutan SVP-1284 Robot Controller (partial kit)
Orangutan SV-168 Robot Controller
Baby Orangutan B-328 Robot Controller
QTR-L-1A Reflectance Sensor (2-Pack)
QTR-L-1RC Reflectance Sensor (2-Pack)
QTR-3A Reflectance Sensor Array
QTR-3RC Reflectance Sensor Array
QTR-1A Reflectance Sensor (2-Pack)
Orangutan LV-168 Robot Controller
Orangutan SVP-324 Robot Controller (partial kit)
QTR-1RC Reflectance Sensor (2-Pack)
Orangutan SV-328 Robot Controller
Pololu 3pi Robot
Baby Orangutan B-48 Robot Controller
QTR-1A Reflectance Sensor
QTR-1RC Reflectance Sensor
Orangutan SVP-324 Robot Controller (assembled)
QTR-8A Reflectance Sensor Array
QTR-8RC Reflectance Sensor Array
Orangutan X2 with VNH3
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors