The short version:
Does anyone know what the threshold is for low to high and high to low transition when an interrupt pin is set up in CHANGE mode?
Long version:
I'm currently trying to run two stepper motors with encoder feedback on them, but I'm getting faulty interrupts whenever the motors are running. I'm using the interrupts to keep track of the encoders.
At the moment I've concentrated my efforts on getting only one motor with feedback working.
I've read other topics on this issues and have tried both external and internal pull up resistors on my encoder pins. I've grounded the chassis of both the motor and power supply with Earth ground.
I'm using Gecko Stepper drivers to drive my motors with 3.5Amps in Series configuration.
There are caps placed on my breadboard across 3.3V and Gnd.
I am able to successfully run the encoders with the motors being off with the code below. Only when the motors are on do I get issues with the interrupts!
I'm running the encoders in quadrature, only using channels A and B.
I measured the signals that are occurring on the interrupt pins and they're below 100mV. I know that a high to low transition signal needs to go from gnd to approximately 3.3V. I've attached images of this from my scope.
My code:
// pins to which Encoder is hooked up to
int encoderPinA = 5;
int encoderPinB = 4;
// variables used to keep track of the encoder position
long encoderCount = 0; // keeps track of the number of times the encoder turns
int encoderCountPrev = 1; // saves old value of encoder
boolean dirFlag = HIGH; // boolena value used to change direction of setpper
int stepPin = 52; // pin used to initiate a step
int directionPin = 50; // pin used to change direction
int enablePin = 48; // pin used to enable mtr controller
void setup()
{
// setting up interrupt
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
digitalWrite(encoderPinA, HIGH); // pull up resistors enabled
digitalWrite(encoderPinB, HIGH);
attachInterrupt(5, doOnA, CHANGE); // calling doOnX isr
attachInterrupt(4, doOnB, CHANGE);
pinMode(enablePin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
digitalWrite(enablePin, LOW);
digitalWrite(directionPin, dirFlag);
Serial.begin(115200);
}
void loop()
{
for(int i =0; i < 200; i++)
{
takeAStep();
delay(10);
}
dirFlag = !dirFlag;
digitalWrite(directionPin, dirFlag);
if(encoderCountPrev != encoderCount)
{
Serial.print("Encoder value = "); // cake topping
Serial.println(encoderCount, DEC); // print out the new value of the encoder count
encoderCountPrev = encoderCount; // save the current value into the old M
}
}
// performs this operation when A changes state on the encoder
void doOnA()
{
if (REG_PIOC_PDSR & (1 << 25)) { // look for a low-to-high on channel A
if (!(REG_PIOC_PDSR & (1 << 26))) { // check channel B to see which way encoder is turning
encoderCount++;
}
else {
encoderCount--;
}
}
else { // must be a high-to-low edge on channel A
if (REG_PIOC_PDSR & (1 << 26)) { // check channel B to see which way encoder is turning
encoderCount++;
}
else {
encoderCount--;
}
}
}
//perform this operation when B changes state on the encoder
void doOnB()
{
if (REG_PIOC_PDSR & (1 << 26)) { // look for a low-to-high on channel B
if (REG_PIOC_PDSR & (1 << 25)) { // check channel A to see which way encoder is turning
encoderCount++;
}
else {
encoderCount--;
}
}
else { // must be a high-to-low edge on on channel B
if (!(REG_PIOC_PDSR & (1 << 25))) { // check channel B to see which way encoder is turning
encoderCount++;
}
else {
encoderCount--;
}
}
}
void takeAStep()
{
REG_PIOB_SODR = 0x1 << 21; //set the pin high
delay(1);
// delayMicroseconds(5);
REG_PIOB_CODR = 0x1 << 21; //set the pin low
}
Some of the things that are running through my mind with this issue:
- EMF from motors is interacting with the digital pins, but would a 100mV signal trigger interrupts, when set up in CHANGE mode?
- It can't be the noise from the connection between the Arduino Due and the Gecko stepper drivers as the drivers are opto-isolated and the encoders work fine with motors powered down, but stepper driver being on.
- The breadboard is picking up the electromagnetic noise, since it is so close to the motors, but a 100mV signal causing faulty interrupt trigger?!
Anyways, any ideas on what could be going on?