Rotary Encoder schematic and example code

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

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:

/* 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;
}

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.

and you will need resistors and capacitors to make it work decent, check out this link Rotary Encoder Tutorial with Arduino Code - YouTube
sample code you can find here also Learn on the fly : Adding Rotary encoder to arduino projects- quick start

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.

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.

How to connect encoder to make it work with my library - Lightweight Arduino Library for Rotary Encoder - Sensors - Arduino Forum

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

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?