Hi - I'm having an odd problem with a 2 channel rotary encoder attached to a motor. This unit is out of an old HP Plotter that I am trying to get working. (the encoder is a HEDS-6300). The motor is just a DC motor - not a stepper.
I am using the Boarduino for this though can't see why that would make a difference.
The problem is: if I run the sketch below and don't turn on the power to the H-Bridge unit I am using to control the motor, turning the shaft by hand I can see encoder values output in the serial window and the logic executes as expected. If I power up the motor - then I see no output at all in the serial window and none of the logic is getting executed. It's as though powering up the motor disables the encoder. (fwiw - I can ramp the speed of the motor, change direction etc. so that is all fine)
my pinout runs like this:
--enable pin for PWM is on pin 5
--Channel A of the encoder is attached to pin 2
--Channel B of the encoder is attached to pin 3
--H-Bridge direction pins are 11 & 12
I am powering the encoder off the boarduino 5v pin and have a common ground to my separate motor power supply. The motor control unit is one I got from FutureLec and seems pretty isolated in terms of back voltage, etc. I've tried different serial speeds (9600 and up).
The wiring for the unit is pretty simple - 4 leads for the encoder, 2 leads for the motor, and as far as I have been able to check, they are completely isolated.
Does this problem make sense to any one? The encoder works and the motor works - just not together.
One thing I thought odd about the unit: even though it is just a DC motor, there are "detents" when the shaft is turned - as with a stepper motor.
That's about all the info I've got - with code below. If I am missing something I'd really welcome any tips or things to try.
--Roy
int enablePin = 5;
int dir1Pin = 11;
int dir2Pin = 12;
int encoder0PinA = 2;
int encoder0PinB = 3;
int theDirection = 1;
volatile long encoder0Pos = 0;
long lastEncoderPos = 0;
void setup(){
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
pinMode(enablePin, OUTPUT);
pinMode(dir1Pin, OUTPUT);
pinMode(dir2Pin, OUTPUT);
attachInterrupt(0, doEncoderA, CHANGE);
attachInterrupt(1, doEncoderB, CHANGE);
Serial.begin(115200);
digitalWrite(dir1Pin, HIGH);
digitalWrite(dir2Pin, LOW);
analogWrite(enablePin, 100);
}
void loop(){
if (encoder0Pos != lastEncoderPos){
Serial.print(theDirection);
Serial.print(" ");
Serial.println(encoder0Pos);
lastEncoderPos = encoder0Pos;
delay(100);
}
if (encoder0Pos > 1000 && theDirection == 1){
digitalWrite(dir1Pin, LOW);
digitalWrite(dir2Pin, HIGH);
analogWrite(enablePin, 100);
theDirection = 0;
Serial.println("A");
}
else if (encoder0Pos < -1000 && theDirection == 0){
digitalWrite(dir1Pin, HIGH);
digitalWrite(dir2Pin, LOW);
analogWrite(enablePin, 100 );
theDirection = 1;
Serial.println("B");
}
delay(5);
}
void doEncoderA(){
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
void doEncoderB(){
// look for a low-to-high on channel B
if (digitalRead(encoder0PinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}