Hello All,
I have a stepper motor which has an integrated optical incremental encoder with channels A, B and Z.
I want to use the Z signal for homing. I'm new to all this but as far as I understood, Z signal is sent once per rotation at the same position. So I want to rotate the motor until a signal is detected on the Z channel and that would be my home position (or did I misunderstood something?).
Here's is the data sheet: https://www.trinamic.com/fileadmin/assets/Products/Motors_Documents/QSH4218-x-10k_datasheet_Rev1.40.pdf
I wrote this simple code but it didn't work. It is supposed to stop the motor if a Z signal is detected. Otherwise the motor will keep moving. Right now, it keeps moving indefinetely:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
int encoder0PinZ = 5; // Zero (index) signal from the encoder is connected to digital I/O pin #5
int Z = LOW; // Initialize the zero signal
bool loOp = true; // This will be used to break out of the loop if Z == HIGH
// Create the motor shield object
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect the stepper motor with (200 steps per revolution, motor port #1)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 1);
void setup() {
pinMode (encoder0PinZ, INPUT);
Serial.begin (115200);
Serial.println("Encoder test begins!");
AFMS.begin(); // stepper default frequency 1.6KHz
}
void loop() {
while (loOp = true)
{
myMotor->step(1, FORWARD, MICROSTEP); // Move the stepper motor 1 step at a time
Z = digitalRead(encoder0PinZ); // Read the Z signal
if (Z == LOW) { // If Z is not detected, print 0 and keep moving
Serial.println (Z);
} else { // If Z is detected, print 1 and break out of the loop
Serial.println (Z);
Serial.println ("Index found!");
loOp = false;
}
delay(1000);
}
}
I checked this thread but it didn't help: Incremental encoder - help with reading signals
I know there is a point where Z is high because I checked it with an oscilloscope. But the signal width was much smaller compared to A and B. Is that why my code cannot detect it? Because it happens too fast? I also recently learned about interrupts. Would it make a difference if I use interrupt?
By the way, I am using Arduino Uno and motor shield with external power supply. I connected the GND and "shield" wire of the encoder to the ground, and VCC to the 5V. A and B are also connected. A-, B-, Z- are not connected. I tried some example codes that does not use Z channel and they seem to work fine.
Also this might be relevant: the project is kind of a robotics project. The motor will turn a small stage. It is supposed to start at the same position every time and stop after certain number of steps. I wanted to use the encoder not just for homing but also for precision (not losing steps even after multiple turns). Now I realise an absolute encoder would have been easier but this encoder-integrated stepper motor is all I have and I would prefer putting it to use.
Any feedback is appreciated.