Arduino uno R3 stepper motor control

Hi JimboZA,

Thanks for correcting my spelling of Robin2.

With respect to the rest of your challenge, I'm not yet up to the stage of writing code, just beginning to get the gist of sketches via tutorials.

I have on order an Easy Driver board, hasn't arrived as yet. Have found a tutorial where speed and direction of a stepper motor is addressed plus wiring diagram. Have the Arduino uno R3 board and have been having a play.

Amazing piece of hardware.

Ken

neksmerj: Have found a tutorial where speed and direction of a stepper motor is addressed plus wiring diagram.

Please post a link to it so I can learn the deficiencies in mine.

...R

Hi Robin2,

I wouldn't say there are any deficiencies in your thread, it's all at this end.

I found this tutorial re Arduino and EasyDriver employing 3 push buttons for forward, stop and reverse plus potentiometer for speed on the fly. Included also is a schematic diagram.

http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html

Ken

neksmerj: With respect to the rest of your challenge, I'm not yet up to the stage of writing code, just beginning to get the gist of sketches via tutorials.

Well you have to start sometime! And if you have the Uno and a couple of led/resistor pairs, you can sort of mimic the direction thing. Read a switch (which if you don't have one can simply be a wire from an i/o pin which is either jabbed into a GND hole or not), and if the result is HIGH switch on led #1 and if it's LOW switch on a different one.

Or, if you like, draw a flow chart of the logic of what you want to do and code it in pseudo-code. Pseudo code is just words that you make up, which should reflect what you want to do, but needn't be correct in terms of the programming language's rules. So you could say something like:

read switchPin
if switchPin is high-
    set led1Pin high
    set led2Pin low.
otherwise-
    set led1Pin low
    set led2Pin high.

Hi JimboZA,

Your words make it sound so simple. It's a pity that the code for the Arduino is not written in english or basic.

How about some code to reflect your english steps?

Ken

Give it a bash yourself.

If you don't feel confident running it in case you pop something, at least type it up and see if it compiles. Compile only, with no upload, poses no danger the Uno. That way you'll know it's grammatically correct even if it falls over on the logic. Then post that and let someone here comment.

But here's some clues with no code, since I really think you'll benefit from trying to write it yourself.

Start with the BareMinimum sketch which gives you a skeleton setup() and loop(). Above setup() define any variables you would like to name. You might for instance decide to have one LED on pin 5 so you could use byte to set say myLedPin1 to be 5. Same for another led on a different pin, and ditto the switch.

Then in setup() use pinMode to set those leds' pins as outputs and the switch one as an input with pullup resistors.

In loop, use digitalRead to read the switch and digitalWrite inside an if / else to conditionally turn on the LEDs.

Time to get your feet wet, methinks: but as I say if you want comment before you hook stuff up, that's cool. At least get to the stage of sorting out compile errors like if you type "PinMode" when it should be "pinMode", for instance.

ps.... you would also want to declare a variable to hold the result of your digitalRead, and use that value (say mySwitchVal) in the test of the if/else.

I am writing a short sketch to do this as we speak, but I'm reluctant to post it until you go first 8)

neksmerj: I found this tutorial re Arduino and EasyDriver employing 3 push buttons for forward, stop and reverse plus potentiometer for speed on the fly. Included also is a schematic diagram.

I'm delighted you have found something that works for you.

But, to be honest, I don't understand how the link you gave is any different from the combination of my code and the diagram on the Pololu A4988 page.

And I guess my frustration has been my inability to find the key that would help me to bridge whatever gap exists between what I write and your understanding of what I write. I suppose I expect people who don't understand to respond with lots of questions that illustrate that gap - and you will have noticed from earlier posts that for a long time I was not even sure if you had read my stuff.

...R

Hi Robin2,

I need to be hand fed at this stage until I'm up to speed. The tutorial I linked to shows the code and a pictorial diagram that I can relate to.

http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html

Your link to the Polou A4988 shows a schematic only, no code.

I wouldn't know how to hook your board up to an Arduino, and how to control direction and speed.

I haven't reached that level of knowledge yet. I'm ploughing through as many tutorials as I can find, it seems to be the only way to learn.

I vividly remember the two days years ago when I let go of the saddle on my kids’ bikes and all of a sudden they could ride a two-wheeler with no fairy wheels. But I was still jogging behind just for in case…

I’m pretty sure you remember those days too neksmerj, yet you seem reluctant to let go of your own saddle so to speak.

My advice: try to write the code as I suggested a few posts ago, to get (and this is merely a suggestion) one or other led on when a switch is pressed or not. Post it here by all means for comment, but the time has come where you have to do that.

I’m ploughing through as many tutorials as I can find, it seems to be the only way to learn.

There’s only one way to find out what you have learned, and that’s to try a sketch. Right now, you’re like Sheldon in TBBT when he said he learned to swim on the floor of the apartment.

JimboZA,

I have set up a bread board with 4 leds to represent the 4 windings of a stepper motor. Each led is connected via resistors to the Arduino board to pins 8, 9, 10 and 11.

Credit goes to David Cuartielles for a very basic program that cycles through each led with a pause in between.

To take it from here, I would like to be able to vary the speed with a pot, and be able to reverse the direction.

His cut down code is here

int motorPin1 = 8; int motorPin2 = 9; int motorPin3 = 10; int motorPin4 = 11; int delayTime = 500;

void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); }

