Using Stepper Motor As Rotary Encoder

13,937

19

12

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

  • Backyard Contest

    Backyard Contest
  • Make it Fly Challenge

    Make it Fly Challenge
  • Fandom Contest

    Fandom Contest

12 Comments

0
BryanRezende
BryanRezende

9 months ago

Thanks for the write up! Will the hardware work the same for a raspberry pi? The logic is 3.3v instead of 5v. Also, the libraries. Anyone aware of equivalents for rpi or someway to run arduino code through it somehow? Cheers

0
BryanRezende
BryanRezende

Reply 9 months ago

Ok so seems like this "how to" solves the translation issue:
https://www.deviceplus.com/raspberry-pi/how-to-run...

Still wondering about hardware. I'll probably try it out as is and let you all know once I'm done setting the breadboard up, but any advice in advance would be appreciated.

0
BryanRezende
BryanRezende

Reply 9 months ago

Ok, I found a script that was actually made for the RPi and it worked great: https://github.com/modmypi/Rotary-Encoder/blob/mas...
Actually, I had to edit out the line where it tells the program to sleep, but then then it worked fine.

Using 3.3v as source, I'm having issues with steps being missed if the shaft is being turned too slow, or fast—there's a sweet spot which is reliable. Also, the position value bounces around sometimes when the shaft is stationary. Using 5v as source produced more reliable results. But of course I'm nervous that could cause the RPi to fail early. Any advice for tuning the setup for 3.3v?

0
eivor.v.gunnarsson

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

0
Bastelkönig
Bastelkönig

Reply 1 year 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 1 year 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

1 year ago

Cool project! Tried it and works great.

0
acheide
acheide

3 years ago

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

0
KushagraK7
KushagraK7

Reply 1 year 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 2 years 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

3 years ago

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

0
NeoStallion
NeoStallion

Tip 3 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.