Notifications
Clear all

Large Stepper Code does not work

119 Posts
5 Users
10 Likes
7,833 Views
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

It uses the Arduino bootloader. My UNO has failed to compile different projects so I had not even tried to use it, I'll dig out another  I am sure I have. 

Only pins 2 + 3 on a UNO are interrupt enabled.

On the Mega (328 processor) 2,3,18,19,20 and 21 are interrupt enabled.

I "should" be able change pin 5 to 2 or 3 and change the pin 2 or 3 reference to another pin.

 


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

Hi @ralphxyz & @Will

  The photo is not completely clear, but looks like the straight 'Metro' ... which according to Adafruit 

https://www.adafruit.com/product/2488#description

The METRO works great with the Arduino IDE, and runs the ATmega328P at 16MHz so it is shape and pin-compatible with Arduino UNO R3 shields and boards. You can use this with the Arduino IDE (both desktop and cloud version) by selecting 'Arduino UNO' in the Boards menu.

.. suggesting it should play like a UNO ....!

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

Ralph says

Only pins 2 + 3 on a UNO are interrupt enabled.

On the Mega (328 processor) 2,3,18,19,20 and 21 are interrupt enabled.

The Mega is based on the ATmega2560 ... I am confused...

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

Unfortunately I can't see all of the wiring to get a complete circuit, in case that revealed anything.

Ralph says "My UNO has failed to compile different projects " ... The Arduino boards do not do any compiling ... the compiling of Arduino C/C++ code is done on the PC/MAC host, before the Arduino board needs to be connected. So any problems "compiling" would be associated with the code, libraries, etc. on the PC/MAC.

Or perhaps you had mean troubles downloaded your executable program ... or actually running your program on the UNO?

--------

I am sorry if some of this sounds like nitpicking, but in my experience, when it comes to nitpicking, computers are world class experts who don't kindly to anyone who doesn't share their passion!

Best wishes and good luck to you both.

Dave

 


   
Inst-Tech reacted
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

Sorry of course the Mega is the 2560 processor.

I have been "trying" to get the DIR button to work.

This whole trying to get a stepper change direction is really

starting to stress me.

I looks so simple yet things just don't work.

Would the DIR work with just wiring to -/+ with out the micro?

I suppose I should have said what I was trying to do.

Well:

          1: Understand and get to work a stepper motor speed control and direction.

          2: Add a stepper motor to my lathe for Z movement.

          3: Add a stepper motor to my lathe for X movement.

The big stepper project is exactly what I need but I cannot get the code to run.

I can get the motor to run, slowly, if I use a Common ground which has been kindly/explicitly illustrated to not do, Thank You! But neither the speed or direction work.

Will's code works for running the motor but there is no speed control or direction control.

Ralph

 


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

Hi @ralphxyz,

  I hope the stress isn't getting too bad... getting a new something (software, hardware, whatever) to work first time always (for me anyway) seems really hard .. I think it is because everything is 'new' and you don't know all the little 'gotchas' to look out for, and it is easy to create a new problem whilst fixing another. In this case, we have both 'unknown' hardware and 'unknown' software to contend with, so I am going to suggest we do one tiny step at a time.

So please, just try to 'de-stress' and start carefully & methodically.

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

Of course, you may wish to ignore me, but I am going to recommend we start using Bill's wiring as a 'gold standard', and make it work, even though this may mean more changes on your part.

I think it would be good to get the hardware/hardware interface sorted and working, with the barest minimum of software ... then introduce the software, knowing the hardware is playing nicely.

First, lets try to get the motor running at a 'predictable' rate. I hope @Will doesn't mind, but I suggest we temporarily park his program and return to the simple program you had working at the bottom of page 1 of this thread. (If we have the hardware sorted, Will's programming can return to centre stage.)

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

1/ Use an Arduino Uno or 'clone', such as the Adafruit Metro   .. to avoid pin and processor confusion

2/ Wire it exactly as shown in Bill's diagram that you included on page 1 of this forum. This implies :

  • Arduino +5V pin goes to:
    • Motor controller Dir+ and Pul+ pins
    • top end of Potentiometer track (I suspect you have 100k, 5k-10k might be better, but not important to get started with)
    • top end of 10k resistor            (value not too critical, but hopefully close)
  • Arduino Gnd pin goes to:
    • bottom end of Potentiometer track
    • Top contact of pushbutton switch
  • Arduino A0 anologue in goes to:
    • Potentiometer wiper
  • Arduino Pin 7 goes to:
    • Motor controller Pul- pin
  • Arduino Pin 6 goes to:
    • Motor controller Dir- pin
  • Arduino Pin 2 goes to:
    • Bottom contact of pushbutton switch + bottom end of 10k resistor

 

  • Motor controller to motor ... 4 wires ... A+controller to A+motor, A-ctrl to A-motor, B+ctrl to B+motor, B-ctrl to B-motor

 

  • +12V and "Motor Gnd" ... two wires ... from "motor power supply unit" to +12V and "Motor Gnd" pin on motor controller

 

  • Power to Arduino (+5V and Gnd) through USB plug from PC/MAC

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

