Simplified control sketch for 4-wire Stepper Motor

Here a simplified sketch to control a four-wire stepper motor without using the Arduino Stepper library. The code handles the bits of PORTB. You can set the number of steps, direction and slowness. It works OK with ST28 and ST35 steppers and the Seeed Studio Motor shield V2.0 (L298 Dual Full-Bridge).

char stepper[]={39,15,30,54}; //sequence of control signals

void setup() {
  for(int i=8; i<14; i++) pinMode(i, OUTPUT);
}

void loop() {
  step(200,5); //clockwise
  step(-200,5); //counterclockwise
}

void step(int steps, int slowness){
   if(steps > 0){
   for(int i=0; i<steps; i++){
   for(int j=0; j<4; j++){
   PORTB=stepper[j];
   delay(slowness);}}}
   else{
   for(int i=0; i<abs(steps); i++){
   for(int j=3; j>=0; j--){
   PORTB=stepper[j];
   delay(slowness);}}}
}

Regards,

p

I love the variable "slowness" ;)

Palliser: Here a simplified sketch to control a four-wire stepper motor without using the Arduino Stepper library

This code is for controlling a stepper motor WITHOUT a proper stepper motor driver - which may be very useful for some readers. There is code in this demo for driving steppers WITH a stepper motor driver.

You need to provide a clear explanation of what sort of motor driver your code works with and how your code expects the motor driver to be connected to the Arduino.

It would be very useful to explain what the values in the array stepper[] actually do. (When I first looked at your code I though they were pin numbers for an Arduino Mega).

...R

Robin2: You need to provide a clear explanation of what sort of motor driver your code works with and how your code expects the motor driver to be connected to the Arduino.

It would be very useful to explain what the values in the array stepper[] actually do. (When I first looked at your code I though they were pin numbers for an Arduino Mega).

...R

I made these tables showing better how the Arduino (PORTB), motor and shield work together. Notice that BIT2 and BIT3 are fixed to ONE. Updated 12/02/2014

Here the link of the motor driver... Seeed Studio Motor Shield V2.0

Here the stepper motor link (ST28 or MOTS2)... Velleman 12VDC stepper motor

p

Thanks that is very clear.

Couple of things Are bits 3 and 2 "don't care" also?

You describe bits 6 and 7 as "don't care" but it is a little more than that. Those bits are the Arduino clock signal and you should not be writing them at all.

Rather than using PORTB=stepper[j ] it would be better to use PORTB &= xxxx to clear the bits that need to be 0s and PORTB |= yyyy to set the bits that need to be 1s.

...R

Great idea I like what you've done and no need for a driver, good call, will this work with the Nano? I dont know if you can help but i want to use this sketch so I can control the stepper motor with another stepper motor wirelessly. I want to use either bluetooth or 2.4ghz chips do you have an idea how I can make this work. I have had success controlling one nano with BT and my phone but i wish to use a stand alone device. So what it will be is :One nano with BT chip and stepper motor controlling another nanao with BT and stepper motor. Any ideas suggestions much appreciated. Thanks

Locohasa: no need for a driver

You need something between the Arduino and the motor.

A proper stepper motor driver has a tonne of advantages over a h-bridge.

...R

Robin2:
Couple of things
Are bits 3 and 2 “don’t care” also?

You describe bits 6 and 7 as “don’t care” but it is a little more than that. Those bits are the Arduino clock signal and you should not be writing them at all.

Rather than using PORTB=stepper[j ] it would be better to use PORTB &= xxxx to clear the bits that need to be 0s and PORTB |= yyyy to set the bits that need to be 1s.

…R

Robin, I polished a little bit my prior tables considering your apt comments. I am also using the Arduino nomenclature for the PORTB bits and elaborating a bit more the driver and motor functioning.

Here also a simplified schematic of the system (notice that I have obviated the ESD protection diodes, so be careful if you want to implement this solution).

