Stepper - A First Introduction to Nema 17 © LGPL

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

  • 5,643 views
  • 6 comments
  • 43 respects

Components and supplies

Nema%2017 2
OpenBuilds NEMA 17 Stepper Motor
I used the model 17HD40005 22B from Busheng - this stepper handles 200 steps per revolution
×1
11026 02
Jumper wires (generic)
×1
12002 04
Breadboard (generic)
×1
Adafruit industries ada63 image 75px
9V 1A Switching Wall Power Supply
Nema 17 works with 4,5-12Volt with jack adapter
×1
A000066 iso both
Arduino UNO & Genuino UNO
tested with Arduino and Particle Photon
×1
Photon new
Particle Photon
×1
09590 01
LED (generic)
Test with an LED the stepper motor
×1
Elegoo A4988 Stepstick from Elegoo
5 pieces round about 13 Euro
×1
Panasonic eca2am470 image
Capacitor 47 µF
Pololu wiring talks about 100 nF
×1
09939 01
Rotary potentiometer (generic)
×1

Apps and online services

Ide web
Arduino IDE
OR build.particle.io in case you use a Particle Product
Blynk logo new svawbywyip
Blynk

About this project

I was struggling with the wiring of a stepper motor and so I hope I can take the fear out of you with this article: I use the same PINs to illustrate two different devices driving the Nema 17 stepper motor using an Adafruit TB6612.

Test the Stepper - Preparation

In the beginning, a short test with an LED is worthwhile to find out the correct cable routing of the two coils in the stepper: pick up an LED and insert the two feet into the adapter: move the motor with your hand.

If the LED is lit you have identified the two cable for connection to the MOTORA or MOTORB breakout board.

Wiring

Works!

The same structure works with the given library "Stepper" examples for both the Uno and the Photon (here only the PINs in the original example have to be replaced by 4, 5, 6 and 7).

Adafruit offers a good step-by-step guide to their break-out board.

Photon LED on D7 blinks ;-)

Now we are ready to face ambitious projects as well. We already have the right breakout board in our hands: this can up to operate four solenoids, spin two DC motors or step one bi-polar or uni-polar stepper with 1.2A per channel.

Update 03.10.2018 - A4988

I found in my component box an alternative Stepper Motor Driver, which I can also recommend: the Stepstick A4988 by Elegoo.

You can read the technical features on the website of the manufacturer.

A nice explanation by video you can find here.

With the knowledge from above, should the adaptation of the wire loop for the A4988 be equally possible? And so it behaves:

Wiring...

... using the example of the photon:

Direction <> Device A0

Step <> Device A1

Sleep <> Reset - mandatory!

MS3, MS2, MS1, Enable <> without connection

GND, VDC <> 5V (from Device)

1B, 1A <> coil1

2A, 2B <> coil2

GND, VMOtor <> 12V (from external PowerSupply)

Works!

To ensure a drop in performance is still a 47 nF Capacitator when supplying the 12 volts necessary.

it works!

Datasheet from Pololu here.

Update 04.10.2018 - Control the speed with potentiometer

Now when I had the structure in front of me, it was now to influence the rotational speed to take: it offers a potentiometer. In the picture you see a blue "U 103" which can be addressed at the side of the notches with GND, signal and 5V. In addition, a black potentiometer, which can be rotated better thanks to its large rotary lever. The wiring is identical and so I have passed the signal to the photon at A2.

The necessary code can be found below.

Control the speed with a software defined potentiometer with Blynk

If you do not have any potentiometers at your disposal, you can also use software-based potentiometers: for this purpose, we integrate the library from Blynk. Blynk offers us several predefined components: I chose a slider and connected it to "virtual 0" in the Blynk app.

The value scale is restricted from 0 to 1023, where 1023 means "slow".

The slightly changed code can be found as usual below. Two additional variables were added and instead of reading out A2, the values arriving from the slider are now linked with "virtual 0".

Pay attention to the use of the function abs (), otherwise it will not work as expected.

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
   }

Update 15.10.2018 - Mapping

 // 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); 

Has the map function noticed? Here he is explained:

Mapping is a way to translate a value from one range of numbers to another. It can be used to create a direct relationship between an input and an output. For instance, the value provide by a potentiometer could control the stepper-rotation. To accomplish this, there is a very useful function called map(). It looks like this:

map(value, fromLow, fromHigh, toLow, toHigh)

value is the value that you would like to map. fromLow and fromHigh is the low and high end of the original data set. toLow and toHigh are the low and high values of the mapped data set. If you were to map the full range of analog input (0 to 1023) to the full range of analog output (0 to 255), you’d use the following line of code

map(value, 0, 1023, 0, 255)

Arduino Map-Reference and Particle Map-Reference

Update 03.11.2018 - PHPoC Blue + PES-2405 R2

If you are unfamiliar with PHPoC, I recommend getting familiar with the preparation/introduction first.

Within the product family you will also find an "smart extension board" to use as a shield on the PHPoC Blue/Black (but not for the PHPoC Shield for Arduino). The PES-2405 R2 is piggy-backed onto the PHPoC board with its PINs and the stepper motor is already under the control of PHP. This is possible with a separate library SD_SPC.php via include_once "/lib/sd_spc.php";

I have detailed command usage here and is quite extensive.
Here move and goto are demonstrated.

The examples are set via DIP switch on port 1 (1,2,3 below, 4 above). More information in chapter 3 - Slave ID Switch.

If you have added the library with the help of the PHPoCD application, then the familiar wiring of the motor is sufficient with the aid of a screwdriver on the green terminal (A +, A-, B +, B- and separate voltage of 12 V in GND and VM).

The PHPoC is powered by 5V from the power bank.

Now add the PHP files to the root directory and call the URL to the PHPoC in the browser - like http://192.168.178.55/goto.php.
Already the stepper should move - done.

goto.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);
?>
move.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);
?>

Code

Stepper Example A4988C/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 PotentiometerC/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 BlynkC/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.phpPHP
<?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.phpPHP
<?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);
?>

Comments

Similar projects you might like

Automatic Pet Water Chiller

Project tutorial by Mitomon

  • 966 views
  • 0 comments
  • 7 respects

Snap Circuits® - Introduction

by Daniel Porrey

  • 9,961 views
  • 0 comments
  • 24 respects

Smart Energy Saver for Your Home

Project tutorial by Dhairya Parikh

  • 7,423 views
  • 14 comments
  • 24 respects

Arduino MKR1000 & Blynk

Project tutorial by Lintang Wisesa

  • 339 views
  • 0 comments
  • 3 respects
Add projectSign up / Login