How to use MDBT42Q with Nema17 stepper motors?

Posted on
  • Hey, y'all. I have the MDBT42Q breakout board and I am trying to create a simple example where a button is pressed and the stepper motor rotates for like a half revolution.

    I am using the A4988 drivers and I have managed to connect and continuously rotate the stepper motor using this sample code:

    const PIN_ENABLE = D14; 
    const PIN_STEP = D13;
    const PIN_DIRECTION = D12;
    
    digitalWrite(PIN_DIRECTION, direction);
    digitalWrite(PIN_ENABLE, enabled);
    
    analogWrite(PIN_STEP, 0.9, {freq: 100});
    

    I am not sure though how is it possible to count the steps per revolution and how frequency is related to rotation and time.
    Thanks in advance for your help :)

  • Hi! That's great - the A4988 stepper drivers do a lot of the work for you so you don't have to do much except count pulses.

    How they relate to how the motor turns depends on the motor and also the MS1/2/3 pins (info here) - but lets say your motor is marked as a 48 steps/revolution and you have 16x microstepping set up with the MS pins, that'd be 48*16=768 steps per revolution.

    What you're doing with analogWrite is getting Espruino to use the on-chip hardware to do the stepping - which makes it very reliable but also means it's hard to accurately count the amount of steps.

    While it's possible to send out a certain number of steps using hardware on the MDBT42, that's getting pretty hardcore, and for 100Hz steps you're fine using software.

    you could try something like this:

    function doSteps(cnt, callback) {
      if (cnt<=0) {
        if (callback) callback();
        return;
      }
      PIN_STEP.set();
      PIN_STEP.reset();
      setTimeout(doSteps, 10/*millisecs*/, cnt-1, callback);
    }
    
    // rotate 100 steps
    digitalWrite(PIN_DIRECTION, 1);
    doSteps(100);
    

    or to rotate 100 forward, 100 back:

    digitalWrite(PIN_DIRECTION, 1);
    doSteps(100, function() {
      digitalWrite(PIN_DIRECTION, 0);
      doSteps(100, function() {
       print("Done!");
      });
    });
    
  • Wow, @Gordon that was fast and damn awesome as well.
    Thanks so much for the info, I'll give it a try and let you know :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to use MDBT42Q with Nema17 stepper motors?

Posted by Avatar for vorillaz @vorillaz

Actions