This L293D Stepper motor controller lets you precisely control a stepper motor with just two signal lines.
void speed_control(){ int val = analogRead(pot_pin); val -= 512; val /= 5; stepper.setSpeed(abs(val)); int step_amount = 0; if ( val > 0) step_amount = 1; else if ( val < 0) step_amount = -1; stepper.step(step_amount); }
First it reads the potentiometer value which is going to be a number between 0 and 1024 and put it in the variable val. Then we subtract 512 so that range will be -512 to +512. We divide this value by 5 (so the motor wont spin too fast) and now the range is about -102 to +102. Now we set the speed of the stepper motor to the absolute value of val which is a number between 0 and 102. So if the potentiometer is exactly in the middle the motors speed will be zero, and as the pot spins towards either extreme the motor will spin faster. Now we set the step amount to either -1 or +1 depending on the sign of val. This will control the direction the motor is spinning. The stepper.step() function is a blocking function meaning nothing can happen after it is called until the motor completes the step. So it is good to step small amount to keep your code more responsive.
void phase_control(){ int val = analogRead(pot_pin); int degree = map(val,0,1023,0,200); int step_amount = 0; if (cur_pos < degree && cur_pos < 200) step_amount = 1; else if(cur_pos > degree && cur_pos > 0) step_amount = -1; stepper.step(step_amount); cur_pos += step_amount; }
Again we start by reading the potentiometer value in, but now we map this value to a number between 0 and 200. We use 200 because that is the number of steps of our particular motor. The smallest step is 1.8 degrees (1.8*200 = 360). Now we check if the cur_pos variable, which stores the current angle of the stepper. If cur_pos is less than the degree than we set the step amount to be positive to turn towards the degree heading. if cur_pos is bigger than degree then we set the step_amount to -1 to turn back towards degree. If cur_pos is equal to degree then we leave step_amount equal to 0 and don't turn at all (we are already pointing where the potentiometer directed). Lastly, we update the cur_pos variable to reflect the new position.
/* LucidTronix Control a stepper motor. * For instructions details and schematic, See: * http://www.lucidtronix.com/tutorials/24 */#include// change this to the number of steps on your motor#define STEPS 200// create an instance of the stepper class, specifying// the number of steps of the motor and the pins it's// attached to:Stepper stepper(STEPS, 2,3);int btn1 = 4;int pot_pin = 0;int mode = 0;int num_modes = 2;int cur_pos = 100;unsigned long last_press = 0;void setup(){ // set the speed of the motor to 30 RPMs stepper.setSpeed(30); pinMode(btn1, INPUT);}void loop(){ if (mode == 0 ) speed_control(); else if (mode == 1) phase_control(); //check_buttons();}void check_buttons(){ if (digitalRead(btn1) == HIGH && millis() - last_press > 500){ mode = ++mode % num_modes; last_press = millis(); stepper.setSpeed(30); }}void speed_control(){ int val = analogRead(pot_pin); val -= 512; val /= 5; stepper.setSpeed(abs(val)); int step_amount = 0; if ( val > 0) step_amount = 1; else if ( val < 0) step_amount = -1; stepper.step(step_amount); }void phase_control(){ int val = analogRead(pot_pin); int degree = map(val,0,1023,0,200); int step_amount = 0; if (cur_pos < degree && cur_pos < 200) step_amount = 1; else if(cur_pos > degree && cur_pos > 0) step_amount = -1; stepper.step(step_amount); cur_pos += step_amount; }
| Title | Description | # | Cost | Link | Picture |
| PCB L293 Breakout | Stepper motor control with just two control lines. | 1 | $4.0 | Link |
|
| L293 | IC DRVR P/P 4CH W/DIODES 20-SOIC L293D(D) 4.5 V ~ 36 V 1.2A PMIC - MOSFET, Bridge Drivers - Internal SwitchValue:4.5 V ~ 36 V | 1 | $3.63 | Link |
|
| Transistor | Darlington BJT NPN 30V SOT23-3Value:NPN | 2 | $0.24 | Link |
|
| Capacitor | CAP ALUM 100UF 25V 20% SMDValue:100µF | 1 | $0.21 | Link |
|
| Resistor | Chip Resistor - Surface Mount RES 10K OHM 1/8W 5% 0805 SMDValue: 10k | 2 | $0.1 | Link |
|
Comments: