Ingo Lohs
Published © LGPL

Stepper - A First Introduction to Nema 17

Here's an introduction to control a Nema 17 stepper motor with the help of the Adafruit TB6612 OR A4988 Stepstick OR PHPoC PES-2405 R2

BeginnerProtip1 hour13,770

Things used in this project

Hardware components

NEMA 17 Stepper Motor
OpenBuilds NEMA 17 Stepper Motor
I used the model 17HD40005 22B from Busheng - this stepper handles 200 steps per revolution
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
Nema 17 works with 4,5-12Volt with jack adapter
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
tested with Arduino and Particle Photon
×1
Photon
Particle Photon
×1
LED (generic)
LED (generic)
Test with an LED the stepper motor
×1
A4988 Stepstick from Elegoo
5 pieces round about 13 Euro
×1
Capacitor 47 µF
Capacitor 47 µF
Pololu wiring talks about 100 nF
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
OR build.particle.io in case you use a Particle Product
Blynk
Blynk

Story

Read more

Code

Stepper Example A4988

C/C++
works without Library
const int dirPin = A0; 
const int stepPin = A1; 


void setup() {
pinMode(stepPin,OUTPUT); 
pinMode(dirPin,OUTPUT);
}

void loop() {
digitalWrite(dirPin,HIGH); 
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH); 
delayMicroseconds(2000); 
digitalWrite(stepPin,LOW); 
delayMicroseconds(2000); 
}
delay(1000);
digitalWrite(dirPin,LOW); 
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}
delay(1000);
}

Stepper Example with A4988 and Potentiometer

C/C++
    /*     Simple Stepper Motor Control Exaple Code
     *      
     *  by Dejan Nedelkovski, www.HowToMechatronics.com
     *  https://howtomechatronics.com/tutorials/arduino/how-to-control-stepper-motor-with-a4988-driver-and-arduino/
     *  updated for Particle by Ingo Lohs
     */
     
    // Defines pins numbers
    const int stepPin = A1;
    const int dirPin = A0; 
    const int PinPoti = A2;
    
    // Defines variables
    int customDelay, customDelayMapped; 
     
    void setup() {
      // Sets the two pins as Outputs
      pinMode(stepPin,OUTPUT);
      pinMode(dirPin,OUTPUT);
      // and the Poti as Input
      pinMode(PinPoti,INPUT);
     
      //Enables the motor to move in a particular direction
      digitalWrite(dirPin,HIGH); 
    }
    
    void loop() {
      
      // Gets custom delay values from the custom speedUp function
      customDelayMapped = speedUp(); 
      
      // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(customDelayMapped);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(customDelayMapped);
    }
    
    // Function for reading the Potentiometer
    int speedUp() {
      // Reads the potentiometer
      int customDelay = analogRead(PinPoti); 
      // Converts the read values of the potentiometer from 0 to 1023 into desired delay values (300 to 4000)
      int newCustom = map(customDelay, 0, 1023, 300, 4000); 
      return newCustom;  
    }

Stepper-Lib Example "One Revolution"

C/C++
/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Stepper Example with A4988 and software defined Potentiometer with Blynk

C/C++
    #include <blynk.h>
     
    // Defines pins numbers
    const int stepPin = A1;
    const int dirPin = A0; 
   
    // Defines variables
    int customDelay, customDelayMapped, PotiValue, absValue; 

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "12c60ade0d684fb5974795f81eeaf230";

    // This function will be called every time Slider Widget
    // in Blynk app writes values to the Virtual Pin V0
    BLYNK_WRITE(V0) // // Blynk app WRITES Slider widget
    {
    int PotiValue = param.asInt(); // assigning incoming value from pin V0 to a variable
    absValue = abs(PotiValue); // abs( ) function in C returns the absolute value of an integer. The absolute value of a number is always positive. Only integer values are supported in C.
    
    // process received value
    }

    void setup() {
      // Sets the two pins as Outputs
      pinMode(stepPin,OUTPUT);
      pinMode(dirPin,OUTPUT);

      //Enables the motor to move in a particular direction
      digitalWrite(dirPin,HIGH); 
      
      Blynk.begin(auth);
    }
    
    void loop() {
      
      Blynk.run();
    
      // Gets custom delay values from the custom speedUp function
      customDelayMapped = speedUp(); 
      
      // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(customDelayMapped);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(customDelayMapped);
    }
    
    // Function for reading the Potentiometer
    int speedUp() {
      
      // Converts the read values of the potentiometer from 0 to 1023 into desired delay values (300 to 4000)
      int newCustom = map(absValue, 0, 1023, 300, 4000); 
      return newCustom;  
    }

move.php

PHP
<?php
include_once "/lib/sd_spc.php";

spc_reset();
spc_sync_baud(115200);

$sid = 1;
spc_request_dev($sid, "set mode 4");
spc_request_dev($sid, "set vref stop 2");
spc_request_dev($sid, "set vref drive 8");
spc_request_dev($sid, "set rsnc 120 250");

spc_request_dev($sid, "move 800 400 800 0");
while((int)spc_request_dev($sid, "get state"))
  usleep(1);

//sleep(1);

spc_request_dev($sid, "move -800 400 0 800");
while((int)spc_request_dev($sid, "get state"))
  usleep(1);
?>

goto.php

PHP
<?php
include_once "/lib/sd_spc.php";

spc_reset();
spc_sync_baud(115200);

$sid = 1;
spc_request_dev($sid, "set mode 4");
spc_request_dev($sid, "set vref stop 2");
spc_request_dev($sid, "set vref drive 8");
spc_request_dev($sid, "set rsnc 120 250");
spc_request_dev($sid, "set pos -400");

spc_request_dev($sid, "goto +400 200 0");
sleep(1);
spc_request_dev($sid, "goto -400 200 0");
sleep(1);
spc_request_dev($sid, "goto +400 200 0");
while((int)spc_request_dev($sid, "get state"))
    usleep(1);
?>

Credits

Ingo Lohs

Ingo Lohs

94 projects • 125 followers
I am well over 40 years and come from the middle of Germany.

Comments

Add projectSign up / Login