Liberty BASIC Community Forum
« Stepper motor control »

Welcome Guest. Please Login or Register.
May 28th, 2017, 10:52pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Stepper motor control  (Read 149 times)
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5292
xx Stepper motor control
« Thread started on: Feb 16th, 2016, 09:00am »

I continue to explore. If you wish to play with stepper motors and our Arduino SKetch then this is a good starting point, they are cheap powerful and often come with the driver board.


The stepper is 28BYJ-48 and the Board a ULN2003 Google for examples.


The stepper operates of 5V and takes a very small current. It is a geared stepper and rotates relatively slowly. It is a 64 step motor though the gearing demands 2048 steps to complete a full revolution of the output shaft.

It is an eight phase motor and so needs a little tweak in our Sketch to get it to run well.

The stepper library we load in the Sketch can sequence two or four phases, to get an eight phase motor to work we need to change the phasing of the pins from 1234 to 1324.

Easily done in the Sketch.


Code:
#include <Stepper.h>
// initialize 64 step stepper on pins 3,4,5,6:
// use whatever digital pins you wish, I used
// 3,4,5,6
// some steppers will use two pins some four
// some steppers need eight steps but will run 
// with the phasing change as below 3,5,4,6
Stepper myStepper(64, 3,5,4,6);

#include <Servo.h>
// set up 14 servo objects in an array
Servo myservos[14] ;

void setup() {
  Serial.begin(9600);
  Serial.println("Com Open");
}
void loop() {

  // you can call your own functions here
  // lbRun() is only called by interrupt
  // when there is serial data available

}

// serialEvent checks to see if data is available on serial port
// when a message comes we just invoke our lbRun() function


void serialEvent() {
  // check if data available, if so call the lbRun() routine
  if ( Serial.available()) {
    lbRun();
  }
}


void lbRun()
{
  while (Serial.available() > 10)
  {
    // look for the next valid integer in the incoming serial stream:
    int cmd = Serial.parseInt();
    // do it again:
    int pin = Serial.parseInt();
    // do it again:
    int val = Serial.parseInt();
    // look for the "*", That's the end of our command string
    if (Serial.read() == '*')
    {
      //now select the command to run and use the pin and val argument
      if ( cmd == 0)//add servo
      {
        myservos[pin].attach(pin);
      }

      if ( cmd == 1)//getdigital
      {
        pinMode(pin, INPUT);//set to input
        digitalWrite(pin, HIGH);//turn on pullup resistor
        Serial.print(pin);
        Serial.print(",");
        Serial.print(digitalRead(pin));
        Serial.print("*");
      }

      if ( cmd == 2)//getanalog
      {
        analogRead(pin);
        delay(10);
        Serial.print(pin);
        Serial.print(",");
        Serial.print(analogRead(pin));
        Serial.print("*");
      }

      if ( cmd == 3)//getpulse
      {
        Serial.print(pin);
        Serial.print(",");
        Serial.print(pulseIn(pin, val));
        Serial.print("*");
      }

      if ( cmd == 4)//setdigital
      {
        pinMode(pin, OUTPUT);//set to ouput
        digitalWrite(pin, val);
      }

      if ( cmd == 5)//setanalog
      {
        analogWrite(pin, val);
      }

      if ( cmd == 6)//setservo
      {
        myservos[pin].write(val) ;
      }

      if ( cmd == 7)//settone
      {
        if (val == 0)
        {
          noTone(pin);
        }
        else
        {
          tone(pin, val);
        }
      }

      if ( cmd == 8)//ping
      {

        // establish variables for duration of the ping,
        long duration;

        // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
        // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delayMicroseconds(2);
        digitalWrite(pin, HIGH);
        delayMicroseconds(5);
        digitalWrite(pin, LOW);

        // The same pin is used to read the signal from the PING))): a HIGH
        // pulse whose duration is the time (in microseconds) from the sending
        // of the ping to the reception of its echo off of an object.
        pinMode(pin, INPUT);
        duration = pulseIn(pin, HIGH);
        Serial.print(pin);
        Serial.print(",");
        Serial.print(duration);
        Serial.print("*");
      }

      if ( cmd == 9)//detach servo
      {
        myservos[pin].detach();
      }
      
      if ( cmd == 10)//set the stepper speed
      {
        myStepper.setSpeed(val);
      }
      
      if (cmd == 11)//set the stepper in motion +
      // this is a blocking call, the stepper will run at 
      // the setSpeed value till steps are complete
      {
        myStepper.step(val);
        Serial.print("99,0,*");

      }
      if (cmd == 12)//set the stepper in motion -
      {
        myStepper.step(-val);
        Serial.print("99,0,*");
      }
    }
  }
}   // end or lbRun()

// add your own functions here

 



Wire the stepper as per the instruction that come with the board, it needs its own 5V supply and only shares a commin ground with the Arduino board. Now use the simple GUI on the Wiki

http://lbpe.wikispaces.com/Fun+with+the+Arduino

Send the SetSpeed command with a value of 200
Send the SetStepR command with a value of 2048
the stepper will rotate one full revolution right.
Send the SetStepL command with a value of 2048
the stepper will rotate one full revolution left.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls