KY-040 rotary encoder on Micro

I have tried this sample ( https://create.arduino.cc/projecthub/vandenbrande/arduino-rotary-encoder-simple-example-ky-040-b78752 ) on an Uno and it works great. However, it does not work when moving it to a Micro using the same pin connections. It has been a long time since I moved a project from an Uno to a Micro, but I recall there are some subtle differences. Any help on hos this project on a Micro should be wired/connected would be greatly appreciated.

what happens ?

That code should work without any changes on the Micro using the same pin connections.

My error . . . I posted the wrong project link. Here is the correct one:

( Using the Rotary Encoder Module KY-040 with Arduino - Phipps Electronics ).

Works great on an Uno, but simply scrolls the "Button released" message on the monitor when run on a Micro. Pressing the button yields no different result. Neither does rotating the encoder.

that's expected since the loop() contains

// check if button is pressed (pin SW)
if (digitalRead(encoderBtn) == LOW) Serial.println("Button Pressed");
else Serial.println("Button Released");

you should see Button Pressed when pressed though (might be saturating the Serial line so printing only when that changed would make sense)

Good point. But, the sketch works like a champ on an Uno.

In that case you got a bad Micro. Return it to Arduino to get a replacement. The pins D8, D9 and D10 are standard I/O pins and are only used as such in the linked sketch.
The code you linked to is rather bad. There is the bad code formatting but that alone won't be that bad. The worse thing is that it's printing constantly to the serial interface which slows down and might let it loose some events because it's waiting for the serial buffer to get emptied.

Remove these lines and try again:


// check if button is pressed (pin SW)
if (digitalRead(encoderBtn) == LOW) Serial.println("Button Pressed");
else Serial.println("Button Released");

You won't get informed about the button press anymore but the serial problem should be less harmful.

As said, you might just be saturating the Serial line, remove the button test to see if the encoder part works

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.