Two rotary encoders work fine, three don't

Hi everyone,

I’m aiming to work with eventually nine rotary encoders (KY-040). I started with one. The code worked well, for a second encoder I copied the relevant snippet and adjusted the variables. Works perfectly fine. (With this little code I can still go without interrupts.)

So I was thinking that I can just do the same for the third encoder (like: defining variables for the third encoder and copying the part needed). What I have now is: I am still able to read encoder 1 and 2. For encoder 3 nothing happens. What exactly am I missing? Thanks for sparing your time!

three-encoders.ino (2.42 KB)

Hello there!

Can you post a schematic or picture of your setup, showing where are the connections are?

Your pinB inputs are floating - all the pins need to be INPUT_PULLUP.

Write one function to handle any of the encoders and pass the pin numbers to it - otherwise
you have 3 nearly identical copies of the code for 3 times as many bugs to lurk in!

Or even use arrays for the pin numbers and counts.

bos1714:
Hello there!

Can you post a schematic or picture of your setup, showing where are the connections are?

A schematic is attached. (The picture doesn’t show the KY-040 encoders, but please imagine them as such.)

MarkT:
Your pinB inputs are floating - all the pins need to be INPUT_PULLUP.

I see! I was wondering whether I needed to define the pin modes of the B pins.

MarkT:
Write one function to handle any of the encoders and pass the pin numbers to it - otherwise
you have 3 nearly identical copies of the code for 3 times as many bugs to lurk in!

Or even use arrays for the pin numbers and counts.

Ok, a function to handle any of the encoders. I will work on it!
(Yeah, three almost identical copies is not an elegant manner.)

ickeai:
A schematic is attached. (The picture doesn't show the KY-040 encoders, but please imagine them as such.)

I see! I was wondering whether I needed to define the pin modes of the B pins.

Why would they be different to the A pins?

Ok, a function to handle any of the encoders. I will work on it!
(Yeah, three almost identical copies is not an elegant manner.)

The most advanced part is using pass by reference for the count variables - might be easier to pass
an integer index and use arrays for all the pin numbers and count variables.

Ok, I managed it! (And I realised the problem wasn’t exactly my sketch but apparently current supply of the third encoder on the breadboard, so plugged all of them directly to the arduino.)

I attached my ‘beautified’ sketch for those who are interested.

read-rotary-encoders.ino (1.4 KB)