Polulu Encoder with Arduino Problem

Starting a new topic because none of the available topics on pololu encoder problems with the arduino were able to solve my problem. Didn’t think that rezzing a dead thread would be a good idea.

I have a Arduino Uno (Rev.3) with a Polulu 19:1 motor + encoder: pololu.com/catalog/product/1442
Feeding 4.8V to the Encoder VCC.

The following code:

#include <PololuWheelEncoders.h>

PololuWheelEncoders encoders;

void setup(){
  Serial.begin(9600);
  encoders.init(2,3,255,255);  
}

void loop(){
  Serial.print(encoders.getCountsM1());
  delay(500);
}

Everything compiles and uploads successfully. I see a response on the serial port when I turn the motor manually. However, the values returned are 0s, 1s and -1s; The true count is never returned successfully.

I’ve tried all possible combinations of initialization of pins (EG: init(3,2,255,255); init(2,255,3,255) etc…) No change.

Any help would be appreciated.

Hello,

Could you describe how you have the encoder connected to the Arduino? Do you have a picture of your setup showing your connections?

- Kevin

It’s very messy and my not be of much use, but here it is:

Here are the pin mappings for my Digital IO on my Arduino:

5 & 6 are PWM waves to a dual channel MC33926 motor driver carrier (also by pololu :slight_smile: ) for rightside motor.
10 & 11 are also PWM waves to the motor driver carrier (IE: the other motor) for leftside motor.
Rightside motor’s yellow encoder wire is connected to digital IO pin 2. Rightside motor’s white encoder wire is connected to digital IO pin 3.
Leftside motor’s yellow encoder wire is connected to digital IO pin 13. Leftside motor’s white encoder wire is connected to digital IO pin 12.

The above set up works (in the sense that turning the wheel one way or another increases/decreases the count) for the RIGHTSIDE motor.

Using the PololuWheelEncoder class I write a quick test program as:

#include <PololuWheelEncoders.h>

PololuWheelEncoders encoders;
int count;

void setup(){
  Serial.begin(9600);
  
  count = 0;
  
  encoders.init(2,3,255,255);
  encoders.getCountsAndResetM1();
}
void loop(){
  if(encoders.checkErrorM1())
  {
    Serial.print("ERROR");  
  }
  
  count = encoders.getCountsM1();
  delay(100);
  
//  Serial.print(count);
  Serial.print(count);
  
  //Newline serial write
  Serial.write(10);
  Serial.write(13);
  
  delay(500);
}

However, while the RIGHTSIDE motor’s encoder works well in the above specific setup, the LEFTSIDE motor’s encoder returns 0 and -1 and ERRORS (using the checkErrorM2())

I think it’s something to do with the changed Interrupt Services provided in the Arduino Uno Rev.3 that has the interrupt->pin mappings declared incorrectly in the PololuWheelEncoders.cpp file. Any useful documentation on how to rewrite it?

Funny things I noticed…

If I swap the LEFTSIDE motor’s yellow and white encoder wires (IE: Now yellow->12 and white ->13) my RIGHTSIDE motor’s encoder stops working.

Switching my RIGHTSIDE (working) yellow and white encoder wires from (2,3) to (4,7) also has the RIGHTSIDE motor encoder working. Of course, you need to change the encoder object initialization to encoders.init(4,7,255,255) instead of encoders.init(2,3,255,255).

When testing both encoders together I’ve also tried instantiating another encoder object.
IE: instead of

encoders.init(2,3,13,12)

I have

encoders.init(2,3,255,255)
encoders.init(13,12,255,255)

No luck however…

In all above mentioned examples I checked and double checked that I’m using the right pairs corresponding to getCountsM1() and getCountsM2() in getting my counts.

IE: In scenario “encoders.init(2,3,13,12)” I use getCountsM1() for pair (2,3) and getCountsM2() for pair(13,12).

If its important, I’m using the Arduino AVR ISP mkII programmer. I tried different programmers available in the Arduino 1.0 IDE as well (AVR ISP, Arduino ISP) but no such luck.

Any advice/comments/ideas on stuff I could try would be well appreciated. Sorry for the lengthy post.

Further research yields the following (please be patient if I mention anything contradictory or simply ridicolous. My understanding of hardware interrupts amounts to some signal that triggers some interrupt routine):

The Arduino Uno has only two external interrupt pins (Digital IOs 2 and 3). However, I don’t expect this to be a problem as I believe the PololuWheelEncoders library maps non external interrupt pins as interrupt pins and does the count processing in-class.

All other peripherals don’t work properly with the encoders attached. IE: I have a ultrasonic sensor + code. Sometimes the sensor doesn’t pulse and the only way to “fix” it is to wiggle one of the PWM motors.

Is this due to the fact that, for whatever reason, an interrupt is generated from the wiggling of the motors allowing resumption of the program?

I could not even have any interrupt code at all; as long as the encoder yellow and whites are connected the problem occurs.

Furthermore, the PWM motor doesn’t even work at all without the encoder pins connected and the encoders powered? Is it not possible to use the PWM motors WITHOUT the encoders?

Do you have an oscilloscope you can use to see if the correct encoder signals are appearing on the wires from the motor? Could you also check the motor wires’ connections to the encoder board for continuity? Since those solder connections have no strain relief, it is often easy for the wires to work loose and break off.

I am not completely sure I understand what you are saying/asking here, but if the motor by itself does not run if its encoder wires are not connected, that definitely sounds suspicious. Do both of your motors run if you connect their red and black wires directly to your power supply, leaving all of the encoder pins disconnected?

- Kevin

In case it helps anyone else with wheel encoder problems. Make sure the wheels are fully pushed onto the motors. In my case the rod was about 1mm short of flush in the wheel. This lead to bad readings no matter what encoder library I used. The answer, push the wheels fully onto the motor spindles so the spindle ends up flush with the wheel. After I’d done this the encoders gave good readings every time.

Sorry if I’ve posted this in the wrong place. My first post in this forum

Derek

ssmc, I too have had the exact same problem with the uno v3.

I was able to resolve by switching the pins around. After spending hours looking at 1s and -1s, I switched from pins 1 and 2 to pins 3 and 4 and everything worked. Also, 0 and 1 work. Hope this works for anyone else experiencing problems with the Arduino