Author Topic: Arduino Encoder to Stepper Motor  (Read 9330 times)

0 Members and 3 Guests are viewing this topic.

Offline gman4925

  • Regular Contributor
  • *
  • Posts: 51
  • Country: us
Arduino Encoder to Stepper Motor
« on: June 22, 2015, 11:50:41 am »
A while ago I put a video up on YouTube of using an encoder to control a stepper motor.
After numerous requests for code and modifications I'm posting it here as it is no longer hosted on my old works website and this is a good forum to discuss etc.

Video:


Code:
Code: [Select]
//Arduino code for controlling stepper motor from encoder
//video at https://www.youtube.com/watch?v=2dh6TIkN6jE
//reconstructed code from 5+ years ago
//untested in current form

 #define encoder_a 2    //encoder a = arduino pin 2
 #define encoder_b 3    //encoder b = arduino pin 3
 #define motor_step 4    //motor step = arduino pin 4
 #define motor_direction 5    //motor direction = arduino pin 5

volatile long motor_position, encoder;

void setup ()
{
    //set up the various outputs
    pinMode(motor_step, OUTPUT);
    pinMode(motor_direction, OUTPUT);
    // then the encoder inputs
    pinMode(encoder_a, INPUT);
    pinMode(encoder_b, INPUT);
    digitalWrite(encoder_a, HIGH);
    digitalWrite(encoder_b, HIGH);
    // encoder pin on interrupt 0 (pin 2)
    attachInterrupt(0, encoderPinChangeA, CHANGE);
    // encoder pin on interrupt 1 (pin 3)
    attachInterrupt(1, encoderPinChangeB, CHANGE);
    encoder = 0;
}

void encoderPinChangeA()
{
    if (digitalRead(encoder_a) == digitalRead(encoder_b))
    {
        encoder--;
    }
    else
    {
        encoder++;
    }
}

void encoderPinChangeB()
{
    if (digitalRead(encoder_a) != digitalRead(encoder_b))
    {
        encoder--;
    }
    else
    {
        encoder++;
    }
}


void loop()
{
//do stuff dependent on encoder position here
//such as move a stepper motor to match encoder position
//if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
    if (encoder > 0)
    {
        digitalWrite(motor_direction, HIGH);
        digitalWrite(motor_step, HIGH);
        digitalWrite(motor_step, LOW);
        _delay_us(200);
        motor_position++;
        encoder = 0;
    }
    else if (encoder < 0)
    {
        digitalWrite(motor_direction, LOW);
        digitalWrite(motor_step, HIGH);
        digitalWrite(motor_step, LOW);
        _delay_us(200);
        motor_position--;
        encoder = 0;
    }
}
 
The following users thanked this post: bkukuk62

Offline Kooksun

  • Newbie
  • Posts: 1
Re: Arduino Encoder to Stepper Motor
« Reply #1 on: June 09, 2018, 07:01:22 am »
I am asking how to connect the power cord.
 

Offline Doctorandus_P

  • Frequent Contributor
  • **
  • Posts: 579
  • Country: nl
Re: Arduino Encoder to Stepper Motor
« Reply #2 on: June 22, 2018, 02:09:58 pm »
Have you ever tried connecting the  quadrature encoder directly to the stepper motor driver?
You do not even need a uC in between.

Use one of the channels for "step" and another for "direction".

The biggest limitation of this is that if there is jitter on the "step" pin the stepper motor will keep making steps in one direction while the encoder is actually standing "stil".
This can be remedied by a bit of simple logic with for example a few 74xx74 flipflops.
 

Offline bkukuk62

  • Contributor
  • Posts: 7
  • Country: us
Re: Arduino Encoder to Stepper Motor
« Reply #3 on: September 12, 2019, 05:02:01 pm »
The above code is perfect for the project I am currently working on.

I am rotating a mast with a stepper motor and a quadrature optical encoder, this works great for just doing that!

Is it possible to modify the same code and check for a button push where the stepper motor would rotate 360 degrees 10 times or until another button is pushed to stop rotating and go back to the original rotation control from the encoder?

To add a little more to this, maybe have a multi position switch to have various speeds of the stepper rotation but only when it is in the loop where it rotates 10 times?

Thanks for any help I can get on this.
bkukuk62 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf