Arduino controlled stepper motor for Mill X-axis drive

Advert

Arduino controlled stepper motor for Mill X-axis drive

Home Forums CNC machines, Home builds, Conversions, ELS, automation, software, etc tools Arduino controlled stepper motor for Mill X-axis drive

Viewing 25 posts - 1 through 25 (of 28 total)
  • Author
    Posts
  • #793716
    nevillet
    Participant
      @nevillet

      Hi all,
      In an effort to motorise the various axis drives of my small Sieg SX2P mill I’ve purchased a stepper motor (Nema 23, 3Nm) and an appropriate driver – all to be controlled by an Arduino.
      The driver details are here: https://www.skysmotor.co.uk/images/upload/File/DM542.pdf

      I’ve done some experiments using the Arduino IDE and have managed to get the motor working.

      My goal is to control the motor by using a single potentiometer like this:

      wWhen the pot is in the central position the motor is stopped.

      When turned a small amount e.g. positive from the central position, the motor turns slowly clockwise.

      When the pot is turned further positive the speed will increase up to the maximum amount at the end of the pot’s travel.

      Retuning the pot to the central position at any point will stop rotation.

      Turning the pot in the e.g. negative direction will mirror the above rotation, but this time in the counter clockwise direction.

      The other thing to add would be to implement some limit switches (reed?) at the end of travel to protect the motor, linkage and the mill.

      Any suggestions would be most welcome.

      Advert
      #793727
      Russell Eberhardt
      Participant
        @russelleberhardt48058

        You could just connect the pot to one of the analog inputs of the Arduino but it might be better to use a rotary encoder. Apush button for the stop function would then effectively return the speed control to its neutral position.

        Russell

        #793735
        Stuart Smith 5
        Participant
          @stuartsmith5

          I have done something similar but used a pot connected to an analogue port for speed and two momentary switches for direction. Ie only moves while I have the appropriate button pressed . The speed setting is continuously sensed so you can change the speed on the fly.

          If you are interested I can send you the Arduino code I used. It is a modified version of a sketch that someone else has done.

          Stuart

          #793737
          nevillet
          Participant
            @nevillet

            Hi Stuart,

            As I have plenty of pots kicking around (I know i only need 1) I’ll try that route first.

            Stuart, you solution and code would be most welcome!

            #793746
            Peter Cook 6
            Participant
              @petercook6

              Neville,  a few random thoughts.

              With a rotary encoder solution you would probably need some sort of  display to tell you what speed you have set which adds to the complexity.

              I would be concerned at your centre point = off solution. You will need a decent sized dead zone at off to ensure you can hit it when you need to (possibly in a hurry). I would also be concerned that if the pot gets dirt in it or a wire disconnects and it defaults to providing zero volts, you end up with the table going (left?) at max speed. Not a good idea with a milling machine!

              I would suggest using a simple pot for speed and a separate On – Off – On toggle switch mounted horizontally so that the direction of the lever indicates the direction of motion.

              I would also suggest writing some no-volt code into the Arduino so that the motor won’t run until the switch has been set to off for a second or so. You don’t want the table setting off when you put the power on if you have forgotten to set the switch to off – or it got knocked while the power was off.

              PS edit – If you also add a momentary push button, you could add code to move the table at maximum speed while the button is pressed. This will allow you to set a good speed for the cut, take a cut. Reverse the direction and hold the fast button to reposition the table back to the beginning of the cut, put on the next cut and go forward again at the preset speed.

              #793749
              nevillet
              Participant
                @nevillet

                I was thinking that reading the value from the pot (0-1023) the Off area could be programmed to be something like 380-580.  The On-Off-Off sounds like a better more robust plan with the pot simply controlling the speed of the motor.

                #793752
                SillyOldDuffer
                Moderator
                  @sillyoldduffer

                  Rotary encoder for choice.  In addition to reporting value and direction in one,  they are easily managed with an Interrupt Service Routine.   Then moving the rotary control causes the Arduino to save what it’s doing, execute the control, and then continue from where it left off.     In contrast, the analogue input has to be polled, it’s not obvious how to manage whether the pot is moving or not when the measurement is taken, and writing the whole program to meet time constraints can be challenging.  Accuracy somewhat limited too.   Pots wear out, and the value has to be considered – 5k Linear would be OK.

                  Stuart’s momentary switches work best as interrupts too (to avoid polling), but a rotary encoder keeps the hardware simpler.

                  Rotary encoders vary from expensive when accuracy is needed to delightfully cheap.  For this application, cheap is fine.  This £4 KY040 is often found in starter kits or can be bought separately:

                  KY040

                  Code examples here, with and without interrupts.

                  Dave

                  #793759
                  John Haine
                  Participant
                    @johnhaine32865

                    Run grbl on the Arduino, add a Bluetooth adapter, then use the free grbl controller app on your phone in jog mode to vary speed and direction and effectively give you virtual stops.  You’ll also get a free hand controller.  I’ve described my solution on these lines on this forum.

                    No need to worry about limit switches with only a nema23, unlikely to have enough torque to break anything

                    #793762
                    Macolm
                    Participant
                      @macolm

                      I have a basic Align X axis drive on a mill/drill. Some thoughts occur based on experience with that.

                      If you want to retain hand wheel operation, make sure the motor can be declutched, or alternatively that it can be set to a minimal drag mode. For setting things up, I find manual operation is essential.

                      The Align has an override button for fast slew. I use that quite a lot, for example to get to a clear space for tool changing, or even to get back to check or modify DRO zero. Rather than changing your pot position for fast, I would suggest separate switches. This makes retaining the speed setting easy once you have found the best value.

                      You may not need this, but I have found it sensible to allow over travel on the end stops so that there is protection if you misjudge stopping distance. The original Align stops could be damaged if used with fast slew. You still do, of course, have to ensure you set things to cover the worst case.

                      #793767
                      nevillet
                      Participant
                        @nevillet

                        With the stepper motor de-energised I can turn it fairly easily by hand and thought that would negate (perhaps) the requirement of a clutch.

                        #793781
                        John Haine
                        Participant
                          @johnhaine32865

                          My drive has a pin clutch between the drive pulley and the feedscrew to allow manual operation.  Even deenergised steppers are very “coggy” which makes fine manual adjustment tricky.

                          #793788
                          Robert Atkinson 2
                          Participant
                            @robertatkinson2

                            OR

                            use a 555 timer, pot and a couple of switches.

                            You don’t need a microprocessor for this application…..

                            Robert.

                            #793791
                            Martin Kyte
                            Participant
                              @martinkyte99762

                              If I was designing the control interface as a minimum I would want a Pot to set speed from zero to max. Momentary action push buttons for start and stop. Toggle switch to set direction. Emergency Stop  button to act as an emergency stop.

                              I would code for a ramp in speed on start/stop. Default to off on power up (NVR action).

                              Embellishments would be a digital read out of the linear feed rate, Feed a set distance and stop, return to origin and stop or repeat cycle for slot cutting.

                              regards Martin

                              #793796
                              Sonic Escape
                              Participant
                                @sonicescape38234

                                I would add  few sugestions if you want to use a potentiometer:

                                – use a central detent model. In this way you could set it into neutral position just by feeling.

                                – most safety issues relaten to a potentiometer can be solved from SW ( filter unplausible states, abrupt changes, stop motor an power on / reset regardless of the pot position an so on). Also connect the pot cursor to a fixed voltage using a large enough resistor. So you have a stable and detectable error state if the wiper doesn’t touch the track. Or use a multi section pot and turn off the motor if they don’t agree. Of course enable the watchdog. And so on.

                                If you don’t want extra wires for reed switches you can monitor the motor current. Or if you adjust the curent capability of the supply to provide just enough current you should be able to ignore the detection. Nothing will hapen a few seconds until you turn the pot. You are nearby anyway.

                                #793799
                                Speedy Builder5
                                Participant
                                  @speedybuilder5

                                  I have done this with my mill and added a display counting the steps showing the ‘X’ position (does not account for backlash). Have also added ‘jog’ of 0.001” and 0.010”. Also have micro switch limit switches and ‘soft’ start/stop.

                                  I needed 2 x Nanos as the display is done by one and stepping etc by the other.

                                  you can have code and circuit diags if you want.

                                  bob

                                  #793805
                                  duncan webster 1
                                  Participant
                                    @duncanwebster1

                                    X drive on my Centec is by arduino controlled stepper. Interface is by joystick, up for fast, down for stop, left and right obvious. It has soft start and end of travel switches. I’m part way along incorporating software end stops, but it works well as it is, so that is stalled. The stepper is permanently engaged via toothed belt, I don’t have any issues with cogging. Anyone wants further details pm me

                                     

                                    #793868
                                    SillyOldDuffer
                                    Moderator
                                      @sillyoldduffer
                                      On Peter Cook 6 Said:

                                      With a rotary encoder solution you would probably need some sort of  display to tell you what speed you have set which adds to the complexity.

                                      Maybe, though watching the motor reveals how fast it’s turning – set it by eye!    The only difference between a pot and rotary converter is the code that reads the control;  the pot decides speed based on an analogue voltage, the rotary encoder by counting pulses.  After that the code that drives the stepper is identical, both set the pulse rate.   If a speed display is needed, it’s the same for both solutions; it reports the pulse rate, not the knob position.

                                      I would be concerned at your centre point = off solution. You will need a decent sized dead zone at off to ensure you can hit it when you need to (possibly in a hurry). I would also be concerned that if the pot gets dirt in it or a wire disconnects and it defaults to providing zero volts, you end up with the table going (left?) at max speed. Not a good idea with a milling machine!

                                      Agreed.  Pots are simple to understand when they are working but what the software must do when they fail needs thinking about.    The programmer has to cover a host of edge cases, which is tricky.  The root cause is pots are analogue, i.e. everything they output is meaningful and has to be managed.  In contrast, rotary encoders are digital, only outputting meaningful values when moving, which reduces the number of edge cases, making them somewhat more likely to ‘fail-safe’.

                                      I would suggest using a simple pot for speed and a separate On – Off – On toggle switch mounted horizontally so that the direction of the lever indicates the direction of motion.

                                      No need with a rotary encoder, though a pair of right-left LEDs might be a nice touch.  Many encoders come with a push-switch that could be pressed to reset ‘centre stop’.

                                      I would also suggest writing some no-volt code into the Arduino so that the motor won’t run until the switch has been set to off for a second or so. You don’t want the table setting off when you put the power on if you have forgotten to set the switch to off – or it got knocked while the power was off.

                                      Yes if a pot is used.  Not needed with a rotary encoder, because these are ‘off’ until moved by the operator.  No need for defences like detents, dead zones, delays, NVR and whatnot.

                                      Can’t help wondering if the forum’s enthusiasm for potentiometers is because rotary encoders aren’t well known?  Encoders aren’t hard to learn, plenty of Arduino encoder examples on the web.  Once understood, rotary encoders are better than pots in many applications, so always worth consideration.

                                      Basic programmer training emphasises the need for programs to reject duff input in a cruel world where almost anything is possible.   Garbage In, Garbage Out!  Therefore common to find most professional code is error management, elaborately protecting a relatively simple core function from bad input. As pots and encoders have different shortcomings, how much code is needed to manage them?

                                      I recommend nevillet mocks-up both approaches; not much code needed.  Lay a motor, controller, PSU, Arduino and IDE out on a table.   Experiment with a pot, using jumper leads to create fault conditions, and write code to manage them!    After doing the same with a rotary encoder, decide which is best.  I’m confident the rotary code will be simpler, because the hardware has fewer and safer failure modes, but nevillet is free to decide for himself.

                                      Quite an interesting problem with workable proposals ranging from an NE555 to a Bluetooth connected GRBL.  They all have pros and cons!  How to decide?   Shine a bright light on all suggestions, asking “what could possibly go wrong” and “what do I have to do about malfunctions?”   And if nevillet wants to learn Arduino and this is just a suitable starter project, then the 555 is useless.   GRBL makes sense for other reasons, but I wouldn’t recommend it to a beginner learning to code – too complicated.

                                      Dave

                                      #793871
                                      John Haine
                                      Participant
                                        @johnhaine32865

                                        Grbl is easy, no coding needed.  Grbl controller is a phone app that can run gcode if you want but also has a user friendly jogging interface.

                                        #794003
                                        Peter Cook 6
                                        Participant
                                          @petercook6

                                          Dave, don’t disagree with most of what you say, but the idea of setting the speed of a table drive by spinning an encoder and watching what the motor is doing under power strikes me as one of your edge cases!

                                          I do use encoders, I have one for setting the “speed” on my little lathe. The display gives me two values, the power set point, and the actual spindle speed once running. With experience (actually some plotted curves for each belt position) I can set the speed fairly well before applying power.

                                          The mill uses a potentiometer, and again with experience I can set the position of the pot to roughly the speed I want before applying power. The nature of the pot means that it’s physical rotational position provides an analogue for the speed I want, something that a digital pulse encoder ( at least not the cheap ones) can’t do.

                                           

                                          #794008
                                          nevillet
                                          Participant
                                            @nevillet

                                            Hi again,

                                            I’ve found a very helpful video and sketch that controls the motors varying speed and direction with a pot and a switch.

                                            It does not have a stop switch but thought that a double pole switch in line with two of the motor wires will take care of that.

                                            The forward/reverse switch is a little flaky and understand this is down to a ‘debouncing’ issue and don’t know if this is a programming or a hardware problem.

                                            I’ve had it running on the bench powered with 24V from a bench supply and it never draws more than 800mA, admittedly with no load.

                                            The video can be found here:  https://www.youtube.com/watch?v=iY_4YOlpqyI&t=1423s

                                            And the sketch here:  https://dronebotworkshop.com/big-stepper-motors/

                                            #794012
                                            John Haine
                                            Participant
                                              @johnhaine32865

                                              Do NOT put a switch in series with the motor wires.  Disconnecting the motor while energised stands a good chance of destroying the driver.

                                              #794016
                                              nevillet
                                              Participant
                                                @nevillet

                                                Thanks John.   Not elegent but how about cutting power to driver?

                                                #794019
                                                Robert Atkinson 2
                                                Participant
                                                  @robertatkinson2

                                                  The current drawn by a stepper motor does not change significantly with speed or load. It is, or should be set by the driver circuit. There are many variables but generally the motor draws the same current when stopped  or at full load.

                                                  Robert.

                                                  #794023
                                                  nevillet
                                                  Participant
                                                    @nevillet

                                                    The motor is rated at 4A and I set the max current below that.

                                                    #794030
                                                    SillyOldDuffer
                                                    Moderator
                                                      @sillyoldduffer
                                                      On nevillet Said:

                                                      Thanks John.   Not elegent but how about cutting power to driver?

                                                      It’s what the EN terminal is for.   The motor gets no power unless the drive is EN-abled.

                                                      A delight for those new to digital logic is that 0 and 1 can have their meanings reversed, so 0=ON and 1=OFF or 0=OFF and 1=ON.  That’s why the controller has pairs of terminals, PUL+ PUL-, EN+ EN-, and DIR+ DIR;  they decide which way up the logic is.  (Inverting digital logic may seem over complicated but it’s handy in electronics and computing.)

                                                      In this example from the T6600 manual the motor is enabled by the microcontroller; the program controls it.

                                                      Screenshot 2025-04-18 214815

                                                      The motor is enabled by grounding EN-. which could be done by a switch.

                                                      Three common ways of applying power to steppers, depending on the requirement:

                                                      • Hardwiring EN so the motor is always on.
                                                      • EN with a physical ON/OFF switch.
                                                      • EN with an IO pin on the microcontroller, you write the code, which can be as complicated as you wish!

                                                      Why is complicated useful?  One example, Enabled Stepper motors have torque when stopped allowing them to hold a load.  Acting like a brake may be what’s wanted, but if holding isn’t required it wastes power and the motor gets warm.  Therefore applications can be programmed to dis-Enable steppers so the motor freewheels. If the motor hits an end stop, not pulsing PUL will stop and lock the motor in position when it might be more appropriate to turn the power off with EN.

                                                      Dave

                                                       

                                                    Viewing 25 posts - 1 through 25 (of 28 total)
                                                    • Please log in to reply to this topic. Registering is free and easy using the links on the menu at the top of this page.

                                                    Advert

                                                    Latest Replies

                                                    Viewing 25 topics - 1 through 25 (of 25 total)
                                                    Viewing 25 topics - 1 through 25 (of 25 total)

                                                    View full reply list.

                                                    Advert

                                                    Newsletter Sign-up