Please note

  1. no connection from "Motor Gnd" to other (Arduino) "Gnds"
  2. This is like the Common Anode wiring pattern in the Maker guide .. you previously showed the common cathode

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

Software:

  1. Use your program that you showed on the bottom of Page 1 of this forum thread, after changing the two pin assignments to match this wiring ... the rest of the program should be fine
    • #define dirPin 6     and   #define stepPin  7
  2. Does motor step at the rate you expect ... probably 1 microstep per millisecond?
  3. Does changing the delayMicroseconds parameter change the step rate as you expect?

-----------

If you can get this far ... or get stuck/anxious/spot an error on my part..., then report back on what you have found.

Sorry this is long-winded, etc. But I think we need to get a sure footing before moving on.

Best wishes and good luck. Dave


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 
Posted by: @davee

First, lets try to get the motor running at a 'predictable' rate. I hope @Will doesn't mind, but I suggest we temporarily park his program

I don't mind at all, he hasn't answered my questions and still hasn't posted the results of changing the reverse pin to one which is a valid interrupt pin on whatever hardware he's using.

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


   
DaveE reacted
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

Sorry Will, I have just been trying to get the DIR button to work,

I think for the past couple of days I had forgotten to change the pin DIR. I made your

code Read Only so my code changes were not saved.

 

I only once or twice a year attempt to do an Arduino project so I get very rusty in my procedures.

 

Ralph

 

 


   
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

Yes DaveE, the motor runs!

 

Ralph


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

Hi @ralphxyz,

  That sounds good news! Sorry I haven't had time to reply today .. I want to check a couple of things first .. hopefully tomorrow for the next step.

Dave


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

   
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

DaveE, your code actually compiled and loaded the first try.

But no response to the button.

Setting direction 1
Button value = 1
Setting direction 1
Button value = 1
Setting direction 1
Button value = 1
Setting direction 1
Button value = 1
Setting direction 1
Button value = 1
Setting direction 1
Button value = 1

This is with the button pressed and being held.

The speed changes:     

              // define default half step = 1 pulse time in microseconds
             #define halfStepUS 20

             // +++++++++++++++++ functions                            +++++++++++++++++++++++++++
            #define halfStepUS 20

 

Not sure why there is 2 defines.

I am going to try a different driver.

I was running a program that stepped the motor and reversed it, from Makerguide.

That program stopped reversing the motor, possible after I connected the common ground.

Possible there is something wrong with the stepper driver.

Of course the Button value should have changed regardless the driver function.

Ralph

 

 

 

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 
Posted by: @davee

@ralphxyz

You may need to add ( into setup() )

pinMode(buttonPin,INPUT);

Also, I'm not sure it's clear that you need to let the sketch run for a couple of cycles with the reverse button UP and then press the button DOWN and let it run another couple of cycles.

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


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

H @ralphxyz and @Will

  Thanks for your feedback and suggestions.

  Sorry about the duplication of the #define .. please delete the second one ... I think a move accidentally turned into a copy.

  I don't have a UNO or a motor controller ... but I have 'tested' the code with an ATMega 2560 card, and observed the DIR-and PUL- outputs on an oscilloscope, so whilst there may be an unexpected difference between UNO and Mega, I am inclined to look elsewhere first.

Using the setup that I have recommended, can you please connect a voltmeter between the Arduino Pin 2 and an Arduino Ground Pin. (NB note that 'Pin 2' is the 3rd pin in the row of pins.) (Please try to probe the actual Arduino board connections, not a convenient wire, etc that you think is connected to it.)

Measure the voltage whilst the switch is operated ... it should be 5V when the switch is released and 0V when it is pressed.

Please let me know what you find.

Thanks. Best wishes, Dave

 

 


   
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

Yes I am letting it run.

Nothing is happen with the button press.

This code works, the motor reverses.

It is from the same Makerguide Tutorial.

 

/* Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino without a library: number of revolutions,
speed and direction. More info: https://www.makerguides.com
https://www.makerguides.com/tb6600-stepper-motor-driver-arduino-tutorial/
*/

// Define stepper motor connections and steps per revolution:
//#define dirPin 2
//#define stepPin 3

#define dirPin 6
#define stepPin 7

#define stepsPerRevolution 1600

void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}

void loop() {
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);

// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}

delay(1000);

// Set the spinning direction counterclockwise:
digitalWrite(dirPin, LOW);

// Spin the stepper motor 1 revolution quickly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}

delay(1000);

// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);

// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}

delay(1000);

// Set the spinning direction counterclockwise:
digitalWrite(dirPin, LOW);

// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}

delay(1000);
}

 

Ralph

This post was modified 2 years ago by Ralphxyz

   
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

I am getting 0 volt with the switch not pressed or pressed.

I am checking my wiring.

Ralph


   
ReplyQuote
(@ralphxyz)
Member
Joined: 4 years ago
Posts: 61
Topic starter  

Who knew, one has to have the wiring correct in order to see the correct results.

TaDah, it is working!!

Ralph


   
ReplyQuote
Page 4 / 8