@davee TLDR, but I didn't say that, it's from the Arduino manual. If it's wrong, report it as a bug. INPUT_PULLUP has always worked for me. For a limit switch, bounce is unimportant, any switch closure is a signal to stop. OR use the NC contact and detect it opening, that should eliminate bounce. Wire the C to 5V via a resistor and look for 0 on the digital read, just remember to invert the logic.
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.
Hi Ron @zander,
Regarding external resistor: I am not sure not sure which part of the Arduino manual you are referring to. I can see
https://docs.arduino.cc/tutorials/generic/digital-input-pullup
which does indeed demonstrate INPUT_PULLUP, with very short wires in a setup that has no other electrical activity ... and I agree with you it will work.
Of course, this page is about demonstrating what INPUT_PULLUP does, so an external resistor would not be appropriate.
This page does not discuss the electrical implications of this arrangement, which arguably would have been helpful, but I think the authors may have felt this would over complicate the explanation.
And I hope you appreciate that I say in my message that it is "it is possible to cut corners and just use INPUT_PULLUP, and you may get away with it"
-----------------
When the wires are longer than a few centimetres, and/or it is used in an environment with electrical noise, due to equipment being switched on and off, and so on, the probabilty of 'weird' failures can become very high.
The internal pullup resistors are intended when all of the wiring to that pin is confined to the PCB is soldered to: That is when there is nothing connected to the pin, and it prevents stray pickup voltages entering the chip, or when on the same PCB as the processor chip, there are configuration jumpers or switches, which connect to ground when jumper is present or switch is closed. In this case, a competent PCB designer would ensure that the switches/jumpers are positioned close to the processor chip, with short tracks, over a ground plane, so the chance of significant electrical pickup is very small.
--------------
Regarding capacitor: As for the capacitor, as you appreciate this is to handle contact bounce .. perhaps there are occasions that it can be ignored, but in the many of cases it is useful, and as it is a very small, cheap part, unless you are looking at a 10,000 or more production run, the cost and effort of proving you don't need it probably exceeds the cost of fitting it in the first place.
In some cases it will depend on the software .. a piece of code that polls until a change of state may naturally ignore subsquent bounces, whilst another software designer may use an interrupt function to do the same function and get multiple calls.
Hence it is quite possible to have a system working, until a small software modification is made which slightly changes the timing, and it stops working. In a commercial concern, with separate software and hardware teams, this is a great way of ending up in a blame game battle, as it may not be clear what has happened. For those of who do both software and hardware, it increases our frustration and head scratching.
--------------------
In summary, I appreciate you and many others have put systems together, relied on INPUT_PULLUP, and not used a capacitor to remove bounce, and it has worked fine. Hopefully, you were not building a system which was safety critical or with expensive consequences if it failed to work properly on any occasion. As the price of a resistor and capacitor is only a penny/cent or two, I just fit them.
Best wishes, Dave
@will Alas, no joy this morning. When I run your latest sketch ( the one you sent while it was about 2 am in England) the motor does nothing, whatever I do with the switch. If I press the reset button on the Arduino while the motor power is on , the motor jitters, producing a horrid growling noise. I have checked the connections and they are all as they should be. Older sketches work fine, so there is nothing fundamentally wrong with the motor wiring.
Very strange, I adapted that from a working project of mine. I can try to a) dig out a similar driver and test with a NEMA17 (which may take some time to find) or b) remove the library and bit-bang the driver like the sketch you were using (that worked). I can start on the latter now since I don't need to find and assemble pars.
Your choice, just let me know.
Anything seems possible when you don't know what you're talking about.
@davee The 'part' of the manual is the part I copied and pasted into a "" block, and also provided a link to the page with the blue text Manual. Here is the raw link https://docs.arduino.cc/learn/microcontrollers/digital-pins
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.
@davee Were you a lawyer in a previous life, with maybe a minor in engineering or the other way around?
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.
Hi Ron @zander,
I may have been butterfly in a previous life, but have no knowledge of that.. more likely hydrogen and star dust🤔
In this life, I have been (amongst other things) a principal research engineer/team leader of a large international company producing safety critical equipment.
-------
I actually don't see any conflict between https://docs.arduino.cc/learn/microcontrollers/digital-pins and what I am saying.
I have agreed that INPUT_PULLUP can be used in specific circumstances ... essentially those in which the scope of electrical interference pickup is very small. More generally, additional limitations apply, but not for this application.
However, not knowing the physical layout of the final product, being aimed for use in a laboratory environment, and assuming the end equipment will be used by a considerable number of people who just want reliable operation, I think it is wise to design to a good standard. From this, I conclude that I cannot assume the risk of electrical interference will be small.
I would suggest the Arduino article you refer actually supports my description of the situation, albeit without explaining why.
It includes:
Pullup Resistors with pins configured as INPUT
Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input. A 10K resistor is a good value for a pullup or pulldown resistor.
and
Properties of Pins Configured as INPUT_PULLUP
There are 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed by setting the pinMode() as INPUT_PULLUP. This effectively inverts the behavior of the INPUT mode, where HIGH means the sensor is off, and LOW means the sensor is on.
The value of this pullup depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ. For the exact value, consult the datasheet of the microcontroller on your board.
Comparing these two pararaphs, you will notice that when adding an external value, 10kOhm is suggested, whilst explaining the internal resistors are somewhat varied from 20KOhm upwards, possibly to 150kOhm, depending on the board.
It is left to the reader to notice the difference, and understand the implications.
The missing link, is that they do not explain why they suggest 10kOhm, whilst the internal value will be at least 20 kOhm, and might be 50kOhm or more.
------------
The reason I have already provided ... the internal pullup is provided to enable board manufacturers to have protected unused pins, and also to have (local) board configuration pins/jumpers, without having to fit resistors. The resistance is deliberately kept as high as possible for these applications to minimise the continuous current drain when grounded of (say) 50 microAmps per pin.
They are not provided for many other reasons, including pullups to external switches or for open collector/drain pullup applications, as in most of these cases, the pullup function is too weak for reliable operation.
Of course, there are no sharp boundaries between these two cases; it is left to the experienced designer to choose appropriately.
-----
Best wishes my friend, Dave
Hi @will,
Just a totally wild guess based on the 'growling' symptom ... Does your new code step at a quicker rate than the older program that apparently worked?
If not, please forgive the intrusion.
Best wishes, Dave
I can't tell for sure because it's not obvious how the AccelStepper class converts its speed argument into delay between pulses.
I read somewhere long ago that the number sent is interpreted as the number of steps per second. Using that as Gospel, the 4000 uSeconds delay used in the original sketch would be about 125 steps/second versus my starting speed of 1500.
I did state that he'd have to fiddle with the numbers because of the differences in type and loading but I never explained how to do that ... my bad.
It may be worth experimenting by turning off acceleration (for now) setting speed and max speed equal and starting both from 50, Increase by. say, 25 each test until a satisfactory rate is established. By satisfactory, I mean that it's not too slow but not so fast that it's hard to stop.
I'm currently working on an alternate version which doesn't use the library, on the theory that it eliminates any possibility that the library is at fault. This operation appears to be simple enough that AccelStepper isn't adding much value (although the additional benefit of having acceleration and deceleration for the movement would be huge).
Anything seems possible when you don't know what you're talking about.
@davee The issue I had with your comment was you attributed it to me instead of the Arduino manual. Also I think in the original message you didn't notice the link. In any case, I am sure your theories are correct, and if I ever encounter a problem, you will be the first person I will ask for advice from.
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.
Ron @zander ,
Of course, I apologise for any mistakes on my part ... I hope it did not cause you any distress.
Take care my friend and best wishes, Dave
I really do appreciate the time you are all spending on helping me! Before Will goes to all the trouble of reproducing
my project with a Nema motor, I thought I should send you a wiring diagram of my setup, in case I am doing something stupid. Even though some of the wires are in a breadboard, I have checked them and they all look firm. The only thing I need to check tomorrow is whether it makes a difference to wire the digital and motor grounds together. I can also try using a different PC.....
Brad
Here's a new version that ...
- eliminates the AccelStepper class
- implements a button to move the carriage to the focus engaged position
- implements a button to move the carriage to a focus disengaged position
- homes the stepper in the setup() routine
- listens for and executes movement for the two new buttons
- includes a SimpleButton class to handle all three buttons (limit, engaged and disengaged) with debouncing built into the button class
This will require several wiring changes on your part ...
1) you'll need to change the wiring on the limit switch to: Arduino pin 3 to limit switch common, limit switch NO to Arduino GND. Lose the resistor you're currently using, the class will assign a pull resistor inside the Arduino by issuing a pinMode(pin,INPUT_PULLUP).
2) add a momentary pushbutton as follows Arduino pin 5 to button, button to Arduino GND (this button when pressed will cause the carriage to go to the 'focus engaged' position)
3) add a momentary pushbutton as follows Arduino pin 6 to button, button to Arduino GND (this button when pressed will cause the carriage to go to the 'focus disengaged' position)
There are a bunch of notes at the head of the sketch. They involve the setup and some important considerations so please read them before use.
WARNING - THIS IS ALL NEWLY WRITTEN CODE AND IT WILL MOST LIKELY FAIL ON FIRST RUN. YOU MAY BE EXCUSED FOR NOT WANTING TO TRY IT 🙂
Anything seems possible when you don't know what you're talking about.
The only thing I need to check tomorrow is whether it makes a difference to wire the digital and motor grounds together. I can also try using a different PC.....
IIRC the motor ground and controller ground are commoned in the A4988, so that should NOT be a problem. But, it doesn't hurt to add a connection.
Anything seems possible when you don't know what you're talking about.
@davee Nah, I know you are honorable, just an honest mistake.
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.
Hi @will,
I understand if you are not interested, having been hard at work on some new code, and I may have this wrong, in which case apologies in advance!
I have finally got my old mega2560 clone, Arduino 2.1-ish, and Accelstepper library together + a scope to if there any pulses that could drive an A4988 (if I had one). I have never seen this library before, so I have been grovelling around at the bottom of a learning curve and may easily be mistaken.
-----------------------
My initial conclusion is there are no step pulses...
-------------------------
The first suspect of a code issue is a combination :
stepper.moveTo(-10000)
and
while (stepper.distanceToGo() >0 ) { stepper.run () }
--------
My interpretation is whilst you and I know -10000 steps is 10000 steps from where you want to be, the >0 is taken literally, and it goes nowhere.
----------
That does rather beg the question, why is the stepper motor growling? ... the 'step' output is permanently at 0V, with the code you supplied, so I would expect it to just sit quietly.
Maybe your code was not the only issue, but that is total speculation.
Best wishes,
Dave