-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stepper_driver.ino
286 lines (247 loc) · 11 KB
/
Stepper_driver.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* This code was written to control a 'two phase bipolar' stepper motor (type number: STH-3D1126-02),
with driver PBL3775 from an HP C5110A flatbed scanner.
* --------------------------------------------------------------------------------
Copyright (C) 2017 F. Ramakers.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For info, mail: framakers@hotmail.com (feb 2017).
* --------------------------------------------------------------------------------
* WARNING:
* Do everything at your own risk. I do not accept any resposibility. I did not add warnings to this text, if you need them then this project is not for you.
* If you hate lists of warnings in projects because you know how to keep yourself safe, and if you agree you are responsible for your own actions: have fun with it!
*
* DRIVER MODES
* The driver supports 3 modes:
* 1) Full step mode (implemented)
* 2) Half step mode (implemented)
* 3) Modified half step mode (NOT implemented. I don't know how to do this (feed 140% of 5V to the driver?). Would love to hear from you, if you do know how: framakers@hotmail.com)
*
* MORE INFO:
* See "README.TXT for more info!
* PHASE: direction of current through spool. HIGH = one direction, LOW = opposite direction.
* Disable: no current through spool. HIGH = spool disabled; LOW enabled.
* VRef = reference voltage.
*
* CREDITS/ FEEDBACK:
* This code was written by F. Ramakers.
* Feedback welcome, feel free to mail: framakers@hotmail.com (feb 2017).
* Github version 1.0 (V3.0 eigen nummering).
*/
const int ledPin = 13; // LED from scanner attached to this pin
const int buttonPin = 12; // push button (signal wire) from scanner attached to this pin
const int disable1Pin = 3; // connect to pin 10 (of the 3775 driver IC)
const int disable2Pin = 4; // connect to 13
const int phase1Pin = 5; // connect to 9
const int phase2Pin = 6; // connect to 14
const int vRef1Pin = 7; // connect to 7
const int vRef2Pin = 8; // connect to 16
int tellerFullStepMode = 4; // step counter fullStep mode
int tellerHalfStepMode = 8; // step counter halfStep mode
int buttonState = 0; // 0 = button not pressed, 1 = button pressed.
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(disable1Pin, OUTPUT);
pinMode(disable2Pin, OUTPUT);
pinMode(phase1Pin, OUTPUT);
pinMode(phase2Pin, OUTPUT);
pinMode(vRef1Pin, OUTPUT);
pinMode(vRef2Pin, OUTPUT);
startPosition(); // Do digitalWrite for all 6 pins, so current output (HIGH/ LOW) is known.
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, 1);
delay(200);
digitalWrite(ledPin, 0);
delay(200);
}
// TEST RUN
fullStep(200, 4);
delay(500);
halfStep(400, 4);
delay(500);
fullStepBack(200, 4);
delay(500);
halfStepBack(400, 4);
delay(500);
}
void loop() {
/* STEPPER METHODS:
* fullStep(numberOfSteps, delayBetweenSteps); run in full step mode, forward
* halfStep(numberOfSteps, delayBetweenSteps); run in half step mode, forward
* fullStepBack(int numberOfSteps, int delayBetweenSteps); run in full step mode, backwards (oppsite direction of 'fullStep')
* halfStepBack(int numberOfSteps, int delayBetweenSteps); run in half step mode, backwards (oppsite direction of 'fullStep')
*
* 200 full steps = 400 half steps = 1 rotation
* 'delayBetweenSteps' is in milliseconds
* bigger 'delayBetweenSteps' = slower speed.
*
* EXAMPLES:
* fullStep(200, 10); // parameters: numberOfSteps, delayBetweenSteps
* halfStepBack(400, 10); // parameters: numberOfSteps, delayBetweenSteps
*
*/
// RESPOND TO BUTTON
buttonState = digitalRead(buttonPin);
if (buttonState == 1) { // 1 = button is pressed
halfStep(1, 5);
} else {
halfStepBack(1, 5);
}
// delay(5); // added for safety, for first test. Delay can be removed if all goes well.
}
void fullStep(int numberOfSteps, int delayBetweenSteps) { // FULL STEP MODE
digitalWrite(disable1Pin, LOW); // disabled is always low in Full Step Mode.
digitalWrite(disable2Pin, LOW);
digitalWrite(ledPin, !digitalRead(ledPin)); // led toggle (turns LED on and off with each step)
for (int i = 0; i < numberOfSteps ; i++) { // counting makes sure the stepper will continue from last step it took (important if step method is called >1 time)
if (tellerFullStepMode < 4) {
tellerFullStepMode++;
} else {
tellerFullStepMode = 1;
}
if (tellerFullStepMode == 1) { // step 1
digitalWrite(phase1Pin, HIGH);
//digitalWrite(phase2Pin, HIGH);
} else if (tellerFullStepMode == 2) { // step 2
//digitalWrite(phase1Pin, HIGH);
digitalWrite(phase2Pin, LOW);
} else if (tellerFullStepMode == 3) { // step 3
digitalWrite(phase1Pin, LOW);
//digitalWrite(phase2Pin, LOW);
} else { // step 4
//digitalWrite(phase1Pin, LOW);
digitalWrite(phase2Pin, HIGH);
}
delay(delayBetweenSteps); // this delay determines the speed of the motor
}
}
void fullStepBack(int numberOfSteps, int delayBetweenSteps) { // FULL STEP MODE, OPPOSITE DIRECTION
digitalWrite(disable1Pin, LOW); // disabled is always low in Full Step Mode.
digitalWrite(disable2Pin, LOW);
digitalWrite(ledPin, !digitalRead(ledPin)); // led toggle (turns LED on and off with each step)
for (int i = 0; i < numberOfSteps ; i++) { // counting backwards, makes the steps go in reversed order. This will make the motor spin in the opposite direction.
if (tellerFullStepMode > 1) {
tellerFullStepMode--;
} else {
tellerFullStepMode = 4;
}
if (tellerFullStepMode == 1) { // step 1
digitalWrite(phase1Pin, HIGH);
//digitalWrite(phase2Pin, HIGH);
} else if (tellerFullStepMode == 2) { // step 2
//digitalWrite(phase1Pin, HIGH);
digitalWrite(phase2Pin, LOW);
} else if (tellerFullStepMode == 3) { // step 3
digitalWrite(phase1Pin, LOW);
//digitalWrite(phase2Pin, LOW);
} else { // step 4
//digitalWrite(phase1Pin, LOW);
digitalWrite(phase2Pin, HIGH);
}
delay(delayBetweenSteps); // this delay determines the speed of the motor
}
}
void halfStep(int numberOfSteps, int delayBetweenSteps) { // HALF STEP MODE
digitalWrite(ledPin, !digitalRead(ledPin)); // led toggle (aan en uit)
for (int i = 0; i < numberOfSteps ; i++) {
// snelheid *= 2 // twice the speed, because twice as many steps as in full step mode
if (tellerHalfStepMode < 8) { // counting makes sure the stepper will continue from last step it took (important if step method is called >1 time)
tellerHalfStepMode++;
} else {
tellerHalfStepMode = 1;
}
if (tellerHalfStepMode == 1) { // step 1a
digitalWrite(phase1Pin, HIGH);
digitalWrite(disable1Pin, HIGH);
digitalWrite(phase2Pin, HIGH);
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 2) { // step 1b
digitalWrite(disable1Pin, LOW);
} else if (tellerHalfStepMode == 3) { // step 2a
digitalWrite(phase1Pin, HIGH);
digitalWrite(disable1Pin, LOW);
digitalWrite(phase2Pin, LOW);
digitalWrite(disable2Pin, HIGH);
} else if (tellerHalfStepMode == 4) { // step 2b
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 5) { // step 3a
digitalWrite(phase1Pin, LOW);
digitalWrite(disable1Pin, HIGH);
digitalWrite(phase2Pin, LOW);
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 6) { // step 3b
digitalWrite(disable1Pin, LOW);
} else if (tellerHalfStepMode == 7) { // step 4a
digitalWrite(phase1Pin, LOW);
digitalWrite(disable1Pin, LOW);
digitalWrite(phase2Pin, HIGH);
digitalWrite(disable2Pin, HIGH);
} else { // step 4b
digitalWrite(disable2Pin, LOW);
}
delay(delayBetweenSteps); // this delay determines the speed of the motor
}
}
void halfStepBack(int numberOfSteps, int delayBetweenSteps) { // HALF STEP MODE, OPPOSITE DIRECTION
digitalWrite(ledPin, !digitalRead(ledPin)); // led toggle (aan en uit)
for (int i = 0; i < numberOfSteps ; i++) {
// snelheid *= 2 // twice the speed, because twice as many steps as in full step mode
if (tellerHalfStepMode > 1) { // counting backwards, makes the steps go in reversed order. This will make the motor spin in the opposite direction.
tellerHalfStepMode--;
} else {
tellerHalfStepMode = 8;
}
if (tellerHalfStepMode == 1) { // step 1a
digitalWrite(phase1Pin, HIGH);
digitalWrite(disable1Pin, HIGH);
digitalWrite(phase2Pin, HIGH);
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 2) { // step 1b
digitalWrite(disable1Pin, LOW);
} else if (tellerHalfStepMode == 3) { // step 2a
digitalWrite(phase1Pin, HIGH);
digitalWrite(disable1Pin, LOW);
digitalWrite(phase2Pin, LOW);
digitalWrite(disable2Pin, HIGH);
} else if (tellerHalfStepMode == 4) { // step 2b
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 5) { // step 3a
digitalWrite(phase1Pin, LOW);
digitalWrite(disable1Pin, HIGH);
digitalWrite(phase2Pin, LOW);
digitalWrite(disable2Pin, LOW);
} else if (tellerHalfStepMode == 6) { // step 3b
digitalWrite(disable1Pin, LOW);
} else if (tellerHalfStepMode == 7) { // step 4a
digitalWrite(phase1Pin, LOW);
digitalWrite(disable1Pin, LOW);
digitalWrite(phase2Pin, HIGH);
digitalWrite(disable2Pin, HIGH);
} else { // step 4b
digitalWrite(disable2Pin, LOW);
}
delay(delayBetweenSteps); // this delay determines the speed of the motor
}
}
void disableStepper() {
digitalWrite(disable1Pin, HIGH); // disable high = motor (spool) turned OFF.
digitalWrite(disable2Pin, HIGH);
}
void startPosition() {
digitalWrite(disable1Pin, HIGH); // disable high = motor (spool) turned OFF.
digitalWrite(disable2Pin, HIGH);
digitalWrite(phase1Pin, HIGH);// phase high: because this is the state needed for the first step the stepper needs to take
digitalWrite(phase2Pin, HIGH);
// vRef is always set to HIGH (100%) in "Full Step Mode" and "Half Step Mode".
// Note: in "modified half step mode, it is set to 140%, I don't know if this is possible with Arduino.
digitalWrite(vRef1Pin, HIGH); // vRef pin is always HIGH.
digitalWrite(vRef2Pin, HIGH);
}