├── README.adoc ├── examples └── Stepper2_oneTurn │ └── stepper_oneRevolution.ino ├── keywords.txt ├── library.properties └── src ├── Stepper2.cpp └── Stepper2.h /README.adoc: -------------------------------------------------------------------------------- 1 | = Stepper2 Library for Arduino IDE = 2 | 3 | This library allows you to control cheap 28BYJ-48 stepper motors. 4 | 5 | For more information about this library please visit us at 6 | https://github.com/udivankin/Stepper2 7 | 8 | == License == 9 | 10 | Copyright (c) Alex Udivankin. All right reserved. 11 | 12 | This library is free software; you can redistribute it and/or 13 | modify it under the terms of the GNU Lesser General Public 14 | License as published by the Free Software Foundation; either 15 | version 2.1 of the License, or (at your option) any later version. 16 | 17 | This library is distributed in the hope that it will be useful, 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | Lesser General Public License for more details. 21 | 22 | You should have received a copy of the GNU Lesser General Public 23 | License along with this library; if not, write to the Free Software 24 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 25 | -------------------------------------------------------------------------------- /examples/Stepper2_oneTurn/stepper_oneRevolution.ino: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Stepper Motor Control - one turn 4 | */ 5 | 6 | #include 7 | 8 | const int rpm = 15; // max rpm on 28BYJ-48 is ~15 9 | int pinOut[4] = { D1, D2, D5, D6 }; 10 | 11 | Stepper2 myStepper(pinOut); 12 | 13 | void setup() { 14 | Serial.begin(115200); 15 | myStepper.setSpeed(rpm); 16 | } 17 | 18 | void loop() { 19 | Serial.println("start"); 20 | myStepper.setDirection(0); // clock-wise 21 | myStepper.turn(); // one full turn 22 | myStepper.stop(); 23 | myStepper.setDirection(1); // counter-clock-wise 24 | myStepper.turn(3); // three full turns 25 | myStepper.stop(); 26 | delay(15 * 1000); 27 | } 28 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Test 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Stepper KEYWORD1 Stepper2 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | step KEYWORD2 16 | stop KEYWORD2 17 | setSpeed KEYWORD2 18 | version KEYWORD2 19 | 20 | ###################################### 21 | # Instances (KEYWORD2) 22 | ####################################### 23 | direction KEYWORD2 24 | speed KEYWORD2 25 | 26 | 27 | ####################################### 28 | # Constants (LITERAL1) 29 | ####################################### 30 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Stepper2 2 | version=0.0.1 3 | author=udivankin 4 | maintainer=allx 5 | sentence=Allows compatible boards to control a cheap 28BYJ-48 stepper motor. 6 | paragraph=This library allows you to control cheap 28BYJ-48 stepper motor. 7 | category=Device Control 8 | url=https://github.com/udivankin/Stepper2 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/Stepper2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Stepper2.h - BYJ48 stepper motor library for Wiring/Arduino - Version 0.0.1 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | * The circuits can be found at 19 | * 20 | * https://github.com/udivankin/Stepper2 21 | */ 22 | 23 | #include "Arduino.h" 24 | #include "Stepper2.h" 25 | 26 | const int microsInSecond = 1000L * 1000L; 27 | const int microsInMinute = 60L * microsInSecond; 28 | 29 | // Maximum steps per second 30 | const int maxHz = 1000; 31 | 32 | // Approx steps count to make a full turn 33 | const int stepsPerFullTurn = 4096; 34 | 35 | // Number of steps to make a rev of internal motor 36 | const int numberOfSteps = 64; 37 | 38 | // Step matrix 39 | int stepMatrix[8][4] = { 40 | { LOW, LOW, LOW, HIGH }, 41 | { LOW, LOW, HIGH, HIGH }, 42 | { LOW, LOW, HIGH, LOW }, 43 | { LOW, HIGH, HIGH, LOW }, 44 | { LOW, HIGH, LOW, LOW }, 45 | { HIGH, HIGH, LOW, LOW }, 46 | { HIGH, LOW, LOW, LOW }, 47 | { HIGH, LOW, LOW, HIGH }, 48 | }; 49 | 50 | // Output levels to stop the motor 51 | int allLows[4] = { LOW, LOW, LOW, LOW }; 52 | 53 | /* 54 | * Constructor for four-pin version 55 | * Sets which wires should control the motor. 56 | */ 57 | Stepper2::Stepper2(int motorPin[4]) 58 | { 59 | this->step_number = 0; // which step the motor is on 60 | this->last_step_time = 0; // time stamp in us of the last step taken 61 | 62 | // setup the pins on the microcontroller: 63 | for (int i = 0; i < 4; i++) { 64 | this->pin_matrix[i] = motorPin[i]; 65 | pinMode(motorPin[i], OUTPUT); 66 | } 67 | } 68 | 69 | /* 70 | * Sets the speed in revs per minute 71 | */ 72 | void Stepper2::setSpeed(int rpm) 73 | { 74 | int duration = microsInMinute / stepsPerFullTurn / rpm; 75 | int minDuration = microsInSecond / maxHz; 76 | this->step_duration = duration > minDuration ? duration : minDuration; 77 | } 78 | 79 | /* 80 | * Sets the speed in revs per minute 81 | */ 82 | void Stepper2::setDirection(int direction) 83 | { 84 | this->direction = direction; 85 | } 86 | 87 | /* 88 | * Write pin state to output 89 | */ 90 | void Stepper2::writeStep(int outArray[4]){ 91 | for (int i = 0; i < 4; i++) { 92 | digitalWrite(this->pin_matrix[i], outArray[i]); 93 | } 94 | } 95 | 96 | /* 97 | * Moves the motor stepsToMove steps. 98 | */ 99 | void Stepper2::step(int stepsToMove) 100 | { 101 | int stepsLeft = abs(stepsToMove); // how many steps to take 102 | 103 | // Adjust stepsToMove accrodign to direction 104 | if (this->direction == 1) { 105 | stepsToMove = -stepsToMove; 106 | } 107 | 108 | // decrement the number of steps, moving one step each time 109 | while (stepsLeft > 0) { 110 | unsigned long now = micros(); 111 | // move only if the appropriate delay has passed 112 | if (now - this->last_step_time >= this->step_duration) { 113 | // get the timeStamp of when you stepped 114 | this->last_step_time = now; 115 | // increment or decrement the step number, 116 | // depending on direction 117 | if (this->direction == 1) { 118 | this->step_number++; 119 | if (this->step_number == numberOfSteps) { 120 | this->step_number = 0; 121 | } 122 | } else { 123 | if (this->step_number == 0) { 124 | this->step_number = numberOfSteps; 125 | } 126 | this->step_number--; 127 | } 128 | // decrement the steps left 129 | stepsLeft--; 130 | // step the motor to step number 0..8 according to step matrix 131 | writeStep(stepMatrix[this->step_number % 8]); 132 | } 133 | } 134 | } 135 | 136 | /* 137 | * Stops the motor (otherwize it will draw between 100 and 200ma when stopped) 138 | */ 139 | void Stepper2::stop() 140 | { 141 | writeStep(allLows); 142 | } 143 | 144 | /* 145 | * Make one full turn 146 | */ 147 | void Stepper2::turn() 148 | { 149 | for (int i = 0; i < (stepsPerFullTurn / numberOfSteps); i++) { 150 | step(numberOfSteps); 151 | // Reset WDT timer, other way to do it on nodeMCU is; 152 | // ESP.wdtDisable(); 153 | // ESP.wdtEnable(WDTO_8S); 154 | delay(1); 155 | } 156 | } 157 | 158 | /* 159 | * Make given amount of full turns 160 | */ 161 | void Stepper2::turn(int turnsCount) 162 | { 163 | for (int i = 0; i < turnsCount; i++) { 164 | turn(); 165 | } 166 | } 167 | 168 | /* 169 | * version() returns the version of the library: 170 | */ 171 | int Stepper2::version(void) 172 | { 173 | return 1; 174 | } 175 | -------------------------------------------------------------------------------- /src/Stepper2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Stepper2.h - BYJ48 stepper motor library for Wiring/Arduino - Version 0.0.1 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | * The circuits can be found at 19 | * 20 | * https://github.com/udivankin/Stepper2 21 | */ 22 | 23 | // ensure this library description is only included once 24 | #ifndef Stepper2_h 25 | #define Stepper2_h 26 | 27 | // library interface description 28 | class Stepper2 { 29 | public: 30 | Stepper2(int motorPin[4]); // constructors 31 | void setSpeed(int rpm); // speed setter method 32 | void setDirection(int direction); // direction setter method 33 | void step(int numberOfSteps); // step method 34 | void turn(); // turn method 35 | void turn(int turnsCount); // turn method to given amount of turns 36 | void stop(); // stop method 37 | int version(); // version method 38 | 39 | private: 40 | void writeStep(int outArray[4]); // write step pin matrix to output 41 | int direction; // direction of rotation 42 | unsigned long step_duration; // target step duration, in ms, based on speed 43 | int step_number; // which step the motor is on 44 | int pin_matrix[4]; // pin matrix array 45 | unsigned long last_step_time; // time stamp in us of when the last step was taken 46 | }; 47 | 48 | #endif 49 | --------------------------------------------------------------------------------