GTranslate

Another motor that is quite nice to use for accurate positioning is a stepper motor. Steppers have many discrete coils that allow them to step through the motor coils and you can use a counter subroutine to move the steppers in step.

This could allow you to count the number of steps to turn the motor one time for example. With a motor that steps 1.8%, you would need to have 200 steps to go one full revolution.

Since the motors are accurate you can also tell them to go 50 steps for example and that would be 1/4 of a 360-degree turn. The challenge with steppers is they have not feedback and if you are holding the motor and it does not turn, while advancing the code telling it to turn, the motor or the program may not know it has not turned.

Notice the part number below is for a ULN2003 versus a ULN2803 and either part will work though pay attention to the extra pins on the ULN2803 and account for them in your design if that is what you are using.

Also, note that the supply voltage is 12 volts and the GND rail is also tied to that power supply. It is important to tie that GND back to the GND rail of the Arduino you use to create the stepping code for this stepper motor.

Code to drive the stepper motor using Maxuino.

You can purchase stepper motors from Sparkfun or Pololu motors or small ones that run at 5-volts. The Stepper Motor 28BYJ-48 are inexpensive and relatively easy to control with the Arduino IDE directly. Here is a nice tutorial on Instructables on how to use these motors and they can be purchased online from Amazon for amazingly low prices.

MAX MSP lessons continue in Chapter 13

This can also be controlled with the Arduino code directly

below: By manipulating the delay(2000);
Direction=!Direction;  steps_left=4095; values you can change the rotation amount and pause between rotation.

/* make BYJ-48 step motor rotating clockwise and counter clockwise
* project tutorial see http://osoyoo.com/?p=201
*/
#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);
//BYJ48 Stepper Motor
}
void loop()
{
while(steps_left>0){
currentMillis = micros();
if(currentMillis-last_time>=1000){
stepper(1);
time=time+micros()-last_time;
last_time=micros();
steps_left--;
}
}
Serial.println(time);
Serial.println("Wait...!");
delay(2000);
Direction=!Direction;
steps_left=4095;
}
void stepper(int xw){
for (int x=0;x<xw;x++){
switch(Steps){
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}