Simple rotary encoder disaster

Hours I've spent on what seems to be a very basic project and it refuses to work.

Given:
1 Arduino NANO
1 simple mechanical rotary encoder (no switch function) with dents.

Goal:
Turn CW = count up
Turn CCW = count down

I've tried countless examples of code, libraries (Encoder, NewEncoder, RotEnc, ezButton,...), with or without (poll) interrupts, changing data rates, RC filtering, internal and external VDC board powering and swapping encoder with spare ones.

The result is always one of the following:

-It does not register movement.
-Encoder only counts up or only counts down no matter the direction of turning.
-Encoder counts +1 and -1 in one cycle so total count remains unchanged.

Any help is GREATLY appreciated.
Cheers

1 Like

have you tried Encoder Library?

try running File>Examples>Encoder>Basic

can you give a link or a photo to the encoder you are using?


It's basically an old guitar pedal pcb with 3 encoders.

I did try the Encoder library with no luck.

Seems there could be lots of things connected to those encoders… have you tried with a plain encoder not from that PCB (or remove it from the PCB) ??

1 Like

Please show just one failing example and its full circuit schematic. Otherwise we’ve nothing solid to discuss.

Is it really an encoder or a switch?

An encoder has 2 outputs for the A and B channel, while a switch has a common and more outputs for each detent.

A mechanical encoder needs software debouncing. Not easy to handle :frowning:

I was thinking the same thing, but that would be a last resort, as I would like to use the leds and buttons etc on it and the current form factor of the pcb.. I did however for now connect the leads directly to the pins of the encoder to run all tests. All leads on the pcb go to a ribon cable connector which is not attached so no connections are made internally.

how did you connect the encoder to the nano?

if I run the following code (variation of example from encoder library referenced in post 2) on a Nano (pins 2 and 3 used) it works OK

// Leonardo - File>Examples>Encoder>Basic
// also tested on a Nano

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
//Encoder myEnc(4, 5);   // arduino uno pins 2 and 2
Encoder myEnc(2, 3);   // Leonardo pins 2 and 3
//   avoid using pins with LEDs attached
int switchPin=5;       // switch is pin 5

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
    pinMode(switchPin, INPUT_PULLUP);
}

long oldPosition  = -999;

void loop() {
  static int sw=0, offset=0;
  // display switch on press and reset position to 0
  if(!digitalRead(switchPin) != sw) 
  {
    sw=!digitalRead(switchPin);
    if(!digitalRead(switchPin))Serial.println("switch");
    offset=oldPosition;
  }
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition-offset);
  }
}

serial monitor output as I rotate the encoded

Basic Encoder Test:
0
1
2
3
4
5
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
switch
1
2
1
2
3
2
3
4
5
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
switch
1
2
3
4
5
6
7
8
7

photo

1 Like

Not sure. On the guitar pedal (zoom g2) it was used to scroll through settings or change tone values. Output of the scrolling was reflected on the 2x 7 digit segment display.

An encoder should not have more than 3 pins:
One common (GND)
and 2 signals

1 Like

Hi, @indecember
Welcome to the forum.

Have you removed the encoder from the PCB?
Can you please show an image of the other side of the PCB?
Do you have a DMM? Digital MultiMeter?

Tom.... :smiley: :+1: :coffee: :australia:

1 Like

Common ground of the encoder to Nano ground.
Lead 1 to pin 2
Lead 2 to pin 3

Result:

Basic Encoder Test:
0
1
2
4
5
6
8
9
10
12
13
14
16
17
16
18
17
16
17
16
18
20
22
24
25
24
25
26
28
29
30
32
33
34
36
37

So it only counts up in this case.

do you have an oscilloscope to look at the signals?

What's that mystery pin?
Where does it connect to?

in addition to rotating the encoder has a press switch which is used to reset the count (connected in the code of post 8 to pin D5)
when using such encoders with menus I use the rotation to scroll up/down the menu and the press to select the menu item

I do have this ancient analog scope which I use to trace AC signals in guitar amps, no clue how I would need to set it up though to measure an encoder :sweat_smile:

You see that it doesn't count properly. Debouncing does not work well.

I've come across your name loads of times on here mate, I've noticed you have great knowledge! Thanks for the warm welcome!

It's almost useless for catching switch bounces.

A cheap logic analyzer is much more useful.

1 Like

connect to the encoder signal pins - I used DC coupling, 10mSec/division and trigger on falling edge
e.g. rotation clockwise

rotation anti-clockwise

the pulse widths etc depend on how fast the encoder is rotated