0
\$\begingroup\$

I have the following problem: I must use two encoders to measure the speed of two DC motors at the same time, using just one Arduino Uno. But the Arduino only possess two interrupt pins, and I need both of them for just one encoder. There's any workaround that I can use to get my two encoders running at the same time?

Any hints will be helpful!

\$\endgroup\$
5
  • \$\begingroup\$ how do your encoders communicate with the microcontroller? \$\endgroup\$
    – Nazar
    Dec 2, 2016 at 13:59
  • \$\begingroup\$ my encoder has 3 pins: two signal pins and one ground pin. Both signal pins are connected to interrupt 0 and 1 on the arduino. \$\endgroup\$
    – Vinicius
    Dec 2, 2016 at 14:03
  • \$\begingroup\$ post the documentation for your components, connection schematic? \$\endgroup\$
    – Nazar
    Dec 2, 2016 at 14:04
  • 1
    \$\begingroup\$ Maybe use pin change interrupts rather than external interrupts and check which edge you got in the code. \$\endgroup\$ Dec 2, 2016 at 14:09
  • \$\begingroup\$ @Roger Rowland If you would elaborate your idea in an answer, I would up vote it. \$\endgroup\$ Dec 2, 2016 at 15:09

4 Answers 4

1
\$\begingroup\$

There is a really nice library for working with encoders on the Arduino platform as well as on Teensys. It's developed by PJRC, the developer of the teensy, and has a very nice explanation on its page which can be found here: https://www.pjrc.com/teensy/td_libs_Encoder.html

To answer your question a bit more comprehensively, encoders work, by encoding the rotation into so-called grey-code.

Grey-code is a form of electrical signaling where you have two pulse trains, that look like this: Illustration of grey-code, from PJRCs encoder library explanation

This method of signaling allows a microcontroller like an Arduino to sense both the direction of rotation, as well as amount of rotation.

One way to read gray code is to trigger when one of the pins, say pin1, goes high, and then read the state of the second pin. Due to the nature of grey-code, if you are rotating one direction, pin 2 will always be low when pin 1 goes high one direction of rotation, while it will always be high for the opposite direction of rotation.

A good way to implement this is to use interrupts to detect when a pin switches state because encoders will generate signals very fast with moderate rotation speeds because a typical encoder used with an Arduino has 4 electrical steps per physical indent. However, you will still have several problems to overcome. First of all, being a mechanical switch, you will have to deal with switch bounce, but amongst other things to deal with is code execution speed, to ensure that you don't miss any impulses on faster moves.

All in all, my recommendation would be to use the library provided by PJRC, as it is a very well performing library, and supports multiple modes of operation, with varying levels of performance.

General tips for the highest performance of reading encoders is to use interrupts as much as possible, to ensure impulses are caught.

\$\endgroup\$
0
\$\begingroup\$

I'm not familiar enough with the Arduino environment to offer a real solution, but I'd be looking into whether the limitation on interrupts is an Arduino thing, or whether it's a AVR thing. If the former, you might consider using the Arduino as an AVR development system, and just program the microcontroller directly without the Wiring system sitting of top of everything.

If the AVR doesn't have enough oomph, use your Uno to prototype different aspects of the system, and then step up to a bigger platform, Arduino or otherwise, to complete the job you need to do. Right tool for the right job.

\$\endgroup\$
0
\$\begingroup\$

two encoders to measure the speed of two DC motors at the same time

Since all you care about is the speed, then the 2 interrupt pins are sufficient. Wire up only 1 phase of each sensor to each Arduino interrupt pin. The rate of interrupt of each pin will tell you the speed of that motor and rotary encoder.

You only need both phases of the rotary encoder if you care about the direction of rotation.

If you had 2 interrupt inputs and also had 2 available non-interrupt digital inputs, you can still measure both rotation direction and rotation speed. Just interrupt on, for example, phase 1 rising edge. When the interrupt occurs, immediately read phase 2 state. At that point Phase 2 will be 1 when rotating in one direction and 0 if rotating in the other direction.

\$\endgroup\$
0
\$\begingroup\$

There's any workaround that I can use to get my two encoders running at the same time?

you can use PCINTs if available on your chip.

Timers with external clock inputs can be configured as external interrupts.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.