element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Arduino
  • Products
  • More
Arduino
Arduino Forum control stepper motor by optical encoder
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 2 replies
  • Answers 1 answer
  • Subscribers 102 subscribers
  • Views 643 views
  • Users 0 members are here
Related

control stepper motor by optical encoder

balexfox
balexfox over 1 year ago

Hello everyone. used a simple code to print to a DTG printer. but when I started printing blanks, or with a little filling of the picture, the picture began to shrink and stretch. since the synchronization of the encoder with the motor is not in real time, it can be seen from the code. did a bunch of work on the rest of the sensor signal code on the printer. and it turned out during the test. who can suggest or help how to implement. synchronization of an optical encoder with a stepper motor in real time with a full transfer of speed (acceleration). thank you all for your help

 

reading encoder

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


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

 

stepper control

if (encoder > 0){
         digitalWrite(motor_direction, HIGH);  
         digitalWrite(motor_step, HIGH);      
         digitalWrite(motor_step, LOW);       
         _delay_us(800);                      
         motor_position++;                     
         encoder = 0;                         
      }
      else if (encoder < 0){
         digitalWrite(motor_direction, LOW);  
         digitalWrite(motor_step, HIGH);      
         digitalWrite(motor_step, LOW);      
         _delay_us(800);                   
         motor_position--;                    
         encoder = 0;                          
       }

 

As I understand it, while the delay 800 encoder is not read as there is a delay. but if even to implement on millis there the coefficient needs to be put down? as there should be synchronization. information has not yet been found. I ask for help.

I don't even think about software protection against pulsation, interference or inertia and other things yet. for now I need to make friends with an encoder

 

motor_position++;

also I do not understand why this line may have been an upgrade but not about it now

  • Reply
  • Cancel
  • Cancel
Parents
  • balexfox
    0 balexfox over 1 year ago

    thank you to autor i find what i look for

     

     

    #include <AccelStepper.h>
    
    #define encoder_pin_A 8
    #define encoder_pin_B 9
    int encoder_pin_A_last = LOW;
    int encoder_pos = 0;
    int n = LOW;
    
    #define stepper_pin_step 6
    #define stepper_pin_dir  5
    
    // Enc have 24 steps per revolution
    // The motor have 800 steps per revolution
    // Want: 1 encoder rev = 1 stepper rev
    // 800 / 24 = 33.3333333333...
    float steps_per_pulse = 33.3333333333;
    
    AccelStepper stepper(AccelStepper::DRIVER, stepper_pin_step, stepper_pin_dir);
    
    void setup() {
      stepper.setMaxSpeed(800.0);
      stepper.setAcceleration(300.0);
       
      pinMode(encoder_pin_A, INPUT_PULLUP);
      pinMode(encoder_pin_B, INPUT_PULLUP);
    }
    
    void loop() {
      // read encoder
      n = digitalRead(encoder_pin_A);
       
      if ((encoder_pin_A_last == LOW) && (n == HIGH)) {
        if (digitalRead(encoder_pin_B) == LOW) {
          encoder_pos--;
        } else {
          encoder_pos++;
        }
    
        // set stepper to the new calculated position
        stepper.moveTo((long) round(encoder_pos*steps_per_pulse));
      }
       
      encoder_pin_A_last = n;
    
      stepper.run();
    }

     

    link

    https://www.norwegiancreations.com/2019/08/arduino-as-a-stepper-motor-controller-jogging-with-acceleration/

    • Cancel
    • Up 0 Down
    • Reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • balexfox
    0 balexfox over 1 year ago

    thank you to autor i find what i look for

     

     

    #include <AccelStepper.h>
    
    #define encoder_pin_A 8
    #define encoder_pin_B 9
    int encoder_pin_A_last = LOW;
    int encoder_pos = 0;
    int n = LOW;
    
    #define stepper_pin_step 6
    #define stepper_pin_dir  5
    
    // Enc have 24 steps per revolution
    // The motor have 800 steps per revolution
    // Want: 1 encoder rev = 1 stepper rev
    // 800 / 24 = 33.3333333333...
    float steps_per_pulse = 33.3333333333;
    
    AccelStepper stepper(AccelStepper::DRIVER, stepper_pin_step, stepper_pin_dir);
    
    void setup() {
      stepper.setMaxSpeed(800.0);
      stepper.setAcceleration(300.0);
       
      pinMode(encoder_pin_A, INPUT_PULLUP);
      pinMode(encoder_pin_B, INPUT_PULLUP);
    }
    
    void loop() {
      // read encoder
      n = digitalRead(encoder_pin_A);
       
      if ((encoder_pin_A_last == LOW) && (n == HIGH)) {
        if (digitalRead(encoder_pin_B) == LOW) {
          encoder_pos--;
        } else {
          encoder_pos++;
        }
    
        // set stepper to the new calculated position
        stepper.moveTo((long) round(encoder_pos*steps_per_pulse));
      }
       
      encoder_pin_A_last = n;
    
      stepper.run();
    }

     

    link

    https://www.norwegiancreations.com/2019/08/arduino-as-a-stepper-motor-controller-jogging-with-acceleration/

    • Cancel
    • Up 0 Down
    • Reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube