Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Using encoders to follow a path


Like would to use encoders say go fwd then turn and fwd.
I have an Astar prime pololu encoders and encoder.h and everything is working
fine. The problem is I don’t know how to make use of the readings. I don’t even rate hack when it comes to programing. I used an if statement to go fwd and stop and reset but can’t figure out what to do next. Maybe one of you smart guys or girls head Me in the right direction.
Thanks in advance


Hello, Bill.

Can you post your entire code so that I can see what part of your code you are referring to?

- Amanda


OK the following code is the example for the encoder library I found
in this forum for A star Prime. Your code is only good for uno. What little change I’ve made is at the bottom which will move both motors fwd until it reaches 3000 ticks. What I would like to be able to do is plot a course using the encoders instead of delays(fwd, turn left, turn right ,ect.)
the bot has 2 150-1 micro metal gear motors with encoders dvr8835dual motor carrier and an Astar Prime.
Any help would be appreciated

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 * This example code is in the public domain.
#include <DRV8835MotorShield.h>
#include <Encoder.h>
DRV8835MotorShield motors;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder LeftWheel(2,4);
Encoder RightWheel(3,5);
//   avoid using pins with LEDs attached

void setup() {
  Serial.println("Basic Encoder Test:");
// motors.setM2Speed(300);   

long positionLeft  = -999;
long positionRight = -999;

void loop() {
   long newLeft, newRight;
  newLeft = LeftWheel.read();
  newRight = RightWheel.read();

  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(", Right = ");
    positionLeft = newLeft;
    positionRight = newRight;
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.println("Reset both knobs to zero");

if( positionLeft>3000)


We did not write the “Basic Example” code for the PJRC encoder library and do not have any examples using that encoder library for the A-Star 32U4 Prime.

Can you elaborate what you mean by “code is only good for uno”?

If you are asking how to use the encoders to make a precise turn angle (e.g. 90 degree turn), there are a few ways you can go about it. Probably the easiest and quickest way to determine the ratio between the encoder counts and turn angle is to manually move your robot 90 degrees and document the count difference for each encoder from the robot’s initial position to its final position. Then use that value as a stopping condition when turning right or left. You might find this “Using encoders to make turns” tutorial on ROBOTC helpful.

- Amanda


The code I was talking about is the “PolouWheelEncoders.h” which works with UNO and can be adjusted to work on a Mega. Ok I had found the following link http://www.robotc.net/wikiarchive/Tutorials/Arduino_Projects/Mobile_Robotics/BoeBot/Using_encoders_to_drive_some_distance prior. Anyway Amanda I’ll take a look at the link you sent as I said before I’m very limited with programing and maybe I’m asking a little to much and not being very clear. If I get stuck I’ll try to ask a more specific question.