# How to calculate resolution to 1:1

Can somebody help me out with how to calculate the following:
I have a quadrature encoder that gives me 1440 p/r and i wish to make my stepper work at a 1:1 ratio my stepper is a 1.8 degree so 200 p/r and it is being driven through a microstepper driver where i can select the number of microsteps and 400 being the smallest number so i need 400 steps to turn one revolution on my stepper motor. The driver is this type http://www.wantmotor.com/ProductsView.asp?id=257&pid=82
My maths is not the best and i have tried all ways to calculate this without success or arrive at a floating point number and the do not know how to apply it. Any help would be appreciated.

You want your stepper to turn one revolution for each one revolution of the encoder?

If the motor is 400 steps/rev and the encoder is 1440 counts/rev, then you need to step every 1140/200 counts of the encoder.

``````const float MOTOR_PPR = 400.0;
const float ENCODER_PPR = 1440.0;
float scale = MOTOR_PPR / ENCODER_PPR;

float encoderPos = 0;
int motorCurrentPos = 0;
int motorTargetPos = 0;

loop()
{
motorTargetPos = (int)(encoderPos * scale);
if (motorCurrentPos != motorTargetPos)
{
moveMotorToPos(motorTargetPos);
}
}
``````

Regards,
Ray L.

thanks for the replies, yes i want the motor to turn 1 complete revolution when the encoder does one complete revolution. And Ray where does the 1140 come from should this be 1440 ?

wijnendael:
thanks for the replies, yes i want the motor to turn 1 complete revolution when the encoder does one complete revolution. And Ray where does the 1140 come from should this be 1440 ?

Yes, 1440. Typo.

Regards,
Ray L.

You can set up that driver so that it has more steps per revolution. 1440/400 = 3.6 steps of the motor for each step of the encoder. If you set the steps per revolution of the motor to a higher value the resolution increases (fewer motor steps / encoder steps) and the ratio comes closer to 1:1.

1440 has prime factors 5, 3, 3, 2, 2, 2, 2, 2
400 has prime factors 5, 5, 2, 2, 2, 2

Eliminating common factors 5, 2, 2, 2, and 2:
1440 has remaining prime factors: 3, 3, 2 (332 = 18)
400 has remaining prime factors: 5

1440 * 5 = 7200 / 18 = 400

Count 5 for each pulse of the encoder and divide by 18 to get the corresponding stepper position.

Wow johnwasser you make it look simple when you say it like that !!
I seem to understand what people are saying and have been playing around with attempting to insert this into the example encoder to stepper code i found on the net somewhere but do not seem to be able to figure out how to apply it to something like this.

``````[quote]
#define encoder_a 2 [color=#7E7E7E]//keep this on and interrupt pin[/color]
#define encoder_b 3 [color=#7E7E7E]//keep this on and interrupt pin[/color]
#define motor_step 4 [color=#7E7E7E]//can be any pin[/color]
#define motor_direction 5 [color=#7E7E7E]//can be any pin[/color]

#include <[color=#CC6600]delay[/color].h>
volatile [color=#CC6600]long[/color] motor_position, encoder;

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color] () {
[color=#7E7E7E]//set up the various outputs[/color]
[color=#CC6600]pinMode[/color](motor_step, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](motor_direction, [color=#006699]OUTPUT[/color]);

[color=#7E7E7E]// then the encoder inputs[/color]
[color=#CC6600]pinMode[/color](encoder_a, [color=#006699]INPUT[/color]);
[color=#CC6600]pinMode[/color](encoder_b, [color=#006699]INPUT[/color]);
[color=#7E7E7E]// enable pullup as we are using an open collector encoder[/color]
[color=#CC6600]digitalWrite[/color](encoder_a, [color=#006699]HIGH[/color]);
[color=#CC6600]digitalWrite[/color](encoder_b, [color=#006699]HIGH[/color]);

[color=#7E7E7E]// encoder pin on interrupt 0 (pin 2)[/color]
[color=#CC6600]attachInterrupt[/color](0, encoderPinChangeA, [color=#006699]CHANGE[/color]);
[color=#7E7E7E]// encoder pin on interrupt 1 (pin 3)[/color]
[color=#CC6600]attachInterrupt[/color](1, encoderPinChangeB, [color=#006699]CHANGE[/color]);
encoder = 0; [color=#7E7E7E]//reseet the encoder position to 0[/color]
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
[color=#7E7E7E]//do stuff dependent on encoder position here[/color]
[color=#7E7E7E]//such as move a stepper motor to match encoder position[/color]
[color=#7E7E7E]//if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying[/color]
[color=#CC6600]if[/color] (encoder > 0) {
[color=#CC6600]digitalWrite[/color](motor_direction, [color=#006699]HIGH[/color]);[color=#7E7E7E]// move stepper in reverse[/color]
[color=#CC6600]digitalWrite[/color](motor_step, [color=#006699]HIGH[/color]);
[color=#CC6600]digitalWrite[/color](motor_step, [color=#006699]LOW[/color]);
[color=#CC6600]delayMicroseconds[/color](200); [color=#7E7E7E]//_delay_us(200); //modify to alter speed[/color]
motor_position++;
encoder = 0; [color=#7E7E7E]//encoder--;[/color]
}
[color=#CC6600]else[/color] [color=#CC6600]if[/color] (encoder < 0) {
[color=#CC6600]digitalWrite[/color] (motor_direction, [color=#006699]LOW[/color]); [color=#7E7E7E]//move stepper forward[/color]
[color=#CC6600]digitalWrite[/color] (motor_step, [color=#006699]HIGH[/color]);
[color=#CC6600]digitalWrite[/color] (motor_step, [color=#006699]LOW[/color]);
[color=#CC6600]delayMicroseconds[/color](200); [color=#7E7E7E]//_delay_us(200); //modify to alter speed[/color]
motor_position--;
encoder = 0; [color=#7E7E7E]//encoder++;[/color]
}
}

[color=#CC6600]void[/color] encoderPinChangeA() {
encoder--;
}
[color=#CC6600]else[/color]{
encoder++;
}
}

[color=#CC6600]void[/color] encoderPinChangeB() {
encoder--;
}
[color=#CC6600]else[/color] {
encoder++;
}
}

[/quote]
``````

I have tried various methods of scaling the encoder pulses to match the stepper where it says but none have worked yet can someone give me some more hints to look at or is it not possible with this code. Would it just be a case of doing johnwassers calculation somewher on the encoder count ?

whoops looks like i posted the code wrong i will try again.

``````#define encoder_a 2 //keep this on and interrupt pin
#define encoder_b 3 //keep this on and interrupt pin
#define motor_step 4 //can be any pin
#define motor_direction 5 //can be any pin

#include <delay.h>
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);
// enable pullup as we are using an open collector encoder
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; //reseet the encoder position to 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);// move stepper in reverse
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
delayMicroseconds(200); //_delay_us(200); //modify to alter speed
motor_position++;
encoder = 0; //encoder--;
}
else if (encoder < 0) {
digitalWrite (motor_direction, LOW); //move stepper forward
digitalWrite (motor_step, HIGH);
digitalWrite (motor_step, LOW);
delayMicroseconds(200); //_delay_us(200); //modify to alter speed
motor_position--;
encoder = 0; //encoder++;
}
}

void encoderPinChangeA() {
encoder--;
}
else{
encoder++;
}
}

void encoderPinChangeB() {