Arduino Stepper Motors (https://learn.adafruit.com/adafruit-arduino-lesson-16-stepper-motors/overview)

Stepper motors fall somewhere in between a regular DC motor and a servo motor. They have the advantage that they can be positioned accurately, moved forward or backwards one 'step' at a time, but they can also rotate continuously.
In this lesson you will learn how to control a stepper motor using your Arduino and the same L293D motor control chip that you used with the DC motor in lesson 15.
learn_arduino_overview.jpg

Parts

To build the project described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_stepper.jpg
5V Stepper Motor 1
learn_arduino_L293d.jpg
L293D IC
1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard
1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3
1
learn_arduino_jumpers_web.jpg
Jumper wire pack
Breadboard Layout
The stepper motor has five leads, and we will be using both halves of the L293D this time. This means that there are a lot of connections to make on the breadboard.
The motor has a 5-way socket on the end. Push jumper wires into the sockets to allow the motor to be connected to the breadboard.
learn_arduino_fritzing.jpg
Note that the red lead of the Stepper motor is not connected to anything.


Arduino Code

The following sketch uses the Serial Monitor, so once the sketch is installed and running, open the Serial Monitor and enter a number of 'steps'. Try a value of about 500, this should cause the motor to turn through about 360 degrees. Enter -500 and it will turn back in the reverse direction.
  1. /*
  2. Adafruit Arduino - Lesson 16. Stepper
  3. */
  4.  
  5. #include <Stepper.h>
  6.  
  7. int in1Pin = 12;
  8. int in2Pin = 11;
  9. int in3Pin = 10;
  10. int in4Pin = 9;
  11.  
  12. Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);
  13.  
  14. void setup()
  15. {
  16. pinMode(in1Pin, OUTPUT);
  17. pinMode(in2Pin, OUTPUT);
  18. pinMode(in3Pin, OUTPUT);
  19. pinMode(in4Pin, OUTPUT);
  20.  
  21. // this line is for Leonardo's, it delays the serial interface
  22. // until the terminal window is opened
  23. while (!Serial);
  24. Serial.begin(9600);
  25. motor.setSpeed(20);
  26. }
  27.  
  28. void loop()
  29. {
  30. if (Serial.available())
  31. {
  32. int steps = Serial.parseInt();
  33. motor.step(steps);
  34. }
  35. }
As you might expect, there is an Arduino library to support stepper motors. This makes the process of using a motor very easy.
After including the 'Stepper' library, the four control pins 'in1' to 'in4' are defined.
To tell the Arduino Stepper library which pins are connected to the motor controller, the following command is used:
  1. Stepper motor(768, in1Pin, in2Pin, in3Pin, in4Pin);
The first parameter is the number of 'steps' that the motor will take to complete one revolution. The motor can be moved by one step at a time, for very fine positioning.
Serial communications is then started, so that the Arduino is ready to receive commands from the Serial Monitor.
Finally the following command sets the speed that we wish the stepper motor to move, when we subsequently tell it how many steps to rotate.
  1. motor.setSpeed(10);
The 'loop' function is very simple. It waits for a command to come in from the Serial Monitor and converts the text of the number sent into an int using 'parseInt'. It then instructs the motor to turn that number of steps. 

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

IC 13

DIY Arduino Traffic Light Pedestrian Light Push Button Control (https://thecustomizewindows.com/2016/06/diy-arduino-traffic-light-pae-push-button/)

Arduino Servo Motors (https://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors/arduino-code-for-sweep)