top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

NEMA Stepper speed control with Arduino and Easy Driver



OVERVIEW


In this third part we will use the Rotary Encoder integrated switch to select if we want to move the NEMA motor or set the speed at which we want it to move when we rotate the encoder.


To visually display the speed, we will use a WS2812 RGB stick and light up the LED’s to represent the set speed.


This will show that we can use 1 control, the Rotary Encoder in this case, and have it do two different things.


Like in our last tutorial we are using a NEMA 17 size motor, the Easy Driver board and the Rotary Encoder.


In the next tutorial we will use more buttons to set IN and OUT point that we can make the motor travel between, as well as a buzzer to give us sound feedback.

 

PARTS USED


EasyDriver Stepper Driver

Rotary Encoder Module


Stepper Motor NEMA 17


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




We are using 5 pins to connect to the Easy Driver and 3 pins for the rotary encoder module.


Pin 8 is connected to DIR Pin 9 to STEPS Pin 10 to MS1 Pin 11 to MS2 and Pin 12 to SLEEP


The Voltage and GND of the Easy Driver are connected to a 12V 1A power supply.

The NEMA 17 motor we are using has a max amperage draw of around 0.45A.


The 4 leads of the NEMA stepper (2 per coils), are connected directly to the Easy Driver A and B.

A quick way to identify which wires are part of the same coil is to connect two wires together and if you feel resistance when trying to turn the stepper motor shaft, that means that those 2 wires are part of the same coil.


Pin 2(CLK), 3(DT) and 4(SW) are receiving information from the rotary encoder.


The encoder is also connected to 3.3V and Ground of the UNO.


DIN of the WS2812 RGB stick is connected to Pin 5 and Voltage and Ground is connected to the 5V and GND of the UNO.


We also connect the UNO Ground to the Easy Driver to serve as a reference.

 

THE CODE


Instead of using the switch on the Rotary Encoder to reset the motor position like in the last tutorial, we are using it to select if the rotary encoder will move or set the speed of the motor.


We are using the FASTLED library to control the WS2812 RGB stick.

We will not use a library to make the motor move since for our needs it is not needed.


We are using a variable “rswitch” to have the interrupt set the speed or move the motor.


The variable is switched between zero and one by clicking the Rotary Encoder switch.


As always for more information about the tutorial and explanation of the code please watch our tutorial video.


/* Start of Code */

#include "FastLED.h" // FastLED library

// EasyDriver connections
#define dir_pin 8 // Pin 8 connected to Direction pin
#define step_pin 9 // Pin 9 connected to Steps pin on EasyDriver
#define MS1 10 // Pin 10 connected to MS1 pin
#define MS2 11 // Pin 11 connected to MS2 pin
#define SLEEP 12 // Pin 12 connected to SLEEP pin

// WS2812 RGB Stick connection
#define led_pin 5 // Pin 5 connected to DIN of RGB Stick

// Rotary Encoder Module connections
const int PinCLK=2; // Generating interrupts using CLK signal
const int PinSW=4; // Reading Push Button switch
const int PinDT=3; // Reading DT signal

#define NUM_LEDS 8 // # of WS2812 LEDs on stick
CRGB leds[NUM_LEDS]; // FastLED Library Init

volatile boolean TurnDetected; // to detect rotation of Rotary Encoder
volatile boolean rotationdirection; // CW or CCW rotation
volatile boolean rswitch=0; // Variable to store rotary encoder switch state
volatile boolean led_speed_change; // to detect change of speed
volatile int StepsToTake=2; // Controls the speed of the Stepper per Rotary click

int direction; // Variable to set Rotation (CW-CCW) of stepper


// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect () {
delay(10); // delay for Debouncing Rotary Encoder

if (rswitch == 1) {
if (digitalRead(PinCLK)) {
if (digitalRead(PinDT)) {
if (StepsToTake > 2){
StepsToTake=StepsToTake-1; }}
if (!digitalRead(PinDT)) {
if (StepsToTake < 9){
StepsToTake=StepsToTake+1; }}
led_speed_change = true;
}
}

else {
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
}

void setup () {

FastLED.addLeds<NEOPIXEL,led_pin>(leds, NUM_LEDS); // Setup FastLED Library
FastLED.clear();

// Light up starting LED's
for(int x = 0; x != (StepsToTake - 1); x++) {
if (x < 2) leds[x] = CRGB::Red; if (x > 1 & x < 5) leds[x] = CRGB::Orange; if (x > 4) leds[x] = CRGB::Green; }

FastLED.setBrightness(50);
FastLED.show();

pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(SLEEP, OUTPUT); 
digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
delay(5); // Wait for EasyDriver wake up

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/ 
digitalWrite(MS1, LOW); // Configures to Full Steps
digitalWrite(MS2, LOW); // Configures to Full Steps

pinMode(PinCLK,INPUT); // Set Pins to Input
pinMode(PinDT,INPUT); 
pinMode(PinSW,INPUT);
digitalWrite(PinSW,INPUT_PULLUP); // use internal resistor of UNO for Rotary Encoder switch
attachInterrupt (0,rotarydetect,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}


void loop () {

if (!(digitalRead(PinSW))) { // Do if Rotary Encoder switch is pressed
delay(250); // debounce switch
if (rswitch == 0) {
rswitch = 1; }
else {
rswitch = 0; }
}

// Runs if Rotary Encoder switch is pressed
if (led_speed_change) {
led_speed_change = false; // do Not repeat IF loop until new rotation detected

// Which LED's to light up
FastLED.clear();
for(int x = 0; x != (StepsToTake - 1); x++) {
if (x < 2) leds[x] = CRGB::Red; if (x > 1 & x < 5) leds[x] = CRGB::Orange; if (x > 4) leds[x] = CRGB::Green; }
FastLED.setBrightness(50);
FastLED.show();
}

// Runs if rotation was detected
if (TurnDetected) {
TurnDetected = false; // do NOT repeat IF loop until new rotation detected

// Which direction to move Stepper motor
if (rotationdirection) { // Move motor CCW
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1); 
}
}

if (!rotationdirection) { // Move motor CW
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW); 
delay(1); 
}
}
}
}

/* End of Code */


 

TUTORIAL VIDEO




 

DOWNLOAD


Just copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.


Used libraries:

You can download the FastLED library here


Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

3,184 views0 comments

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page