Using Stepper Motor As Rotary Encoder

10,407

14

9

Introduction: Using Stepper Motor As Rotary Encoder

I want to tell you how to make incremental encoder from stepper motor. When we turning shaft of stepper motor it works like generator. It generates certain impulses on its coils. After some signal processing, we get same impulses as incremental encoder.

This encoder has one problem, it can drop steps if you turning very slowly. But for many applications, it doesn't matter.

Step 1: Circuit

Impulses from stepper motor turn on/off this triggers and on output we get signal like rotary encoder.


Step 2: Demonstration

In this video shown how encoder works with arduino. In this example, I used stepper motors from old printer but you can use any stepper motors.

Code of these examples you can find here:

Stepper motor example;

Neopixel example;

Necessary libraries:
Encoder.h

Adafruit_NeoPixel.h

Circuit diagram with stepper motor and driver a4988:

https://drive.google.com/file/d/1GCRXKmik49xVYwpirRgTCWHo17r8T0BW/view?usp=sharing

I'm currently freelancer. For hiring, you can contact me at about.prototype@gmail.com

3 People Made This Project!

Recommendations

  • Puzzles Speed Challenge

    Puzzles Speed Challenge
  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020

9 Discussions

0
eivor.v.gunnarsson
eivor.v.gunnarsson

6 weeks ago on Step 2

The code URLs isn't working, "Hmm. We’re having trouble finding that site."

0
Bastelkönig
Bastelkönig

Reply 6 weeks ago

neopixel example:

//**************begin***************
#include <Encoder.h>
#include <Adafruit_NeoPixel.h>
//Conncet Led ring DI - 7pin
#define PIN 7
#define NUMPIXELS 16
int MaxPixelID = NUMPIXELS - 1; //because first pixel is 0
volatile byte mybrightness = 36; //max brightness when turning the knob
volatile byte lowbrightness = 2; //initial brightness
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Encoder myEnc(2, 3);
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(2, HIGH); //turn pullup resistor on
digitalWrite(3, HIGH); //turn pullup resistor on
pixels.setBrightness(lowbrightness);
pixels.begin();
}
long newPosition;
int pixelPosition = 0;
void loop() {
newPosition = myEnc.read();
pixelPosition = newPosition % 24;
Serial.println(newPosition);
if (pixelPosition < 0) {
pixelPosition = 24 + pixelPosition;
}
pixels.setPixelColor(pixelPosition, pixels.Color(0, 255, 0));
pixels.show();
delay(100);
pixels.setPixelColor(pixelPosition, pixels.Color(0, 0, 0));
pixels.show();
}
//*****************end************************

0
Bastelkönig
Bastelkönig

Reply 6 weeks ago

stepper-motor example:

//**************begin***************
#include <Encoder.h>
#define dir 12 // Stepping
#define stp 13 // Direction
Encoder myEnc(3, 2);
void setup() {
pinMode(dir, OUTPUT);
pinMode(stp, OUTPUT);
}
int newPosition = 0;
int oldPosition = 0;
int diff = 0;
void loop() {
oldPosition = newPosition;
newPosition = myEnc.read();
diff = newPosition - oldPosition;
if (diff != 0) {
if (diff > 0) {
digitalWrite(dir, HIGH);
} else {
digitalWrite(dir, LOW);
}
diff = abs(diff);
for (int i = 0; i < diff; i++) {
digitalWrite(stp, HIGH);
delay(50);
digitalWrite(stp, LOW);
}
}
}
//*****************end************************
0
KushagraK7
KushagraK7

10 months ago

Cool project! Tried it and works great.

0
acheide
acheide

2 years ago

Now my small scavenged steppers have a purpose. To post the code here would complete the Instructable. Thanks.

0
KushagraK7
KushagraK7

Reply 10 months ago

The two outputs labeled 'Phase A' and 'Phase B' are just like the 'CLK' and 'DATA' pins of a regular rotary encoder which you can be used with Arduino. Search for any Arduino rotary encoder project and upload it on your Arduino board. connect the previously mentioned output pins to the digital input pins mentioned in the code.
You can find an example code here:
https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/

0
ruediheimlicher
ruediheimlicher

Question 1 year ago on Introduction

I tried the circuit and it works, but the output of the op amps after a turn on the stepper are not always on the same logic level. Both high, both low, or one high, one high, not predictable. Any Ideas?

IMG_3533.jpg
0
Pierrick
Pierrick

2 years ago

Interesting project. Same question as NeoStallion, are you able to detect a step slippage during motion?

0
NeoStallion
NeoStallion

Tip 2 years ago

Can that Stepper motor also be rotated in a normal fashion ? and know the exact location where it stopped. because in many practical applications I need to know where it is now and rotate the same.