Arduino Encoder to Stepper Motor - Page 1
Forum section: electronics / general

author: gman4925
author_rank: Regular Contributor
timestamp: June 22, 2015, 11:50:41 am
content: 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://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 5volatile long motor_position, encoder;void setup (){//set up the various outputspinMode(motor_step, OUTPUT);pinMode(motor_direction, OUTPUT);// then the encoder inputspinMode(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/multiplyingif (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;}}

author: Kooksun
author_rank: Newbie
timestamp: June 09, 2018, 07:01:22 am
content: I am asking how to connect the power cord.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: June 22, 2018, 02:09:58 pm
content: 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.

author: bkukuk62
author_rank: Newbie
timestamp: September 12, 2019, 05:02:01 pm
content: 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