Auto Diagnostic Logiciel OBD2 Diagnostic

Auto Diagnostic Appareils

Basic Arduino example code to control a stepper motor 

Basic Arduino example code to control a stepper motor 

Now that you have wired up the digital stepper motor driver
and set the current limit, it is time to connect the Arduino to the computer and upload some code. You can upload the following example code to your Arduino using the Arduino IDE. For this specific example you do not need to install any libraries.

This sketch controls both the speed, the number of revolutions and the spinning direction of the stepper motor.

You can open the code in a new window by clicking on the button in the top right corner.

  1. /*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino without a library. More info: https://www.makerguides.com */
  2. // Define stepper motor connections and steps per revolution:
  3. #define dirPin 2
  4. #define stepPin 3
  5. #define stepsPerRevolution 200
  6. void setup() {
  7. // Declare pins as output:
  8. pinMode(stepPin, OUTPUT);
  9. pinMode(dirPin, OUTPUT);
  10. }
  11. void loop() {
  12. // Set the spinning direction clockwise:
  13. digitalWrite(dirPin, HIGH);
  14. // Spin the stepper motor 1 revolution slowly:
  15. for (int i = 0; i < stepsPerRevolution; i++) {
  16. // These four lines result in 1 step:
  17. digitalWrite(stepPin, HIGH);
  18. delayMicroseconds(2000);
  19. digitalWrite(stepPin, LOW);
  20. delayMicroseconds(2000);
  21. }
  22. delay(1000);
  23. // Set the spinning direction counterclockwise:
  24. digitalWrite(dirPin, LOW);
  25. // Spin the stepper motor 1 revolution quickly:
  26. for (int i = 0; i < stepsPerRevolution; i++) {
  27. // These four lines result in 1 step:
  28. digitalWrite(stepPin, HIGH);
  29. delayMicroseconds(1000);
  30. digitalWrite(stepPin, LOW);
  31. delayMicroseconds(1000);
  32. }
  33. delay(1000);
  34. // Set the spinning direction clockwise:
  35. digitalWrite(dirPin, HIGH);
  36. // Spin the stepper motor 5 revolutions fast:
  37. for (int i = 0; i < 5 * stepsPerRevolution; i++) {
  38. // These four lines result in 1 step:
  39. digitalWrite(stepPin, HIGH);
  40. delayMicroseconds(500);
  41. digitalWrite(stepPin, LOW);
  42. delayMicroseconds(500);
  43. }
  44. delay(1000);
  45. // Set the spinning direction counterclockwise:
  46. digitalWrite(dirPin, LOW);
  47. //Spin the stepper motor 5 revolutions fast:
  48. for (int i = 0; i < 5 * stepsPerRevolution; i++) {
  49. // These four lines result in 1 step:
  50. digitalWrite(stepPin, HIGH);
  51. delayMicroseconds(500);
  52. digitalWrite(stepPin, LOW);
  53. delayMicroseconds(500);
  54. }
  55. delay(1000);
  56. }

How the code works: 

The sketch starts with defining the step and direction pins. I connected them to Arduino pin 3 and 2.

The statement #define is used to give a name to a constant value. The compiler will replace any references to this constant with the defined value when the the program is compiled. So everywhere you mention dirPin, the compiler will replace it with the value 2 when the program is compiled.

I also defined a stepsPerRevolution constant. Because I set the driver to full step mode I set it to 200 steps per revolution. Change this value if your setup is different.

  1. // Define stepper motor connections and steps per revolution:
  2. #define dirPin 2
  3. #define stepPin 3
  4. #define stepsPerRevolution 200

In the setup() section of the code, all the motor control pins are declared as digital OUTPUT with the function pinMode().

  1. void setup() {
  2. // Declare pins as output:
  3. pinMode(stepPin, OUTPUT);
  4. pinMode(dirPin, OUTPUT);
  5. }

In the loop() section of the code, we let the motor spin one revolution slowly in the CW direction and one revolution quickly in the CCW direction. Next we let the motor spin 5 revolutions in each directions with a high speed. So how do you control the speed, spinning direction and number of revolutions?

  1. // Set the spinning direction clockwise:
  2. digitalWrite(dirPin, HIGH);
  3. // Spin the stepper motor 1 revolution slowly:
  4. for(int i = 0; i < stepsPerRevolution; i++)
  5. {
  6. // These four lines result in 1 step:
  7. digitalWrite(stepPin, HIGH);
  8. delayMicroseconds(2000);
  9. digitalWrite(stepPin, LOW);
  10. delayMicroseconds(2000);
  11. }

Control spinning direction: 

To control the spinning direction of the stepper motor we set the DIR (direction) pin either HIGH or LOW. For this we use the function digitalWrite(). Depending on how you connected the stepper motor, setting the DIR pin high will let the motor turn CW or CCW.

Control number of steps or revolutions: 

In this example sketch, the for loops control the number of steps the stepper motor will take. The code within the for loop results in 1 step of the stepper motor. Because the code in the loop is executed 200 times (stepsPerRevolution), this results in 1 revolution. In the last two loops, the code within the for loop is executed 1000 times, which results in 1000 steps or 5 revolutions.

Note that you can change the second term in the for loop to whatever number of steps you want. for(int i = 0; i < 100; i++) would result in 100 steps, or half a revolution. nema 23 gearbox

Control speed: 

The speed of the stepper motor is determined by the frequency of the pulses we send to the STEP pin. The higher the frequency, the faster the motor runs. You can control the frequency of the pulses by changing delayMicroseconds() in the code. The shorter the delay, the higher the frequency, the faster the motor runs.

http://outilsdediagnosticobd2.yolasite.com/

http://duohao212.inube.com/blog/8567939/differences-between-the-a4988-and-drv8825/