filmov
tv
Control a Stepper Motor using an Arduino and a Rotary Encoder - Tutorial - Part 1

Показать описание
n this first part of controlling a Stepper Motor with a Rotary Encoder, we will use the 28BYJ-48 stepper with the included ULN2003 driver board. These little steppers a cheap and good to start learning about using stepper motors with an Arduino.
In Part 2, we will use a Nema 17 motor with the EasyDriver board instead.
here is the code:
#include "Stepper.h"
#define STEPS 32
volatile boolean TurnDetected;
volatile boolean rotationdirection;
const int PinCLK=2;
const int PinDT=3;
const int PinSW=4;
int RotaryPosition=0;
int PrevPosition;
int StepsToTake;
Stepper small_stepper(STEPS, 8, 10, 9, 11);
void isr () {
delay(4);
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup() {
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH);
attachInterrupt (0,isr,FALLING);
}
void loop() {
if(!(digitalRead(PinSW))) {
if(RotaryPosition == 0) {
} else {
RotaryPosition=0;
}
}
if (TurnDetected) {
PrevPosition = RotaryPosition;
if (rotationdirection) {
RotaryPosition=RotaryPosition-1;}
else {
RotaryPosition=RotaryPosition+1;}
TurnDetected = false;
if ((PrevPosition +1) == RotaryPosition) {
StepsToTake=50;
}
if ((RotaryPosition +1) == PrevPosition) {
StepsToTake=-50;
}
}
}
Thanks you for watching, leave comment or a like and subscribe!
In Part 2, we will use a Nema 17 motor with the EasyDriver board instead.
here is the code:
#include "Stepper.h"
#define STEPS 32
volatile boolean TurnDetected;
volatile boolean rotationdirection;
const int PinCLK=2;
const int PinDT=3;
const int PinSW=4;
int RotaryPosition=0;
int PrevPosition;
int StepsToTake;
Stepper small_stepper(STEPS, 8, 10, 9, 11);
void isr () {
delay(4);
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup() {
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH);
attachInterrupt (0,isr,FALLING);
}
void loop() {
if(!(digitalRead(PinSW))) {
if(RotaryPosition == 0) {
} else {
RotaryPosition=0;
}
}
if (TurnDetected) {
PrevPosition = RotaryPosition;
if (rotationdirection) {
RotaryPosition=RotaryPosition-1;}
else {
RotaryPosition=RotaryPosition+1;}
TurnDetected = false;
if ((PrevPosition +1) == RotaryPosition) {
StepsToTake=50;
}
if ((RotaryPosition +1) == PrevPosition) {
StepsToTake=-50;
}
}
}
Thanks you for watching, leave comment or a like and subscribe!