Encode how accurate is it

Hi
Can some one tell me if this is the best I can get

I am running a stepper at about 300rpm and have a encoder with 400 seps per rev

I have cut the program down to run as fast as possible (I think but I am no expert)

I ask the stepper to run 2000 steps then run back to an end stop

for each rpm the carriage move 2mm i.e. 0.005mm per step which is very small

I am using one interrupt I am counting the No forward and backward and they are not the same see below
is this good or bad and what is the best I can get
This is a hobby and I have not been doing it very long

Ready PRESS START
Starting
2004
2001
-4
Ready PRESS START
Starting
2011
2001
-11
Ready PRESS START
Starting
1994
2001
7
Ready PRESS START
Starting
1973
2001
27
Ready PRESS START
Starting
2043
2001
-44
Ready PRESS START
Starting
2039
2001
-39

You want us to tell you how good your code is? Did you forget something? Like the code you want us to look at?

If you put a mark on the stepper motor shaft (or pulley, or whatever) it should be easy to see if it doesn’t return to the starting position. (That would be with your software counting steps and ignoring the encoder.) You could also go 2000 steps (10 full revolutions) and pause to visually check the angle before going 2000 steps back.

…It’s somewhat unusual to use an encoder with a stepper motor. Usually, there are one or two "home’ or “limit” switches/sensors to find the starting-point, and then you just count the steps.

OK sorry to not give the correct question
I am running a stepper motor at 300rpm and have an encoder reading at 400 steps per rev
so the encoder is giving out 2000 per sec

I am using an interrupt
attachInterrupt(digitalPinToInterrupt(encoder1PinB), ai1, RISING );// pin 3
void ai1() {

counter = ++counter;

}

from void loop()
do
{
forward(); // set direction

tone(speed,xrun);
}
while (counter <= 61200); //102.6/mm about

stop();//disconect motor

the above is only part of the code but it’s the main part

the carriage that the stepper motor is driving is moving to the same point almost
in varies about 0.5mm 1 rev = 2mm i.e. 100 encoders signals
I would like more accuracy
what I am asking “is the can the uno do this job”

the distance travel i.e. counter value is set at the star but will vary on different jobs

First, are we to assume you are running the stepper motor at 200 steps per revolution?

Second, are there gears involved in moving the carriage and how much slop is in the gearing?

Third, is your 0.5mm close to the width of one segment on your encoder, times the gearing ratio/

Paul

Paul_KD7HB:
First, are we to assume you are running the stepper motor at 200 steps per revolution?

Second, are there gears involved in moving the carriage and how much slop is in the gearing?

Third, is your 0.5mm close to the width of one segment on your encoder, times the gearing ratio/

Paul

Paul thanks for the reply
NO the stepper is running at 400 steps no gears are involved, the lead screw has a pitch of 2mm

one segment of the encoder there are 400 per rev so I assume is 2/400 = 0.005mm = 1 segment

I am using a HY-DIV268N-5A to control the motor and the "tone(speed,xrun);" command
xrun is a variable set by the operator but assume in about 2000

I do not know what I am doing really, I am fascinated by what the Arduino can do and I am trying to improve my welder welder - YouTube

joc1274:
Paul thanks for the reply
NO the stepper is running at 400 steps no gears are involved, the lead screw has a pitch of 2mm

one segment of the encoder there are 400 per rev so I assume is 2/400 = 0.005mm = 1 segment

I am using a HY-DIV268N-5A to control the motor and the “tone(speed,xrun);” command
xrun is a variable set by the operator but assume in about 2000

I do not know what I am doing really, I am fascinated by what the Arduino can do and I am trying to improve my welder welder - YouTube

Ok, so hard to get the whole picture.

With your concern with accuracy, You must be running a ball screw and not a nut or worse yet, a split nut on the the lead screw.

If you are measuring the 0.5 mm, is it on the piece attached and following the lead screw, or is it the lead screw itself that is 0.5mm. If it is the table or mechanism to the lead screw is moving, you are asking the wrong forum to solve your problem.

Paul

Paul_KD7HB:
Ok, so hard to get the whole picture.

With your concern with accuracy, You must be running a ball screw and not a nut or worse yet, a split nut on the the lead screw.

If you are measuring the 0.5 mm, is it on the piece attached and following the lead screw, or is it the lead screw itself that is 0.5mm. If it is the table or mechanism to the lead screw is moving, you are asking the wrong forum to solve your problem.

Paul

I am using a a trapezoidal lead screws and nut
and it is the table I am measuring the distance move must be constant may be I should increase the 0.05 to 1mm

I do not understand why this is the wrong forum can you please explain

John

the above is only part of the code but it’s the main part

It is not sufficient. Post ALL your code and post it in code tags (use the </> icon)

Pete

Pete I hope this helps

Note I only use one of the phases of the encoder i.e. pin 3

/16/3/17
Moving the carraige a set distance
about line 164 while (counter <= 2052); //205.2/mm
/
//
*****************
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//**********************************

volatile unsigned int counter = 0;
volatile unsigned int counter1 = 0;
volatile unsigned int counterEnd = 0;
const int encoder0PinA = 11;
const int encoder0PinB = 12;
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int FSTOP = 5; //Front Stop
const int AIN1 = 9; //rotate Low Forward
const int AIN2 = 8; //Motor Engaged High dis-engauge
const int START = 7; //Start Button
int speed = 10;
int encoder0PinALast = LOW;
int n = LOW;
int i;
int rotate; // 1 = forward 3 Backward
int xstart = 2000;
int xrun = xstart;
int pausetime = 0;
int errocode;
unsigned long time1;
unsigned long time;

void stop()
{
digitalWrite(AIN2, HIGH);// Motor dis-engauged
//delayMicroseconds (50);
// Serial.println(“stopped”);
}
void accelerate () {

// Serial.println (“accelerate”);
i = xstart ;
while (i< xrun)
{
i = i +100;
tone(speed,i);
//delayMicroseconds(4);
//Serial.println (i);
}
}
//*******************************
void backward()
{
//Serial.println(“backward”);
rotate = 3;
digitalWrite(AIN1, HIGH);// backward
digitalWrite(AIN2, LOW);// Motor engauged

accelerate();
}
//*******************************
void forward()
{
// Serial.println(“forward”);
rotate = 1;
digitalWrite(AIN1,LOW); //forward
digitalWrite(AIN2, LOW);// Motor engauged

accelerate();
}
//*******************************
void error()
{
//Serial.print(" Erro Code = ");
//Serial.println(errocode);
}
//*******************************
void readspeed(){

n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
xrun = xrun - 100;
}
else
{
xrun = xrun + 100;
}
}
if( xrun < 500){
xrun = 500;
}

encoder0PinALast = n;

}

//*******************************

void setup()
{
// by default, we’ll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
display.display();// to displace the Adfruit logo that is embeded
display.clearDisplay();
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(START, INPUT);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode(speed,OUTPUT);

pinMode(encoder1PinA, INPUT); // set pin to input
pinMode(encoder1PinB, INPUT); // set pin to input

digitalWrite(encoder1PinA, HIGH); // turn on pullup resistors
digitalWrite(encoder1PinB, HIGH); // turn on pullup resistors
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr encoder1PinA on moust Arduino.
// attachInterrupt(1, ai0, RISING );

//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
attachInterrupt(digitalPinToInterrupt(encoder1PinB), ai1, RISING );// pin 3

displayscreen();
}

