Getting Wheel Encoders to work with Mega 2560

I’ve been using an Arduino Uno board, and I just switched to an Arduino Mega 2560 for the additional ports. I have code that works with the Arduino Uno to read the encoders, but now that same code will not work with the Mega 2560, I just get all zeroes. All the wires are plugged into the same ports, the encoders turn on and run, they just aren’t feeding any data back. Any thoughts? Thank you!

quick guess:

Code for uno uses “interrupts.” The mega interrupts are probably not in the same location as the uno interrupts.

Google “uno interrupt pin to mega mapping” or something like that. You have two possible problems
(1) Hardware- the mega and uno have different interrupt pin locations. Kind of like how not all pins can be used for PWM, the same way, not all pins can be used for interrupts.
(2) hard-coded interrupt stuff. So it might be that the code just says something like “use interrupt 1” just like the codes can say things like “user timer1”. If it only does that, then switching the physical pins to the corresponding “interrupt 1” would be enough to make your code work. But maybe the code says something like “use pin X” which, for the uno, is the interrupt pin. But for the mega, you’d have to change that pin name to the correct pin.

Thank you for responding! I checked both points you mentioned. I found pin mapping for both boards and tried plugging the encoders into (10,11,12,13) and (50,51,52,53) which are all PCINT pins, but none of them work. The is code set up for “use pin x” and we changed the pin numbers in the code and that didn’t work. The code in the encoder library enables the interrupts in whatever pins you give it, so that should have worked, theoretically, but it didn’t.

Any other ideas? :confused:


Which encoder library are you using? Does it use the pin change interrupts to count the pulses of the encoders? If so, those pins should work; however, if the library requires using pins with external interrupts, it will most likely have problems reading the encoders. If you want to use external interrupts to count the pulses of the encoder, you might try using this encoder library.

You might also try posting about this on the Arduino forum for additional help.

- Jeremy