Thursday, January 3, 2013

Stepper motor control using Arduino and L293


Components
IC1
L293D

B1
5v DC supply

SW1,SW2
N.O. Push Button

R1,R2,R3,R4
100 omhs   1/4W

R5.R6
10k ohms 1/4W

D1,D2,D3,D4
LED

CN1
To pin13 (digital pin)
ARDUINO
CN2
To pin12 (digital pin)
CN3
To pin11 (digital pin)
CN4
To pin10 (digital pin)
CN5
To Stepper motor(4 wire)
MOTOR
CN6
To pin2 (digital pin)
ARDUINO
CN7
To pin3 (digital pin)
CN8
To A0(analog pin)
CN9
To Ground pin
CN10
To 5v pin




























There are 2 push buttons, one is for CW rotation and the other is for reverse.
Potentiometer is used to control the speed of the motor. Minimum speed is 0 and 100 for the maximum. If the speed is 0 the motor will not rotate even the push button is pressed. If the two push buttons were pressed the motor will not rotate as well. 


Codes:


#include <Stepper.h>

int forward = 2;
int reverse = 3;

Stepper motor(200, 10,11,12,13);            

void setup() {
  pinMode(forward,INPUT);
  pinMode(reverse,INPUT);
    Serial.begin(9600);
}

void loop() {
  int Speed = analogRead(A0);
  int RPM = map(Speed, 0, 1023, 0, 100);
  int f = digitalRead(forward);
  int r = digitalRead(reverse);
  if(f == 1 && r == 0 && RPM > 1){
  motor.step(1);
    motor.setSpeed(RPM);
    delay(.01);
  }
  if(r == 1 && f== 0  && RPM > 1){
    motor.step(-1);
    motor.setSpeed(RPM);
        delay(.01);
  }
  delay(5);
    Serial.println(RPM);
}







7 comments:

Rick said...

Bravo! Thank you for your diagram and code. I am a beginner and find simple and efficient code like yours perfect for me learning. Now I just wish I could find a circuit diagram for a unipolar stepper.

Have a great day!

Rick

Janine said...

Thank you rick :)-N

Mike said...

Can this code be used with the Arduino Motor Shield V3.0 ?
Mike

Janine said...

yes mike.

sara said...

thank you very much
can you explain the code please ,because i cannot understand it.
and when i connect the circuit where i have to put the potential meter.

sara

sara said...

thank you Jonine
i love every thing about it but i face some problems can you explain the code please and how can i connect a potential meter in the circuit .
have a great weekend

sara

Unknown said...

I like your sketch I am trying to push a button and it does one full rotation. I like the pot on yours to adjust speed but I also am using another button to turn on/off a laser.