void loop() {
// Serial.print ("Forward = ");
// Serial.println (counter1);
// Serial.print ("Backwards = ");
//Serial.println (counter);

//Serial.println (counter1 - counter);

counter = 0;
counter1 = 0;
digitalWrite(AIN2, HIGH);// Motor dis-engauged
Serial.println("Ready PRESS START ");

while (digitalRead(START) == LOW )//pin 7
{
readspeed();
displayscreen();
}
Serial.println(“Starting”);
Serial.println (counter);
pausetime = 500;
pause();//to stop bounce

do
{
forward();
// backward();
tone(speed,xrun);
}
while (counter <= 61200); //102.6/mm

stop();
Serial.println (counter);
counter1 = counter ;
counter = 0;
//Serial.println (counter1);
while (digitalRead(START) == LOW )//pin 7

pausetime = 500;
pause();//to stop bounce
backward();
tone(speed,xrun);
do
{
//Nothing
}
while (digitalRead(FSTOP) == LOW);//pin 7

stop();
// pausetime = 400;
//pause();//to stop bounce
}
//end of loop

void ai1() {
//Serial.println (“ai1”);
// ai0 is activated if DigitalPin nr encoder1PinB is going from LOW to HIGH
// Check with pin encoder1PinA to determine the rotate

counter = ++counter;
//Serial.println (counter);
}

void pause(){
time1 = millis();
do{
// nothing
} while (millis() - time1 < pausetime);
}

void displayscreen(){

display.clearDisplay();
display.setTextSize(2);
display.setCursor(6,15);
display.setTextColor( WHITE);
display.println(xrun);
display.display();
//Serial.println (xrun);
}

el_supremo:
It is not sufficient. Post ALL your code and post it in code tags (use the </> icon)

Pete

Pete
sorry I do not now what and post it in code tags (use the </> icon) means
I am new to this

joc1274:
I am using a a trapezoidal lead screws and nut
and it is the table I am measuring the distance move must be constant may be I should increase the 0.05 to 1mm

I do not understand why this is the wrong forum can you please explain

John

Because you are working with problems appropriate to a CNC forum. I understand some CNC software can allow for variations in your lead screw, including slack when the screw begins and when it stops. I recall it has to be measured the entire length of the leadscrew and placed in a table so appropriate + and - can be applied to the stepper motor incrementing.

Changing to a recirculating ball screw can decrease your difference greatly. My CNC milling machine has ball screws and stepper motors. I cannot tell any difference in moving one way or another. However, my lathe has a lead screw and split nuts and there is visible slop in changing from right to left on the carriage.

Paul

Read How to post code properly and then edit your message #9 and insert the code tags.

Pete

/16/3/17
Moving the carraige a set distance
about line 164 while (counter <= 2052); //205.2/mm
/
//
*****************
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//**********************************

volatile unsigned int counter = 0;
volatile unsigned int counter1 = 0;
volatile unsigned int counterEnd = 0;
const int encoder0PinA = 11;
const int encoder0PinB = 12;
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int FSTOP = 5; //Front Stop
const int AIN1 = 9; //rotate Low Forward
const int AIN2 = 8; //Motor Engaged High dis-engauge
const int START = 7; //Start Button
int speed = 10;
int encoder0PinALast = LOW;
int n = LOW;
int i;
int rotate; // 1 = forward 0 Backward
int xstart = 2000;
int xrun = xstart;
int pausetime = 0;
int errocode;
unsigned long time1;
unsigned long time;

void stop()
{
digitalWrite(AIN2, HIGH);// Motor dis-engauged
//delayMicroseconds (50);
// Serial.println(“stopped”);
}
void accelerate () {

// Serial.println (“accelerate”);
i = xstart ;
while (i< xrun)
{
i = i +100;
tone(speed,i);
//delayMicroseconds(4);
//Serial.println (i);
}
}
//*******************************
void backward()
{
//Serial.println(“backward”);
rotate = 3;
digitalWrite(AIN1, HIGH);// backward
digitalWrite(AIN2, LOW);// Motor engauged

accelerate();
}
//*******************************
void forward()
{
// Serial.println(“forward”);
rotate = 1;
digitalWrite(AIN1,LOW); //forward
digitalWrite(AIN2, LOW);// Motor engauged

accelerate();
}
//*******************************
void error()
{
//Serial.print(" Erro Code = ");
//Serial.println(errocode);
}
//*******************************
void readspeed(){

n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
xrun = xrun - 100;
}
else
{
xrun = xrun + 100;
}
}
if( xrun < 500){
xrun = 500;
}

encoder0PinALast = n;

}

//*******************************

void setup()
{
// by default, we’ll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
display.display();// to displace the Adfruit logo that is embeded
display.clearDisplay();
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(START, INPUT);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode(speed,OUTPUT);

pinMode(encoder1PinA, INPUT); // set pin to input
pinMode(encoder1PinB, INPUT); // set pin to input

digitalWrite(encoder1PinA, HIGH); // turn on pullup resistors
digitalWrite(encoder1PinB, HIGH); // turn on pullup resistors
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr encoder1PinA on moust Arduino.
// attachInterrupt(1, ai0, RISING );

//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
attachInterrupt(digitalPinToInterrupt(encoder1PinB), ai1, RISING );// pin 3

displayscreen();
}

void loop() {
// Serial.print ("Forward = ");
// Serial.println (counter1);
// Serial.print ("Backwards = ");
//Serial.println (counter);

//Serial.println (counter1 - counter);

counter = 0;
counter1 = 0;
digitalWrite(AIN2, HIGH);// Motor dis-engauged
Serial.println("Ready PRESS START ");

while (digitalRead(START) == LOW )//pin 7
{
readspeed();
displayscreen();
}
Serial.println(“Starting”);
Serial.println (counter);
pausetime = 500;
pause();//to stop bounce

do
{
forward();
// backward();
tone(speed,xrun);
}
while (counter <= 61200); //102.6/mm

stop();
Serial.println (counter);
counter1 = counter ;
counter = 0;
//Serial.println (counter1);
while (digitalRead(START) == LOW )//pin 7

pausetime = 500;
pause();//to stop bounce
backward();
tone(speed,xrun);
do
{
//Nothing
}
while (digitalRead(FSTOP) == LOW);//pin 7

stop();
// pausetime = 400;
//pause();//to stop bounce
}
//end of loop

void ai1() {
//Serial.println (“ai1”);
// ai0 is activated if DigitalPin nr encoder1PinB is going from LOW to HIGH
// Check with pin encoder1PinA to determine the rotate

counter = ++counter;
//Serial.println (counter);
}

void pause(){
time1 = millis();
do{
// nothing
} while (millis() - time1 < pausetime);
}

void displayscreen(){

display.clearDisplay();
display.setTextSize(2);
display.setCursor(6,15);
display.setTextColor( WHITE);
display.println(xrun);
display.display();
//Serial.println (xrun);
}
[code][code][/code][/code]

el_supremo:
Read How to post code properly and then edit your message #9 and insert the code tags.

Pete

Thanks for all your help but it's getting way over my head I have tried but it's not worked

joc1274:
Thanks for all your help but it’s getting way over my head I have tried but it’s not worked

Hear are some forums where you may find additional help:

http://www.cnczone.com/forums/

http://bbs.homeshopmachinist.net/forum.php

They have been big helps to me in the past.

An additional question on your setup relating to accuracy: how do you have the thrust bearings for the lead screw set up. Are they adjustable? They have a primary function as to accuracy of your leadscrew. I know when they need tightening on my lathe, the first indication is loss of accuracy.

Paul

Paul_KD7HB:
Hear are some forums where you may find additional help:

http://www.cnczone.com/forums/

http://bbs.homeshopmachinist.net/forum.php

They have been big helps to me in the past.

An additional question on your setup relating to accuracy: how do you have the thrust bearings for the lead screw set up. Are they adjustable? They have a primary function as to accuracy of your leadscrew. I know when they need tightening on my lathe, the first indication is loss of accuracy.

Paul

Thanks Paul for taking the time to answer