void loop() { digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH); delay(delayTime); } This I can follow without much trouble.

I have fallen off my bike many times, but that was a long, long time ago........

Ken

neksmerj: This I can follow this without much trouble.

To take it from here, I would like to be able to vary the speed with a pot, and be able to reverse the direction.

Well taking the speed thing first, have you looked at this?

And for the direction, you could do what I suggested earlier: run either leg of an if/else based on the switch's value. One leg has the code you have currently in loop(), the other has very similar code but with those highs and lows in the digitalWrites re-jigged to go in the opposite direction.

Hi JimboZA,

Let's see your version of using the if/else statement, I've had my little go.

I've managed to control the speed of the sequential leds using a pot but haven't yet figured how to reverse the sequence.

Ken

neksmerj: I have set up a bread board with 4 leds to represent the 4 windings of a stepper motor. Each led is connected via resistors to the Arduino board to pins 8, 9, 10 and 11.

Sorry - but this is sheer folly.

Your Arduino will NOT be connecting to motor coils. That is the job of the stepper driver. You have no need to know what goes on inside the stepper driver.

The Arduino just needs to control the stepper driver by setting the direction pin for Clockwise or CounterClockwise and producing a pulse on the step pin whenever you want the motor to move one step in the selected direction.

This is what my code has been illustrating all along. My first simple code is deliberately very simple. I think part of the "cycling without stabilizers experience" is just to sit down and study my code (100 times if necessary) until you do understand it. And ask as many "stupid" questions about it as you like. Learning the code will also help you to learn about stepper motors.

...R

Oh dear oh dear Robin2,

Now I'm wondering whether you have read the above threads/posts etc.

I fully realise that in the real world a stepper motor will require a stepper motor board, and the code will be different.

The above excercise was a challenge started by JimboZA to simply simulate a stepper using leds run straight off the Arduino board.

Ken

Ken-

neksmerj:
Let’s see your version of using the if/else statement, I’ve had my little go.

My code from yesterday looks like the below: two leds and a switch, as shown in the attached schematic.

//read a switch and if it's high,
//    put a led on. If it's low,
//    put a different led on instead.
//LEDs are on pins 5 and 6, with the anodes
//    to 5V and cathodes to the pins. So, the
//    leds come on when their i/o pins are low
//The switch is on pin 10 with pullup enabled,
//    so pin 10 is usually high but goes low
//    when the switch is pushed.
//Serial is enabled and the value of the pin
//    is shown in the monitor each time thru loop()

byte ledOne=5; // a byte is the smallest type that can store a number
byte ledTwo=6;
byte mySwitch = 10;
bool mySwitchVal; // it can only be on or off so bool is cool


void setup() {
 Serial.begin(9600);
 pinMode(ledOne, OUTPUT);
 pinMode(ledTwo, OUTPUT);
 pinMode(mySwitch, INPUT_PULLUP);

}

void loop() {
mySwitchVal= digitalRead(mySwitch);
Serial.println(mySwitchVal);

if (mySwitchVal == 1)  // note the == not just a =
{
 digitalWrite(ledOne, HIGH);
 digitalWrite(ledTwo, LOW);
}
else // if not 1, it must be 0
{
 digitalWrite(ledOne, LOW);
 digitalWrite(ledTwo, HIGH);
}

}

twoledsswitch.PNG

Thanks JimboZA,

At my next opportunity, ie, daylight, I'll whack your code in and see what it does, looks interesting.

Ken

neksmerj: Oh dear oh dear Robin2,

Now I'm wondering whether you have read the above threads/posts etc.

I've put a lot into this. Now, and I'm out of here.

@KenF I hope your patience is more elastic than mine.

...R

For no apparent reason, the port comm3 has changed to comm1, and sample sketches that were running ok, now don’t.

Under the “Tools” menu, comm1 appears with no provision to change it to comm3.

When I first started to play with the Arduino, I had the choice, not now.

Any clues?

Ken

edit, looking at device manager, and clicking on Ports (com & lpt)
Arduino uno reports com3

When trying to run a sketch, the bottom dialogue box reports com1.

The sketch compiles but will not run

I accidentally found a fix. In Device Manager, I uninstalled Arduino UNO com1 port, then plugged my Arduio UNO board in.

Device manager then found the board and assigned com3 to it.

I uploaded the sketch, and it failed saying "could not find port com1, try another port", so I selected com3 and wallah.

Ken