Notifications
Clear all

Large Stepper Code does not work

Page 4 / 8

Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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
(@davee)
Reputable Member
Joined: 12 months ago
Posts: 355
 

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 liked
ReplyQuote
Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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
(@davee)
Reputable Member
Joined: 12 months ago
Posts: 355
 

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)
Noble Member
Joined: 11 months ago
Posts: 1764
 
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.

I was kidnapped by mimes.
They did unspeakable things to me.


DaveE liked
ReplyQuote
Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
Topic starter  

Yes DaveE, the motor runs!

 

Ralph


ReplyQuote
DaveE
(@davee)
Reputable Member
Joined: 12 months ago
Posts: 355
 

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
(@davee)
Reputable Member
Joined: 12 months ago
Posts: 355
 

@ralphxyz Hi Ralph,

   I have prepared the next step, which is software based, assuming the wiring is as above.

The principle is to try to move towards a very simple program using the switch and the potentiometer, and base it on what you have reported as working above.

I have added a new listing below. (Sorry if the formatting isn't as good as it should be ... as I write it, it is fine in the editing window, but monochrome in the preview.)   {and yes, I did have a bit of read of the forum instructions}

This version:

  • has the same functionality as your test above, with the addition that pressing and holding the switch should cause it to move in the opposite direction ... releasing the switch should cause it to revert to the original direction.
    • (Sorry, it is not possible to predict if the default is clockwise or counter clockwise.)
    • (It ignores the potentiometer -- that is for a later instalment!)
    • the default speed should be as before .. approx 1000 pulses/microsteps per second. It is set by the line "#define halfStepUS 500", which can be altered to show it is controlling the speed

 

  • I have pushed most of the detail down into tiny functions - initially this may look more complicated, but as each function concentrates on one thing, it is simple in itself - and the 'main program' bit becomes more of a management job

I'll describe the code, starting near the bottom of the listing and moving upwards:

  • The 'Main program, in the lower part, consists of the usual setup() and loop() functions
    • setup has the additional 'Serial.begin(115200);' to enable the Serial Monitor to be used. I assume you know how that works, but if not, let me know. Note I have set speed to 115200 Baud. You may have to adjust the Serial Monitor when it opens to that speed on the first time.
    • I have moved the dirPin setting into loop ()
    •  
    • loop() consists of two simple parts
      • (1) Read the level from the switch and use that data to set the level on the pin connected to DIR- on your motor controller
      • (2) A 'for' loop to create 1000 step pulses ... which with the default pulse width, should take approximately 1 second
      • Summarising the switch is read, direction sent to controller, then 1000 pulses, sent to controller.....and repeat and nauseum. Thus the switch must be held down to continue to move in reverse direction.

 

  • The 'Worker functions' are
    • do_a_step  ... send one pulse to the motor controller. (I have put a commented out print statement into this routine ... hopefully you don't need it !
    • getDir ... read the switch level, which it returns, plus a print statement of the level
    • setDir ... set the level passed as parameter, to control the direction

 

  • #defines at the start of the code consist of two to connect to motor controller, unchanged from before, a new buttonPin = 2, assuming the switch is connected to pin 2 as described, and a half pulse time in microseconds, to make it easier to change the default step rate.

 I hope this is all clear. Please ask any questions.

Please try it as a new sketch, keeping your earlier sketch as a backup to check the hardware.

And let me know how it goes on.

Assuming it appears to work as expected, please try temporarily changing the value in "#define halfStepUS 500" and checking it affects the speed as you would expect ... e.g. 1000 would halve the speed.

Good luck, Dave

 

 

/* Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino
without a library: continuous rotation. More info:   https://www.makerguides.com   */
// Ver 2.0

// Define stepper motor connections:
#define dirPin 6
#define stepPin 7

// define user control connections - buttonPin is the push button to command change direction
#define buttonPin 2

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

// +++++++++++++++++  functions +++++++++++++++++++++++++++
#define halfStepUS  500
//   getDir --- read the pushbutton value into a variable

uint8_t getDir (){
  uint8_t sensorVal = digitalRead(buttonPin);
  //print out the value of the pushbutton
  Serial.print ("Button value = ");  Serial.println (sensorVal);
  return sensorVal;
}

// ----------------------------------------------------------

// --- do_a_step --- produce 1 pulse on stepPin of length 2 * halfStepTime

void do_a_step (int halfStepTime)
{
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(halfStepTime);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(halfStepTime);

 /*   Serial.print("Step\n");   // only enable if step fails and needs debugging
    delay(100);*/
}
//---------------------------------------------------------------

//  setDir - set direction to dir ... HIGH or LOW

void setDir(uint8_t dir)
{
  digitalWrite(dirPin, dir);
  Serial.print("Setting direction ");   Serial.println(dir);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++ Main program ++++++++++++++++++++++++++++++++++++++

void setup() {
  // set up serial monitor for debug
  Serial.begin(115200);
  
  // set up output pins
  pinMode (stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {

  // read the switch level and use the result to set the DIR pin level
  uint8_t dirNow = getDir();
  setDir(dirNow);
  
  // do a 1000 pulses
  for (int i=0; i< 1000; i++) {
     do_a_step (halfStepUS);
  }
}

 


ReplyQuote
Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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)
Noble Member
Joined: 11 months ago
Posts: 1764
 
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.

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
DaveE
(@davee)
Reputable Member
Joined: 12 months ago
Posts: 355
 

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
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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 4 months ago by Ralphxyz

ReplyQuote
Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
Topic starter  

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

I am checking my wiring.

Ralph


ReplyQuote
Ralphxyz
(@ralphxyz)
Trusted Member
Joined: 2 years ago
Posts: 62
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