Optic encoded precise step motor (3D printed)
Angle control code
We know that each 6400 steps we have a full rotation because the encoding disc has 32 steps/rot and we use a 1:200 reduction worm gear. To control the direction of the rotation we use an H bridge. Connect pins 3 and 5 to the direction control of the H bridge and upload this code. The sensor pin is still D2.
int sensor = 2;
int steps=0; //initial steps value is 0
int previous_step=0;
////////////////////////////Choose the angle//////////
int angle=90; //in degree
//////////////////////////////////////////////////////
int right=3; //PWM pins for the H bridge
int left=5;
int stepsToDo = (6400/360)*angle;
void setup() {
pinMode(sensor,INPUT); //define the pin as input
}
void loop() {
/////////////SPIN TO Right////////////
while (steps < stepsToDo)
{
digitalWrite(left,HIGH);
digitalWrite(right,LOW);
if (digitalRead(sensor)==0 && previous_step==1)
{
steps=steps+1;
previous_step=0;
}
if (digitalRead(sensor)==1 && previous_step==0)
{
steps=steps+1;
previous_step=1;
}
}
digitalWrite(left,LOW);
digitalWrite(right,LOW);
steps=0; //reset the steps
/////////////SPIN TO LEFT////////////
while (steps < stepsToDo)
{
digitalWrite(left,LOW);
digitalWrite(right,HIGH);
if (digitalRead(sensor)==0 && previous_step==1)
{
steps=steps+1;
previous_step=0;
}
if (digitalRead(sensor)==1 && previous_step==0)
{
steps=steps+1;
previous_step=1;
}
}
digitalWrite(left,LOW);
digitalWrite(right,LOW);
steps=0; //reset the steps
}
Now that we tested the circuit we can build the case of the optic encoded step motor. Let's go to the next page and start the 3D printed case building.