writing code for stepper motor

Hello Everyone,

This is my first post on this forum so bear with me. I have an arduino uno a pololu a4988 and a 42HS02 bipolar stepper motor. I have never written code before and I am finding myself quite frustrated with the attempt to figure it out. I am much more of a hands on visual learner than reader.

I would like to be able to control the direction of the stepper with two momentary switches. A forward and reverse switch or clockwise and counter clockwise. The motor should stop when the switch is released.

I greatly appreciate any advice you folks can provide. If you need any additional information please let me know.

Thank you

John

Post a link to the datasheet for the motor.
What motor power supply are you using (volts and amps).

Try this Simple Stepper Code to get the motor moving. Use the wiring diagram on the Pololu A4988 web page.

You may find some useful stuff in Stepper Motor Basics

...R

Hi,
google

pololu a4988 arduino

All sorts of resources that have what you need.

Also check out some of the basic examples in the IDE to get the feel of how C++ and arduino works.

What is your electronics, programming, arduino, hardware experience?

Tom.... :slight_smile:

Hello this message will be a reply to Tom and Robin.

I am using a 12v 2a power supply
Stepper
Phase current 0.4a
Phase resistance 12.5
Phase inductance 21mh

I have minimal experience with arduino and programming. I have done lots of things with electronics but only on a simple level. The wiring aspect is not a challenge for me it is the code that is kicking my butt.

jlavapors:
it is the code that is kicking my butt.

I have given you a link for that.

...R

Hey everyone so I am still having issues figuring out how to get this working. I used Robin2 new version of the simple stepper code. I do not know what I did wrong but I would just get chatter from the stepper motor. I then had an issue and the arduino fried. I have a spare but I do not want to have the same mistake twice. I hope you guys can help me navigate through this. I apologize I do not understand any of this. I will include a description of the wiring. I know some of this is redundant as most of you will know but I may be an idiot and have connected one of the wires incorrectly.

Arduino

power
5v-positive
gnd-negative

Digital PWM
8-step
9-direction
10-CW Switch
11-CCW Switch
GND-to both switches

A4988
Vmot- + 12v 2A power supply
GND- power supply ground
2B- coil 1 +
2A- coil 1 -
1A- coil 2 +
1B- coil 2 -
VDD- 5V arduino power
GND- GND arduino power
Sleep/Reset- jumper wire connecting together
Step- 8pin PWM
Direction- 9pin PWM

jlavapors:
I used Robin2 new version of the simple stepper code. I do not know what I did wrong but I would just get chatter from the stepper motor.

Post a copy of the code from YOUR program.

Make a pencil drawing showing all the connections and post a photo of the drawing. Verbal (oral or written) descriptions of wiring are too easy to mis-understand).

...R

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds

void setup() {

Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");

pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);

pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);

}

void loop() {

curMillis = millis();
readButtons();
actOnButtons();

}

void readButtons() {

buttonCCWpressed = false;
buttonCWpressed = false;

if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
}

void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
singleStep();
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
singleStep();
}
}

void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}

That does not look like a pencil drawing to me. However, for a Fritzing diagram it is reasonably clear. I asked for a pencil diagram because they are easier to follow than Fritzing diagrams.

In the code you posted the step and direction pins are 8 and 9. In your diagram they seem to be 3 and 4.

The code you posted requires pushbuttons. Your diagram has no push buttons.

Over to you ...

...R

I am using some momentary switches from radio shack for the cw(pin10) and ccw(pin11). the original image was a template I used.

jlavapors:
I am using some momentary switches from radio shack for the cw(pin10) and ccw(pin11). the original image was a template I used.

Go back to Reply #6 and read the last paragraph again. I wrote it like that for a purpose.

I am not going to waste time trying to guess what your problem is without all the necessary data. It should not have been necessary for me to write this Reply or Reply #8

...R

I am doing my best to understand this and explain everything that is needed for your assistance. I am happy to paypal a monetary reimbursement for your assistance. You definitely have a set of knowledge that I do not. I revised the image to best explain everything. I did not do a pencil drawing for fear that it would be far worse than the "Fritzing diagram" I modified. One other thing the arduino I am using is not a mega it is a UNO.

Hi,

Just to help us.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a picture of your project?

The picture is so we can see your layout and components used.

Thanks......Tom ..... :slight_smile:

Hello Tom,

I will post pictures and code tags Sunday when I get back to the shop.

jlavapors:
I did not do a pencil drawing for fear that it would be far worse than the "Fritzing diagram" I modified.

I don't mind if the pencil drawing is poor as long as it is legible. This is not an art competition. The value of a pencil drawing is that I can be reasonably confident that it will reflect what you actually have rather than something that is conveniently on the web.

The point of the diagram is not to give a general idea but to give a very precise and unambiguous description.

For example, real switches are not wired the way they are in your fritzing image. They each have at least 2 terminals. And I can't read the writing on the A4988 in your diagram to check which is the step pin.

Are you using the little square push button switches with 4 terminals? It is very easy to put them in the wrong way round. Check how the contacts work with your multimeter. You can make an effective test switch just by sticking two pieces of wire into the breadboard and touching the two ends together.

...R

Hi,
Sometimes drawing a circuit diagram, means you do some reverse engineering and it from the existing project.
In doing so you usually find any wiring defects or mistakes that can easily be made.

Tom... :slight_smile:

The switches are 2 pole type. Now that I have drawn out the pencil sketch I can appreciate the importance much more. Hopefully there is an obvious issue that is an easy fix so I can get this thing up and running.

Okay I have a new development. I have identified one problem the wiring loom that came with the stepper motor was incorrect. I switched the leads and I now have motion. This does present a new issue though the motor rotates in one direction without any commands. When I hold down the switch on pin 10 the motor will switch direction and stop if I hold both switches.

the only thing that has changed on the wiring is the wires going from the A4988 to the stepper motor.

jlavapors:
the only thing that has changed on the wiring is the wires going from the A4988 to the stepper motor.

You need to post a new diagram. It is much too easy to misunderstand descriptions.

Have you used your multimeter to measure the resistances of the stepper motor coils? That is the best way to identify the wires that belong to each coil.

...R

I will draw one up as soon as I get to the shop. In the diagram is there any sign that wireing between arduino and a4988 are incorrect? Am I missing any resistors for the pull-up and are both switches supposed to be pull-up?