Lego Walker stepper motor with Arduino

Geared Stepper Motor 28BYJ-48  5V  with  ULN2003 driver board


/*
 Stepper Motor Test
 This sketch test a geared stepper motor.
 The motor will revolve one revolution forward then backward.
 The motor is attached to digital pins 8 - 11. 
*/

int i=0;
int m=4


void setup() {              
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
      for(i=0;i<512;i++)
     {
      forward();
      }
       delay(1000);             

      for(i=0;i<512;i++)
     {
      backward();
      }
       delay(1000);            
}

void forward()
{
  step1();
  step2();
  step3();
  step4();
  step5();
  step6();
  step7();
  step8();
}
void backward()
{
  step8();
  step7();
  step6();
  step5();
  step4();
  step3();
  step2();
  step1();
}
void step1()
{
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay(m);
}
void step2()
{
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay(m);
}
void step3()
{
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
   delay(m);
}
void step4()
{
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
   delay(m);
}
void step5()
{
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
   delay(m);
}
void step6()
{
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  v
}
void step7()
{
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
   delay(m);
}
void step8()
{
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
   delay(m);
}



Materials and Assembly Section:







 



For Raspberry Pi:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

enable_pin = 18
pinA= 4
pinB = 17
pinC = 23
pinD = 24

GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(pinA, GPIO.OUT)
GPIO.setup(pinB, GPIO.OUT)
GPIO.setup(pinC, GPIO.OUT)
GPIO.setup(pinD, GPIO.OUT)

GPIO.output(enable_pin, 1)

def forward(delay, steps):
  for i in range(0, steps):
    setStep(1, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 1, 1, 0)
    time.sleep(delay)
    setStep(0, 1, 0, 1)
    time.sleep(delay)
    setStep(1, 0, 0, 1)
    time.sleep(delay)

def backwards(delay, steps):
  for i in range(0, steps):
    setStep(1, 0, 0, 1)
    time.sleep(delay)
    setStep(0, 1, 0, 1)
    time.sleep(delay)
    setStep(0, 1, 1, 0)
    time.sleep(delay)
    setStep(1, 0, 1, 0)
    time.sleep(delay)

def setStep(m1, m2, m3, m4):
  GPIO.output(pinA, m1)
  GPIO.output(pinB, m2)
  GPIO.output(pinC, m3)
  GPIO.output(pinD m4)

while True:
  delay = raw_input("Delay between steps (milliseconds)?")
  steps = raw_input("How many steps forward? ")
  forward(int(delay) / 1000.0, int(steps))
  steps = raw_input("How many steps backwards? ")
  backwards(int(delay) / 1000.0, int(steps))

Comments

Popular posts from this blog

Lego Walker using Lawsin Linkage