Arduino Stepper Con...
 
Notifications
Clear all

Arduino Stepper Control

40 Posts
5 Users
6 Reactions
465 Views
(@davee)
Member
Joined: 3 years ago
Posts: 1765
 

Hi Ron @zander, @will and @azslab,

 Just some comments that I hope may be useful for turning Bill (@dronebot-workshop)'s demonstration sketch, into a CNC tool.

  I am aware of the possibility of using code for switch debounce, but personally, I think it is better to use hardware. As there is already a resistor in the sketch circuit, the only addition is a capacitor ... something like 0.47 microFarad ceramic (connected in parallel with the switch), would probably do, assuming a 10 kOhm pull-up resistor.

I note in the blog, Bill mentions the possible need for debounce, but he is also demonstrating how to reverse the motor using the same switch, so this is less of an issue, but this is less likely to happen in a 'professional' CNC application.

Whilst it is possible to use just one Arduino input, if there are limit switches at each end of the travel, it might be safer to provide each switch with its own input pin, debounce circuit, and software input, etc., which instead of reversing, stops the motor going any further in the limiting direction, but allows the controller to move the motor in the direction away from the limit switch, rather than automatically always reversing.

That is, the motor would just stop, when the limit switch is activated, but the motor could then be commanded to move in the opposite direction, either as a result of the next command in the CNC sequence, or because a user presses an appropriate command button. Users of 3D printers will be familiar with similar operations at the beginning of a print program, when the printer 'discovers' the limits of the printer by driving in one direction, until the appropriate limit switch is activated.

And similarly to 3D printers, I guess, there will also be some way of manually driving the motor, as well as any pre-programmed CNC operations.

---------

Of course, these are just suggestions ... they are not the only options.

In commercial development situations, it is normal for a prototype to start with over-simplified controls, etc. which are then modified during the later part of the project to produce a 'user-friendly' machine.

Best wishes and good luck with the project, Dave


   
ReplyQuote
(@azslab)
Member
Joined: 1 month ago
Posts: 14
Topic starter  

Hi Dave @davee, @zander and @will

Dave I only had some Pico Farad ceramic capacitors lying around. I wired some across the switch and the reverse interrupt is now about 80% reliable. I'm waiting for some 0.47 Micro Farad ones to come through the letterbox. 

Will, I tried that sketch you wrote and the stepper motor wouldn't turn either way. I need to get my head in the Programming Arduino book to understand more.

Also, I don't intend using this method on my actual CNC machine, it's only to test the mechanics of my rotary axis before I fit it to the machine (that runs off a dedicated controller and Mach3).

When I get the capacitors I'll try and attach a short video of it working with and without the capacitor.

Many thanks to all Chris

 

 

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@azslab @will @davee If you are going with the hardware approach, you can remove the software debounce.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
AZSLAB reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1131
 

And just in case you want to read a little more of debouncing you could peruse this link:

https://www.ganssle.com/debouncing.htm


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1765
 

Hi @azslab,

   Good luck with the new capacitors. As Ron (@zander) has already commented, you should be able to dispense with any software providing a debounce function, and hopefully concentrate on the main functionality.

----------

In case you are wondering about the action of the capacitor. When a switch is first mechanically closed, it is common for it to vibrate several times, like a rubber ball falling onto a hard surface, and hence the references to the word 'bounce' and 'debounce'.

Thus, when the switch contacts touch, the voltage seen by the microcontroller (almost) instantaneously falls to zero. However, without a capacitor, if the contacts then open, the voltage will also (almost) instantaneously rise back to the supply voltage (usually 5 or 3.3V depending on the microcontroller type), so that the microcontroller is likely to get several 'high's and 'low's each time the switch is mechanically closed.

With a capacitor added, the voltage will still fall to zero almost instantaneously, but will rise more slowly as the current supply to recharge the capacitor is limited by the pull-up resistor. Hence, the voltage the microcontroller observes will be held near zero for a longer period. If the capacitor is large enough, the period that the voltage is held in the 'low' range, will be longer than the total 'bouncing' time, so the microcontroller never sees any 'high's due to bounce.

The time that the voltage is held 'low' can be roughly estimated by a simple maths formula:

Time (in microseconds)  =  Capacitance (in microFarads) * Resistor (in Ohms)

e.g. C = 0.47 microFarad   R = 10 kOhms

Time = 0.47 * 10,000 = 4700 microseconds = 4.7 milliseconds

Note this is a very crude calculation, because I don't want to bore you with the full sermon, so don't be surprised if the actual value is (say) 1 or 2 milliseconds, but it is a handy trick to know, when trying to decide what value to try first.

In reality, different switch types can have very different bounce times, so some 'trial and error' is not unusual, when designing a new product. In this case, I chose 0.47 microFarad, guessing the bounce time is likely to be less than a 1 millisecond. I hope my guess was ok!!!

--------------------

I can understand if you find the programming aspects a bit confusing at present.

As a quick hint, before you worry about the coding, etc. take some time to write down exactly how you want the machine to behave. A computer programme tends to be a bit like a set of instructions for a beginner or a young child, where each action is carefully listed, and nothing can be 'assumed' to be known. In general, the clearer that description is, the easier it is to convert to code.

Best wishes, Dave


   
ReplyQuote
(@azslab)
Member
Joined: 1 month ago
Posts: 14
Topic starter  

@davee, @zander, @will, @byron

Hi everyone,

Thanks Dave for a very clear explanation. I tried the 0.47 microfarad capacitor and various other values but in the end none were anywhere near reliable so I opted for the software approach.

I managed to write a sketch that uses a debounce library and it works flawlessly. Here it is-

#include<Bounce2.h>
int reverseSwitch = 2;
int driverPUL = 7;
int driverDIR = 6;
int spd = A0;
Bounce bouncer = Bounce();

// Variables
int pd = 500;
boolean setdir=LOW;
void reverseMotor(){
  setdir=!setdir;
}
void setup(){
pinMode(driverPUL,OUTPUT);
bouncer.attach(reverseSwitch);
pinMode(driverDIR,OUTPUT);
}
void loop(){
if(bouncer.update() && bouncer.read() == LOW){
reverseMotor();
}
  pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
 

 Here is a little video of it working with some hardware.

Thankyou for everyone's help and advice

Chris

 

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@azslab Excellent, but just curious, why didn't you want a more reliable toggle switch?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2548
 

@zander 

It's wasteful calling the ISR like that, delete the void reverseMotor() code and reset the direction inside the test in loop) to ...

if(bouncer.update() && bouncer.read() == LOW) {
  setdir=!setdir;
}

Anything seems possible when you don't know what you're talking about.


   
AZSLAB reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@will It's not an ISR, just a procedure. While Will is not wrong, any compiler worth it's salt will see that the procedure is only ever referenced once and will 'inline' it giving the same result but doing as Will says, will make the code read easier.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
AZSLAB reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2548
 

@zander 

I called it an ISR because that's what it was in the original sketch.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@will I just tried changing the code, and the compiler did not inline the code. My understanding is the compiler is 'optioned' to produce the smallest executable but I thought/hoped it would also do 'in lining', but it seems not.

BTW, the difference between Will's code and the original is 8 bytes, so as Will says it is very expensive in absolute terms.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@will AH, I only looked at his last sketch, but your code applies there as well and does execute faster. The procedure call requires 8 bytes of additional code to manage the stack and branch etc.

Good catch!

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@azslab)
Member
Joined: 1 month ago
Posts: 14
Topic starter  

@zander I tried a couple of other switches and they all bounced on this experiment.

I don't actually use mechanical switches on my home made CNC mill / router. Instead I use travelling (along the axis) proximity sensors that detect a ferrous reference point. They are super accurate and don't have moving parts (and probably don't bounce much). I am not sure how to wire them to an Arduino though.

This has been a great learning curve to achieve something that may turn out to be useful on another project. 

Il be taking this experiment apart soon when I finish the rotary axis and install it on my machine.

Regards Chris

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7334
 

@azslab That sounds a lot like a Hall effect sensor/switch. Without looking, I would assume it is wired up just the same as you already have, but if in doubt, look for a Hall Effect library and check it's sample code.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@azslab)
Member
Joined: 1 month ago
Posts: 14
Topic starter  

@zander these things. They have 3 wires

IMG20240527180611

   
ReplyQuote
Page 2 / 3