Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Description:
- * This project is about a control of the 5V stepper motor with ULN2003 driver module and arduino uno.
- * When push a button the motor do a step. The motor move only a direction. the amount of steps is configurable from the code
- *
- * Circuit:
- * Arduino ULN2003
- * 8-------------> IN1
- * 9-------------> IN2
- * 10------------> IN3
- * 11------------> IN4
- *
- * 7 ------------> button
- */
- //definition de pins
- const int button = 7;
- const int motorPin1 = 8;
- const int motorPin2 = 9;
- const int motorPin3 = 10;
- const int motorPin4 = 11;
- //definition variables
- int motorSpeed = 1000; //speed of motor
- int stepCounter = 0; // count of steps
- int stepsPerRev = 1;
- //int stepsPerRev = 4076; // steps for a full turn
- //secuence 1-phase
- //const int numSteps = 4;
- //const int stepsLookup[4] = { B1000, B0100, B0010, B0001 };
- //secuence 2-phases
- //const int numSteps = 4;
- //const int stepsLookup[4] = { B1100, B0110, B0011, B1001 };
- //secuence half phase
- const int numSteps = 8;
- const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };
- void setup()
- {
- //pins output
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motorPin3, OUTPUT);
- pinMode(motorPin4, OUTPUT);
- //pin input
- pinMode(button,INPUT);
- }
- void loop()
- {
- if(digitalRead(button)==HIGH){
- for (int i = 0; i < stepsPerRev; i++){
- clockwise();
- delay(motorSpeed);
- }
- }
- delay(200);
- }
- void clockwise()
- {
- if (stepCounter >= numSteps) stepCounter = 0;
- setOutput(stepCounter);
- stepCounter++;
- }
- void setOutput(int steps)
- {
- digitalWrite(motorPin1, bitRead(stepsLookup[steps], 0));
- digitalWrite(motorPin2, bitRead(stepsLookup[steps], 1));
- digitalWrite(motorPin3, bitRead(stepsLookup[steps], 2));
- digitalWrite(motorPin4, bitRead(stepsLookup[steps], 3));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement