You are on page 1of 10

P10:ANTARMUKAMOTORDC

P10.1SERVO

P10.2STEPPER

A.Controllingposition(sweep)

A.OneStepataTime

#include<Servo.h>

/*
StepperMotorControlonestepatatime
Servomyservo;//createservoobjecttocontrolaservo

intangle=0;//variabletostoretheservoposition
Thisprogramdrivesaunipolarorbipolarsteppermotor.
Themotorisattachedtodigitalpins811oftheArduino.
voidsetup()

{
Themotorwillsteponestepatatime,veryslowly.
myservo.attach(9);//attachestheservoonpin9totheservoobject Youcanusethistotestthatyou'vegotthefourwireswiredtothe
}
correctpins.Ifwiredcorrectly,allstepsshouldbeinthesame
direction.
voidloop()

{
Usethisalsotocountthenumberofstepsperrevolutionofyour
for(angle=0;angle<180;angle+=1)//goesfrom0to180degrees motor,ifyoudon'tknowit.Thenplugthatnumberintothe
{//instepsof1degree
oneRevolutionexampletoseeifyougotitright.
myservo.write(angle);//tellservotogotopositioninvar'angle' */
delay(20);//waits20msbetweenservocommands
}
#include<Stepper.h>
for(angle=180;angle>=1;angle=1)//goesfrom180to0degrees
{
constintstepsPerRevolution=200;//changethistofitthe
myservo.write(angle);//moveservoinoppositedirection
//ofstepsperrevolutionforyourmotor
delay(20);//waits20msbetweenservocommands
continued

continued
Page1of10|P10AntarmukaMotorDC

}
}

B.Controllingwithpotentiometer(Knob)

//initializethestepperlibraryonpins8through11:
SteppermyStepper(stepsPerRevolution,8,9,10,11);

#include<Servo.h>

voidsetup(){
//initializetheserialport:
Serial.begin(9600);
}

Servomyservo;//createservoobjecttocontrolaservo

intstepCount=0;//numberofstepsthemotorhastaken

intpotpin=0;//analogpinusedtoconnectthepotentiometer
intval;//variabletoreadthevaluefromtheanalogpin

voidloop(){
//steponestep:
voidsetup()
myStepper.step(1);
{
Serial.print("steps:");
myservo.attach(9);//attachestheservoonpin9totheservoobject Serial.println(stepCount);
}
stepCount++;
delay(500);
voidloop()
}
{
val=analogRead(potpin);//readsthevalueofthepotentiometer B.OneRevolution
val=map(val,0,1023,0,180);//scaleittouseitwiththeservo
myservo.write(val);//setspositiontothescaledvalue
/*
delay(15);//waitsfortheservotogetthere
StepperMotorControlonerevolution
}

Thisprogramdrivesaunipolarorbipolarsteppermotor.
C.Controllingthespeedof2servos
Themotorisattachedtodigitalpins811oftheArduino.

#include<Servo.h>
Themotorshouldrevolveonerevolutioninonedirection,then
onerevolutionintheotherdirection.
continued
continued

Page2of10|P10AntarmukaMotorDC

ServomyservoLeft;//createservoobjecttocontrolaservo
ServomyservoRight;//createservoobjecttocontrolaservo

*/
#include<Stepper.h>

intangle=0;//variabletostoretheservoposition

constintstepsPerRevolution=200;//changethistofitthenumber
//ofstepsperrevolutionforyourmotor

voidsetup()
{
//initializethestepperlibraryonpins8through11:
myservoLeft.attach(9);//attachleftservoonpin9toservoobj
SteppermyStepper(stepsPerRevolution,8,9,10,11);
myservoRight.attach(10);//attachrightservoonpin10toservoobj
}
voidsetup(){
//setthespeedat60rpm:
voidloop()
myStepper.setSpeed(60);
{
//initializetheserialport:
for(angle=90;angle<180;angle+=1)//goesfrom90to180deg
Serial.begin(9600);
{//instepsof1degree
}
//90degreesisstopped
myservoLeft.write(angle);//rotateservoatspeedgivenby'angle' voidloop(){
myservoRight.write(180angle);//gointheoppositedirection
//steponerevolutioninonedirection:
Serial.println("clockwise");
delay(20);//waits20msbetweenservocommands
myStepper.step(stepsPerRevolution);
}
delay(500);
for(angle=180;angle>=90;angle=1)//goesfrom180to90deg

{
//steponerevolutionintheotherdirection:
myservoLeft.write(angle);//rotateataspeedgivenby'angle' Serial.println("counterclockwise");
myservoRight.write(180angle);//otherservogoesinoppositedir myStepper.step(stepsPerRevolution);
}
delay(500);
}
}

next

Page3of10|P10AntarmukaMotorDC

C.Speedcontrol
/*
StepperMotorControlspeedcontrol

Thisprogramdrivesaunipolarorbipolarsteppermotor.
Themotorisattachedtodigitalpins811oftheArduino.
Apotentiometerisconnectedtoanaloginput0.

Themotorwillrotateinaclockwisedirection.Thehigherthe
potentiometervalue,thefasterthemotorspeed.BecausesetSpeed()
setsthedelaybetweensteps,youmaynoticethemotorisless
responsivetochangesinthesensorvalueatlowspeeds.

*/
#include<Stepper.h>
constintstepsPerRevolution=200;//changethistofitthenumber
//ofstepsperrevolutionforyourmotor

//initializethestepperlibraryonpins8through11:
SteppermyStepper(stepsPerRevolution,8,9,10,11);
intstepCount=0;//numberofstepsthemotorhastaken
voidsetup(){
//nothingtodoinsidethesetup
continued

Page4of10|P10AntarmukaMotorDC

}
voidloop(){
//readthesensorvalue:
intsensorReading=analogRead(A0);
//mapittoarangefrom0to100:
intmotorSpeed=map(sensorReading,0,1023,0,100);
//setthemotorspeed:
if(motorSpeed>0){
myStepper.setSpeed(motorSpeed);
//step1/100ofarevolution:
myStepper.step(stepsPerRevolution/100);
}
}

P10.3MOTORSHIELD
///ThismotorshieldusePin6,5,7,4tocontrolthemotor
//SimplyconnectyourmotorstoM1+,M1,M2+,M2
//UploadthecodetoArduino
//Throughserialmonitor,type'a','s','w','d','x'tocontrolthemotor

intEN1=6;
intEN2=5;
intIN1=7;
intIN2=4;

Page5of10|P10AntarmukaMotorDC

continued
voidMotor1(intpwm,booleanreverse)
{
analogWrite(EN1,pwm);//setpwmcontrol,0forstop,and255formaximumspeed
if(reverse)
{
digitalWrite(IN1,HIGH);
}
else
{
digitalWrite(IN1,LOW);
}
}

voidMotor2(intpwm,booleanreverse)
{
analogWrite(EN2,pwm);
if(reverse)
{
digitalWrite(IN2,HIGH);
}
else
{
digitalWrite(IN2,LOW);
}
}

voidsetup()
{
inti;
continued

Page6of10|P10AntarmukaMotorDC

//for(i=6;i<=9;i++)//ForRoboduinoMotorShield
//pinMode(i,OUTPUT);//setpin6,7,8,9tooutputmode

for(i=4;i<=7;i++)//ForArduinoMotorShield
pinMode(i,OUTPUT);//setpin4,5,6,7tooutputmode

Serial.begin(9600);
}

voidloop()
{
intx,delay_en;
charval;
while(1)
{
val=Serial.read();
if(val!=1)
{
switch(val)
{
case'w'://Moveahead
Motor1(100,true);//Youcanchangethespeed,suchasMotor(50,true)
Motor2(100,true);
break;
case'x'://moveback
Motor1(100,false);
Motor2(100,false);
break;
continued

Page7of10|P10AntarmukaMotorDC

case'a'://turnleft
Motor1(100,false);
Motor2(100,true);
break;
case'd'://turnright
Motor1(100,true);
Motor2(100,false);
break;
case's'://stop
Motor1(0,false);
Motor2(0,false);
break;

}
}
}
}

Page8of10|P10AntarmukaMotorDC

Page9of10|P10AntarmukaMotorDC

P10.1A

P10.2ABC

P10.1B

P10.1C

P10.3

Page10of10|P10AntarmukaMotorDC

You might also like