Go Down

Topic: Arduino for A4988 Pololu Stepper Motor Driver code! (Read 76394 times) previous topic - next topic

murf83

I really need help. I'm trying to learn how to control a 200 step, 1.8 degree stepper motor with an A4988 Stepper Motor driver and arduino. I'm having trouble finding example codes that work at all, or when they do work and something happens, I have no idea if the motor is spinning the way it is supposed to. All of this is making the learning process very difficult.

If anyone has sample code I would really appreciate it! The more the better so I can learn to have full control on the motor and get it to do what I want for many projects I have planned. If you know of any helpful links, I would also be very grateful for those!

I don't know if it is relavent, or if it changes the code, but I am connecting everything through a RAMPS 1.4 motor control board: http://reprap.org/wiki/RAMPS_1.4

Again, I will be grateful for anything that will help me learn and understand how to code the stepper motor!

Thanks in advance.

Grumpy_Mike

There are only two lines to control. One controls the direction and the other the step. So put the direction high and pulse the other one like the LED blink example. Then repeat putting the direction low.
That is all there is to it.

murf83

Do you know how to control the motor by number of steps? all of the stepper libraries don't seem to work properly, or are not written for drivers. The rotation of the motor sputters at lower speeds also, do you know any fix for this?

Grumpy_Mike

You don't need a libary to control the motor just give it the number of pulses to move required angle. Most motors are 200 steps per revolution.
The delay between the pulses gives you the speed.
If the motor sputters you are doing it too fast, that is the delay is not long enough.

samwell

There is a tutorial with code and circuits for driving stepper motors by direction, speed, and angle over at lucidtronix. http://www.lucidtronix.com/tutorials/24

ajpope

I know this is an old post, but I came here looking for an answer and left without one.
So I decided to come back and post.

Code: [Select]


//simple A4988 connection
//jumper reset and sleep together
//connect  VDD to Arduino 3.3v or 5v
//connect  GND to Arduino GND (GND near VDD)
//connect  1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)


int stp = 13;  //connect pin 13 to step
int dir = 12;  // connect pin 12 to dir
int a = 0;     //  gen counter

void setup()
{               
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);       
}


void loop()
{
  if (a <  200)  //sweep 200 step in dir 1
   {
    a++;
    digitalWrite(stp, HIGH);   
    delay(10);               
    digitalWrite(stp, LOW); 
    delay(10);             
   }
  else
   {
    digitalWrite(dir, HIGH);
    a++;
    digitalWrite(stp, HIGH); 
    delay(10);               
    digitalWrite(stp, LOW); 
    delay(10);
   
    if (a>400)    //sweep 200 in dir 2
     {
      a = 0;
      digitalWrite(dir, LOW);
     }
    }
}

Pagemaker

An old topic, most certainly!
But still very helpful for the absolute beginner.
I'm still wondering in what order the coils of the motor are powered.
Or is that all done by the A4988?

Pagemaker

Found an answer!

"This product is a breakout board for Allegro's A4988 DMOS Microstepping Driver with Translator..........
The translator is the key to the easy implementation of the A4988.
Simply inputting one pulse on the STEP input, drives the motor one micro-step.
There are no phase sequence tables, high frequency control lines, or complex interfaces to program.
The A4988 interface is an ideal fit for applications where a complex microprocessor is unavailable or is overburdened."

That was rather hard to find.
But persistency and careful reading did the trick.

Grumpy_Mike

Quote
That was rather hard to find.
But persistency and careful reading did the trick.

Yes you had to read replies #1 & #3, bet it took a long time to find that.

polymorph

Seriously, learn how to do the same thing with "Blink Without Delay" and you'll be much happier in the future. Much more scalable.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - tinyurl.com/q7uqnvn
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

alfadex

Hi , i have made this but i want also to add a simple lcd display like hd44780, how easy is it

Robin2

Hi , i have made this but i want also to add a simple lcd display like hd44780, how easy is it
I have suggested to the Moderator to move your question to its own Thread.

Then you can tell us what you mean by "this" and you can post your existing program code.

Also tell us what you want the display to show.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

alfadex

#12
Mar 23, 2017, 08:55 pm Last Edit: Mar 23, 2017, 09:41 pm by alfadex
Hi, i see it is not moved so i will post it here.
My code is below.
From a potensiometer i am adjusting speed of a nema 17 motor with a a4988 driver.

But now i want to add an lcd display hd44780 just dispay rpm of the motor, but i don't know what should i change to the code







Code: [Select]



[code]// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 
  digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
  
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;  
}




[/code]

Robin2

I am not familiar with those displays so I will leave that to someone else.
Your code
Code: [Select]
digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);

treats the step pulse and the interval between pulses as the same. I think you will find it easier if you make a short step pulse and then just change the interval between steps to alter the speed. have a look at this Simple Stepper Code

...R
Stepper Motor Basics
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy