Max Encoder PPR with Nano?

Was trying to follow an encoder tutorial with an incremental quadrature encoder I had on hand (46080 PPR @ 480kHz). Originally thought it was bouncing and added a delay but it's still not correctly reading direction at very slow RPM (turning by hand with additional 125 reduction). Changing the baud to 115200 didn't improve anything. At this point just figure I'm overwhelming this Nano. Is there some kind of intermediate chip I can use to decode the quadrature counts or do I just outright need a faster processor?

This is the tutorial I followed, code is essentially the same, minus the LEDs:

Post a data sheet for the encoder.

Post the actual code that you uploaded to the Nano.

Post a schematic of the project. Include all components, their values/part numbers and power supplies.

It is a PMIR4-20-2048-480KHZ-TTL-Z0-1M-S with a PMIR4-20-90 ring.

Not sure how to make a schematic. Only components are the encoder readhead, ring and ELEGOO Nano. Encoder to +5V and GND, A & B to digital pins 2 & 3. Switching power supply, 5V 1A (PSAC05R-050).

Code:

// Rotary Encoder Inputs
#define CLK 2
#define DT 3

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";

void setup() {
	
	// Set encoder pins as inputs
	pinMode(CLK,INPUT);
	pinMode(DT,INPUT);

	// Setup Serial Monitor
	Serial.begin(9600);

	// Read the initial state of CLK
	lastStateCLK = digitalRead(CLK);
}

void loop() {
	// Read the current state of CLK
	currentStateCLK = digitalRead(CLK);

	// If last and current state of CLK are different, then pulse occurred
	// React to only 1 state change to avoid double count
	if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

		// If the DT state is different than the CLK state then
		// the encoder is rotating CCW so decrement
		if (digitalRead(DT) != currentStateCLK) {
			counter --;
			currentDir ="CCW";
		} else {
			// Encoder is rotating CW so increment
			counter ++;
			currentDir ="CW";
		}

		Serial.print("Direction: ");
		Serial.print(currentDir);
		Serial.print(" | Counter: ");
		Serial.println(counter);
	}

	// Remember last CLK state
	lastStateCLK = currentStateCLK;

	// Put in a slight delay to help debounce the reading
	delay(1);
}

You will never get it to read fast when printing to serial every time the state changes. Why do you need to do that? Does the end use of the encoder data require printing the encoder data.

For high speed, usually interrupts are used. See the Encoder library.

Delay doesn't help either. That type of encoder should not bounce (like a mechanical switch).

I am trying to read angle output of a shaft and stream the current position to a console. The shaft spins very slow so I figured the large number of counts would allow me to detect position accurately, but I haven't be able to get it to read consistently.

I will take a look at the encoder library you provided.

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