# Wiring KY-040 rotary encoder button function into matrix

**URL:** <https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768>\
**Category:** General Electronics\
**Created:** [July 30, 2021, 7:18pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768 "2021-07-30T19:18:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugasjunior](https://avatars.discourse-cdn.com/v4/letter/f/dc4da7/32.png) [@fugasjunior](https://forum.arduino.cc/u/fugasjunior)\
**Post date:** [July 30, 2021, 7:18pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/1 "2021-07-30T19:18:25Z")

</div>

Hello,

I'm building a button box for flight sim and I'm trying to wire up several buttons into a 5x4 button matrix, alongside three KY-040 rotary encoders.

Originally I was planning to wire the encoder button function into the matrix, but then I found the encoder needs a 5V wire to function properly, so I can't connect the button to a row and a column. I am now helpless on how to wire it properly into the matrix, and I'm not sure if it's even possible.

I don't have enough free I/O to wire these buttons directly into the Arduino Leonardo.

Do you have any ideas on how to fix the problem, besides expanding the I/O?

Thank you for any tips!

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 30, 2021, 8:08pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/2 "2021-07-30T20:08:30Z")

</div>

Welcome to the forum.

A rotary encoder doesn't go well with a keyboard matrix.  
What else is connected to your Leonardo board ?

---

<div class="post-metadata">

**Author:** ![fugasjunior](https://avatars.discourse-cdn.com/v4/letter/f/dc4da7/32.png) [@fugasjunior](https://forum.arduino.cc/u/fugasjunior)\
**Post date:** [July 30, 2021, 8:19pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/3 "2021-07-30T20:19:00Z")

</div>

Thank you for the reply. I'm not trying to connect the rotary encoder itself into the keyboard matrix. Rather, I just want to connect its push function into the matrix, which I think normally should be possible. I was following this tutorial: [How to Build a Button Box - YouTube](https://www.youtube.com/watch?v=wkY1NsbWj5I).

Unfortunately, I have a different kind of encoder that comes with a breakout board, and needs 5V input to function properly. This leaves me with just a single pin for the push function instead of two, which I would normally connect into the matrix.

As for what's connected to the Leonardo, I have 19 buttons in total (3 2-way switches, 4 regular switches and 6 push buttons, all momentary, plus the ones on the rotary encoders), 3 rotary encoders and 4 potentiometers.

Edit: the rotary encoder I have is this one:

 ![141496045_max](https://europe1.discourse-cdn.com/arduino/original/4X/d/f/6/df635386f2da6a3dc1156d84d8d740617317e553.jpeg)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 30, 2021, 8:28pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/4 "2021-07-30T20:28:31Z")

</div>

The KY-040 rotary encoder only needs Vcc for the external pullups on the KY-040 board. And the pullups are only for the encoder side, the switch is not pulled up. The push switch can still be wired as any NO push button switch so should go into a matrix like any other. You will have to separate the push switch ground from the encoder ground, though.

Here is the schematic for the KY040.

 ![KY-040](https://europe1.discourse-cdn.com/arduino/original/4X/3/6/9/369d7ddf1279c8c539a86e3e36895b5ed9ae516c.jpeg)

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 30, 2021, 8:38pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/5 "2021-07-30T20:38:25Z")

</div>

That is indeed a KY-040 rotary encoder. The 'CLK' is often called 'A' and the 'DT' is 'B'.  
The 'SW' is connected to GND when pressed.  
You have already answered your own question, it can not be combined with a keyboard matrix. [ADDED] Maybe you can, as groundFungus wrote

Have you read [this comment](https://github.com/Chris--A/Keypad/blob/master/src/Keypad.cpp#L84) in the Keypad library: "_Re-intialize the row pins. Allows sharing these pins with other hardware_".  
But I don't know how and what that sharing can be.

The [Leonardo](https://store.arduino.cc/arduino-leonardo-with-headers) has exactly 19 pins. There are 3 more digital I/O pins on the ICSP header.  
The switches can be placed behind extra hardware.  
There are also Teensy boards with more pins, such as the [Teensy 4.1](https://www.pjrc.com/store/teensy41.html).

---

<div class="post-metadata">

**Author:** ![fugasjunior](https://avatars.discourse-cdn.com/v4/letter/f/dc4da7/32.png) [@fugasjunior](https://forum.arduino.cc/u/fugasjunior)\
**Post date:** [July 30, 2021, 8:57pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/6 "2021-07-30T20:57:44Z")

</div>

I have just tried desoldering the encoder from the board. Unfortunately, when I plug it into the Leonardo and try rotating it, it behaves weirdly:  
Every other rotation by one detent seems to be registered just fine. However, the next rotation causes the encoder to keep switching A and B states indefinitely. The next rotation stops this behavior, another one makes it keep going, and so on...  
Maybe I don't understand encoders and how to read them enough. Is this an expected behavior? Would you be so kind to provide a code snippet on how to handle this behavior? (I have tried several code snippets found on the internet, none of which worked for me).

To provide an example, I have written this code:

```cpp
int encoderPinA = 0;
int encoderPinB = 1;
int prevA = -1;
int prevB = -1;

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);
}

void loop() {
  int a = digitalRead(encoderPinA);
  int b = digitalRead(encoderPinB);

  if (a != prevA || b != prevB) {
    Serial.print(a);
    Serial.print(" ");
    Serial.println(b);
  }

  prevA = a;
  prevB = b;
}

```

which creates this pattern:

```cpp
// first rotation, this pattern starts appearing
1 0
0 0
1 0
1 1
1 0
0 0
1 0
1 1
0 1
0 0
1 0
1 1
0 1
0 0
1 0
1 1
0 1
0 0
// second rotation, this pattern stops displaying

```

This is how the encoder is wired:

 ![IMG_20210730_225631](https://europe1.discourse-cdn.com/arduino/original/4X/f/3/6/f36b7ec4782ca26982105bf312c01a901a0656df.jpeg)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 30, 2021, 9:01pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/7 "2021-07-30T21:01:59Z")

</div>

When you removed the encoder from the board, you left the pullup resistors behind. Compensate by setting the pinModes for the encoder inputs to INPUT\_PULLUP.

Put a 0.1uF cap from A to ground and from B to ground to help debounce the encoder switches or include software debounce in your code.

Check out the [Encoder library.](https://www.pjrc.com/teensy/td_libs_Encoder.html)

And don't use pins 0 and 1. Those pins are for the hardware serial RX and TX. You are not using serial for this code, but you will soon find out the importance of the serial port for debugging. Also if you connect some things to those pins you will not be able to upload code via the serial port. My advice is to forget that those pins exist for normal projects. Edit: comment based on mistaken assumption that the board an Uno instead of a Leonardo.

---

<div class="post-metadata">

**Author:** ![fugasjunior](https://avatars.discourse-cdn.com/v4/letter/f/dc4da7/32.png) [@fugasjunior](https://forum.arduino.cc/u/fugasjunior)\
**Post date:** [July 30, 2021, 9:28pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/8 "2021-07-30T21:28:40Z")

</div>

Thank you! I've tried the Encoder library, added software debounce, and it works perfectly now.  
Also, thank you for the tip about RX and TX, I'm completely new to Arduino, so I'll take any advice I can get.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 30, 2021, 9:42pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/9 "2021-07-30T21:42:24Z")

</div>

> [@groundFungus](#):
>
> And don't use pins 0 and 1. Those pins are for the hardware serial RX and TX. You are not using serial for this code, but you will soon find out the importance of the serial port for debugging. Also if you connect some things to those pins you will not be able to upload code via the serial port. My advice is to forget that those pins exist for normal projects.

Pins 0 & 1 on a Leonardo are Serial1 and are separate from the USB serial, so can be used without interfering with debugging or uploading code.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 30, 2021, 9:49pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/10 "2021-07-30T21:49:01Z")

</div>

Sorry. I saw an Uno board and based my comment on that false observation.

---

<div class="post-metadata">

**Author:** ![ira11y](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@ira11y](https://forum.arduino.cc/u/ira11y)\
**Post date:** [August 20, 2021, 8:59pm UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/11 "2021-08-20T20:59:07Z")

</div>

Thanks for all the information here, I was trying to do exactly the same thing with the same encoder. The help on here is awesome.

---

<div class="post-metadata">

**Author:** ![ira11y](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@ira11y](https://forum.arduino.cc/u/ira11y)\
**Post date:** [August 21, 2021, 1:56am UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/13 "2021-08-21T01:56:39Z")

</div>

So I have had another look at trying to rectify this design whilst still using the KY-040 board without de-boarding the rotary encoder as per the OP's solution. Here's what I came up with and have proven it works in testing.

As a new user I was unable to add all the images and links so here's a link to the folder with them all.  
[KY-040 Keyboard Matrix Solution](https://ibb.co/album/TKLrPZ)

The board circuit is big enough to be able to work with, so firstly de-solder the pin as best as possible, use a solder sucker is best.  
[Solder Sucker Link](https://core-electronics.com.au/media/catalog/product/cache/1/image/650x650/fe1bcd18654db18f328c2faaaf3c690a/1/4/148-02.jpg)

see KY-040 Circuit Map in folder

In the circuit image you can isolate the earthed pin, do this by carefully using a scalpel or small sharp blade to cut an area around the pin, careful not to impact any other tracks. once you have cut lines down to the phenollic (the base on which the copper tracks lay) then carefully use the blade to push under the copper track towards the port pin, always working away from the other tracks, once all the copper is removed you need to solder in a new wire on the pin.

see KY-040 KY-040-Solution-9 in folder

I found the best way was to push the wire through the hole and to solder on to the leg on the top side of the board.

See KY-040-Solution-4 in folder

Using the Keyboard Matrix sketch you wont need to use the +5V, you will only need the pins in the image below. Hope this helps anyone in the same situation.

See KY-040-Solution-7 in folder

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 19, 2021, 1:57am UTC](https://forum.arduino.cc/t/wiring-ky-040-rotary-encoder-button-function-into-matrix/890768/14 "2021-12-19T01:57:06Z")

</div>

Closed on December 19, 2021, 1:57am UTC

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