Robin, regarding your questions…
Bits 1 and 2 of PORTB (digital pins 9 and 10) are connected to the ENABLE input pins (6 and 11) of the L298 bridge driver as shown in the above schematic.

You are rigth about bits 6 and 7 but notice that I set only the digital pins 8 to 13 (PORTB bits 0 to 5) to output bits as part of the setup() function. Bits 6 & 7 shall remain as input.

I’d use PORTB &+ and PORTB |= but I thought using stepper[j] made the code more elegant.

Finally, I think that the big thing with the code is that permit the user a full control of the stepper. Notice for example that with ST28, you can get 2048 steps per revolution which means 0.17 DEG per step as minimum resolution! It’s very convenient for applications that required accurate positioning. I am also using full step sequence that provides more torque.

p

You should have a look at the Atmel datasheet. Writing to an INPUT pin changes the state of the PULLUP resistor. It is NOT the same as not writing to an input pin.

...R

port B doesn't do anything to the XTAL pins if they are set up as XTAL pins in the fuse settings, you can treat B6, B7 as don't care (datasheet section 13.3 - alternate port functions). There is selection logic to fully take control away from the PORT/PIN/DDR registers for pins set to an alternate function.

Of course if you want your code to work whatever the fuse settings then you should mask out PORTB and DDRB appropriately.

Here a variant of my simplified sketch manipulating only bits 0 to 5 in PORTB.

void setup() {
  DDRB  = B111111; //sets digital pins 8 to 13 as outputs
  PORTB = B100111; //sets digital pins 13,10,9,8 HIGH
}

void loop() {
  step(1024,5); //one clockwise revolution (ST28)
  step(-1024,5); //one counterclockwise revolution (ST28)
}

void step(int steps, int slowness){
 for(int i=0; i<abs(steps); i++){
   if(steps > 0){
     PORTB ^= B101000;
     delay(slowness);
     PORTB ^= B010001;}
   else{
     PORTB ^= B010001;
     delay(slowness);
     PORTB ^= B101000;}
     delay(slowness);}
}

p

is it a good idea to connect the stepper direct to i/o pins if you don't have a stepper driver or full h driver ic?

Palliser: Here the stepper motor link (ST28 or MOTS2)... Velleman 12VDC stepper motor

p

But that's not a 4-wire stepper motor - have you mis-named this thread then?

pieter1992: is it a good idea to connect the stepper direct to i/o pins if you don't have a stepper driver or full h driver ic?

It would be the worst possible idea - certain to destroy your Arduino.

...R

pieter1992: is it a good idea to connect the stepper direct to i/o pins if you don't have a stepper driver or full h driver ic?

I did it once (UNO directly with a tiny bipolar Symbol 50-03500-??? with no load). It worked OK and no harm inflicted but as Robin said, I risked my UNO.

p

Palliser: I did it once (UNO directly with a tiny bipolar Symbol 50-03500-??? with no load). It worked OK and no harm inflicted but as Robin said, I risked my UNO.

There are some incredibly small steppers that can work like that - is that what you have? Have you a link to the motor datasheet?

...R

50-03500-028. 40 steps/rev. I don't have the specs. It was part of an old bar code scanner (Symbol LS2000II) and it had tied an oscillating concave mirror.

p

Hi everyone!

I am trying to actionate the same stepper motor mentioned in this post with this shield http://www.waveshare.com/Motor-Control-Shield.htm I made the following links: Blue-M1A Yellow-M1B Pink - M2A Orange - M2B And I'm using this sketch (https://www.arduino.cc/en/Tutorial/StepperUnipolar) exept for the pin used that are 2,3,4,5 instead of 8,9,10,11 However I have no response from the motor Am I missing something in wiring, or do I need a different sketch?

Thanks in Advance P.

You have re-awakened a 2-years dead Thread. I certainly can't remember back that far.

Start from the beginning again.

  • Post a link to the datasheet for your motor
  • Make a simple pencil drawing showing how everything is connected and post a photo of the drawing.
  • Post the program you are using.

...R