Reading an encoder with a third signal for 'homing'

I am using a stepper motor / encoder combo which has an added ‘Z’ signal for finding a home position.

link: https://www.digikey.com/product-detail/en/cui-inc/NEMA14-10-04D-AMT112S/102-4710-ND/9477637
datasheet: https://www.cui.com/product/resource/digikeypdf/nema14-amt112s.pdf

Using the encoder library works very well to read the A and B quadrature pulses, yet I haven’t found any examples of people using an arduino to read A B and Z to get pseudo-absolute position.

Ultimately, I would like to send the encoder data over i2c to a master board so as not to bog down the greater project with the interrupts from reading the encoder.
Do you know how I would go about reading the encoder normally, but if the Z pulse is detected, save that point reliably?

here’s an example of my code that reads A & B but doesn’t do anything with Z:

/* Encoder Library - Basic Example (modified by tman1733)
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 */

#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(2, 3);
//   avoid using pins with LEDs attached

long ePos = -999; // encoder position

union u_tag { // allow long to be read as 4 seperate bytes
   byte b[4]; // 4 bytes to be sent over I2C
   long LePos; // encoder position as 4 byte long
} u;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}


void loop() {
  long newPosition = myEnc.read();
  if (newPosition != ePos) {
	  ePos = newPosition;
	  u.LePos = ePos;
	  Serial.print(u.LePos, DEC );
	  Serial.print(" = ");

	  Serial.print(u.b[0], HEX );

	  Serial.print(" : ");
	  Serial.print(u.b[1], HEX );
	  
	  Serial.print(" : ");
	  Serial.print(u.b[2], HEX );
	  
	  Serial.print(" : ");
	  Serial.print(u.b[3], HEX );

	  Serial.println();
  }
}

The union is there preemptively because it is how I will be sending the encoder value over i2c.

tman1733:
I am using a stepper motor / encoder combo which has an added ‘Z’ signal for finding a home position.

link: https://www.digikey.com/product-detail/en/cui-inc/NEMA14-10-04D-AMT112S/102-4710-ND/9477637
datasheet: https://www.cui.com/product/resource/digikeypdf/nema14-amt112s.pdf

Using the encoder library works very well to read the A and B quadrature pulses, yet I haven’t found any examples of people using an arduino to read A B and Z to get pseudo-absolute position.

Ultimately, I would like to send the encoder data over i2c to a master board so as not to bog down the greater project with the interrupts from reading the encoder.
Do you know how I would go about reading the encoder normally, but if the Z pulse is detected, save that point reliably?

here’s an example of my code that reads A & B but doesn’t do anything with Z:

/* Encoder Library - Basic Example (modified by tman1733)

*/

#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(2, 3);
//  avoid using pins with LEDs attached

long ePos = -999; // encoder position

union u_tag { // allow long to be read as 4 seperate bytes
  byte b[4]; // 4 bytes to be sent over I2C
  long LePos; // encoder position as 4 byte long
} u;

void setup() {
  Serial.begin(9600);
  Serial.println(“Basic Encoder Test:”);
}

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != ePos) {
  ePos = newPosition;
  u.LePos = ePos;
  Serial.print(u.LePos, DEC );
  Serial.print(" = ");

  Serial.print(u.b[0], HEX );

  Serial.print(" : ");
  Serial.print(u.b[1], HEX );
  
  Serial.print(" : ");
  Serial.print(u.b[2], HEX );
  
  Serial.print(" : ");
  Serial.print(u.b[3], HEX );

  Serial.println();

}
}



The union is there preemptively because it is how I will be sending the encoder value over i2c.

Assign one of your digital pins to the home signal and connect that wire to that pin.

Then in your “loop”, after this line," long newPosition = myEnc.read();",
read the home signal pin and if HIGH, save the newPosition as the home position.

Paul