# Rotary Encoder schematic and example code

**URL:** <https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111>\
**Category:** Sensors\
**Created:** [September 13, 2018, 6:24pm UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111 "2018-09-13T18:24:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitaldetritus](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@digitaldetritus](https://forum.arduino.cc/u/digitaldetritus)\
**Post date:** [September 13, 2018, 6:24pm UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/1 "2018-09-13T18:24:01Z")

</div>

I've looked in several places before coming here but to no avail.

Does anyone have a schematic and example code for hooking this rotary encoder with push button up to the arduino leonardo: [Rotary Encoder with Pushbutton Switch](http://www.mpja.com/Rotary-Encoder-with-Pushbutton-Switch/productinfo/30403+SW)

here's the code i've been using but i don't even know if i have the circuit build correctly.

Here's the schemata i've been using:  
 ![](https://europe1.discourse-cdn.com/arduino/original/4X/5/8/1/581ec0c06b1a0e54b654ca681094ed93d62c63f2.jpeg)

```cpp
/* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

   Sketch by max wolf / www.meso.net
   v. 0.1 - very basic functions - mw 20061220

*/

int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

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

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n;
}

```

---

<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:** [September 13, 2018, 8:41pm UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/2 "2018-09-13T20:41:10Z")

</div>

The internals of the encoder are,in effect, normally open momentary switches and can re treated as such. Set the A and B input pinModes to INPUT\_PULLUP like you would a switch and give your code a try. The input will be HIGH when not being turned and pulse LOW while being turned. I usually put a 0.1uf cap from A to ground and one from B to ground to debounce the contacts.

There are encoder libraries that make using encoders easier. Check the library manager.

---

<div class="post-metadata">

**Author:** ![MichaAVR](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@MichaAVR](https://forum.arduino.cc/u/MichaAVR)\
**Post date:** [September 14, 2018, 10:19am UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/3 "2018-09-14T10:19:15Z")

</div>

and you will need resistors and capacitors to make it work decent, check out this link [Rotary Encoder Tutorial with Arduino Code - YouTube](https://www.youtube.com/watch?v=HQuLZHsGZdI)  
sample code you can find here also [Learn on the fly : Adding Rotary encoder to arduino projects- quick start](https://www.riyas.org/2015/12/adding-rotary-encoder-to-arduino.html?m=1)

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [September 14, 2018, 10:44am UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/4 "2018-09-14T10:44:58Z")

</div>

> digitaldetritus:  
> I've looked in several places before coming here but to no avail.
> 
> Does anyone have a schematic and example code for hooking this rotary encoder with push button up to the arduino leonardo:

The common pin of the encoder needs to go to +5V, not to ground.

The A and B inputs should be declared INPUT\_PULLUP, not INPUT.

Then there is actual hope of it working - where did you find that broken code and circuit? You should  
go back and see if you can provide feedback on how broken it is.

---

<div class="post-metadata">

**Author:** ![enjoyneering](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/enjoyneering/32/240058_2.png) [@enjoyneering](https://forum.arduino.cc/u/enjoyneering)\
**Post date:** [September 14, 2018, 10:59am UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/5 "2018-09-14T10:59:26Z")

</div>

The " **if else**" slow. Use “ **switch-case** ” instead. Also you are running encoder code in the main loop. This is not a good idea. When the main cycle becomes more busy, you going to have missing steps. Been there done that. If you want stable result use external interrupt or interrupt by the timer.

Check my code for details - [GitHub - enjoyneering/RotaryEncoder: This is small and fast Arduino library for Rotary Encoder with interrupts.](https://github.com/enjoyneering/RotaryEncoder)

How to connect encoder to make it work with my library - [Lightweight Arduino Library for Rotary Encoder - Sensors - Arduino Forum](https://forum.arduino.cc/index.php?topic=553990.0)

Tips & tricks to optimize your code for microcontrollers - [http://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf](http://ww1.microchip.com/downloads/en/AppNotes/doc8453.pdf)

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [September 14, 2018, 1:50pm UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/6 "2018-09-14T13:50:34Z")

</div>

> MarkT:  
> The common pin of the encoder needs to go to +5V, not to ground.

I respectfully question this. If encoder common is to 5V and inputs are pulled up to 5V how will there be a voltage change to tell when the switch closes?

---

<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:** [May 6, 2021, 10:22am UTC](https://forum.arduino.cc/t/rotary-encoder-schematic-and-example-code/546111/7 "2021-05-06T10:22:53Z")

</div>

Closed on May 6, 2021, 10:22am UTC


