Go Down

Topic: Arduino Encoder to Stepper Motor (Read 2043 times) previous topic - next topic

Hi, Everyone:

I am new to arduino, I found "Arduino Encoder to Stepper Motor " on youtube, it's very similar as what I want to do, but the code is not complete!

1) arduino duemilanove
2) Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0
3) Stepper motor (Connect to M3 & M4)
4) Encoder from old printer(connect to PIN2 & PIN3)

My questions:
how to declare "motor_step" & "motor_direction"?
   INT motor_step=????
   INT motor_direction=????
   INT encoder_a=2;
   INT encoder_b=3;
---------------------------------------------------


http://www.youtube.com/watch?v=2dh6TIkN6jE

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 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;
}}

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

kf2qd

MotorStep and MotorDirection are assuming you have another component called a stepper driver between your Arduino and your stepper motor.

A common configuration for a Stepper Driver is to have 2 inputs - Step and Direction. Every time the direction input is toggled the motor moves 1 step in the direction controlled by the Direction pin. Typically the direction input to the driver stays in one mode or the other and only changes when the direction changes.

You can buy or build them using a variety of different approaches. I have done some work on one that is ATtiny2313 (cousin of the ATMega328P used in the Arduino) based and works for Unipolar steppers. Design works, but s not finished - other projects took over and it go back burnered...

http://arduino.cc/forum/index.php/topic,84809.0.html is the link to the information I came up with as I worked on it if you are interested.

kf2qd:

Thank you very much for your post.

In my mind, Motor Shield works as stepper driver.

Could I add "AFmotor.h" in the code?

Hope someone can help me modify above code.

Thanks

kf2qd

Do you know the outputs to control the motor shield?

It should not be difficult to write your own function to step the motor as appropriate -

This is how I did it when I was developing my stepper controller - for starting out is should give you some code you can read and understand. Later I used some more advanced code that works faster.
This is assuming a unipolar motor.

of course ctr and dir are globally defined ints.

Code: [Select]
void Step()
{
  if (dir)
  {
    ctr++ & 3;   // code adds 1 to ctr and ANDs it with 3 so only least sig 2 bits are kept. Value of ctr goes from 0 to 3 and then resets to 0
  }
  else
  {
    ctr-- & 3;   // code subtracts 1 from ctr and ANDs it with 3 so only least sig 2 bits are kept. Value of ctr goes from 3 to 0 and then resets to 3
  }

  switch (ctr){
  case 0:
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);
    digitalWrite(pin4,LOW);
    break;
  case 1:
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);
    digitalWrite(pin4,LOW);
    break;
  case 2:
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,HIGH);
    digitalWrite(pin4,LOW);
    break;
  case 3:
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);
    digitalWrite(pin4,HIGH);
    break;
  }   


the code would look like this for a bipolar driver -
Code: [Select]
void Step()
{
  if (dir)
  {
    ctr++ & 3;   // code adds 1 to ctr and ANDs it with 3 so only least sig 2 bits are kept. Value of ctr goes from 0 to 3 and then resets to 0
  }
  else
  {
    ctr-- & 3;   // code subtracts 1 from ctr and ANDs it with 3 so only least sig 2 bits are kept. Value of ctr goes from 3 to 0 and then resets to 3
  }

  switch (ctr){
  case 0:
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    break;
  case 1:
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    break;
  case 2:
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    break;
  case 3:
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,HIGH);
    break;
  }   

srosefx

did this ever get solved

hi
i have been watching this post for a while and it seemed that it never got solved.
apart from the suggestion previous to my post.
id like to know if Richard had any success.

let me know.

thanks
simon

aiud

:)#define motor_step      12
#define motor_direction 11
#define encoder_a    2
#define encoder_b    3



volatile long motor_position, encoder;





void setup() {               
  // inìzializzazione
  Serial.begin(19200);
  Serial.println("TEST LETTURA ENCODER");

  pinMode (encoder_a, INPUT);
  digitalWrite (encoder_a, HIGH); //Attivo la resistenza  interna di pull-up
 
  pinMode (encoder_b, HIGH);
  digitalWrite (encoder_b, HIGH); //Attivo la resistenza di pull-up
 
  pinMode(motor_step, OUTPUT); 
  pinMode(motor_direction, OUTPUT);

aiud

:D
hello I tried the sketch and it works great, I too am new to Arduino, you must add a function to set the steps to the encoder reading I hope is an answer

AWOL

Quote
if I wasn't clear enough explaining my problem,

No, I think all three of your identical posts were equally clear.
Note I wrote "were" because I've now deleted two of the duplicates.

DO NOT CROSS POST - IT WASTES TIME.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

I am deeply sorry!
I am new to the forum, arduino and programing.
And I guess I exaggerated, as the impatience grew up after so much time of unsuccess.

I apologize, it won't happen again.

Go Up