You are on page 1of 11

When the program starts running, the servo motor will rotate slowly from 0 degrees

to 180 degrees, one degree at a time. When the motor has rotated 180 degrees, it
will begin to rotate in the other direction until it returns to the home position.

#include //Servo library

Servo servo_test; //initialize a servo object for the connected servo

int angle = 0;

void setup()
{
servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino
}

void loop()
{
for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(15);
}

delay(1000);

for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(5);
}

delay(1000);
}
Once the program is started, rotating the potentiometer should cause the shaft of
the servo motor to rotate.

#include //Servo library

Servo servo_test; //initialize a servo object for the connected servo

int angle = 0;

int potentio = A0; // initialize the A0analog pin for potentiometer

void setup()

servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino

void loop()

angle = analogRead(potentio); // reading the potentiometer value between 0 and 1023

angle = map(angle, 0, 1023, 0, 179); // scaling the potentiometer value to angle value for servo between 0 and
180)

servo_test.write(angle); //command to rotate the servo to the specified angle

delay(5);
}

Target Problem:

Move the servo cap to the left when one button is pushed and to the right when the other button is pushed.
Double-check the wiring connections before connecting the microcontroller to the computer unit . Observe the
rotation of the servo cap to see if it moves to the angle desired.

volatile int i=0;//initializing a integer for incrementing and decrementing duty ratio.

#include <Servo.h>// header file for controlling servo

Servo servo;//defining the name usage as servo itself

void setup()

                pinMode(3, OUTPUT);   // sets the pin3 as output

                pinMode(0, INPUT);// sets the pin0 as output

                pinMode(1, INPUT);// sets the pin1 as output

}
void loop()

                servo.write(i);//set servo potion ‘i’ degrees

                if (digitalRead(0)==LOW)

                {

                                if (i<180)

                                {

                                                i++;//if pin0 is pressed and degrees is less than 180

                                                delay(30);

                                }

                }

                if (digitalRead(1)==LOW)

                {

                                if (i>0)

                                {

                                                i--;// if pin1 is pressed and degrees is greater than 0

                                                delay(30);

                                }

                }

}
#include <Servo.h>;

// pushbutton pin

const int buttonPin = 2;

// servo pin

const int servoPin = 9;

Servo servo;

//create a variable to store a counter and set it to 0

int counter = 0;

void setup()

{
servo.attach (servoPin);

// Set up the pushbutton pins to be an input:

pinMode(buttonPin, INPUT);

void loop()

int buttonState;

buttonState = digitalRead(buttonPin);

if (buttonState == LOW) // light the LED

counter++;

delay(150);

if(counter == 0)

servo.write (0);
}

else if(counter == 1 )

servo.write (180);

else

counter = 0;

}
//include the servo library

#include

//create a servo object called servo1 Servo servo1;

void setup() { // put your setup code here, to run once:

// set the servo pin, pin 9, as an servo output pin servo1.attach(9);


}

void loop() { // put your main code here, to run repeatedly:

int lightValue = analogRead(A0);

// map the light readings to the angle possible by the servo motor
lightValue = map (lightValue, 0, 1023, 0, 180);

// control the servo motor based on the light value read, adjust
linearly by angles servo1.write (lightValue); }
#include <Arduino.h>

#include <LiquidCrystal.h>

#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Servo myservo;

void write_angle_lcd(int pos){

lcd.setCursor(0, 1);

lcd.print(pos);

lcd.print(" ");

void setup(){

myservo.attach(10);

lcd.begin(16, 2);

lcd.print("SERVO ANGLE");

lcd.setCursor(0, 1);
}

void loop(){

int pos;

for(pos = 0; pos <= 180; pos += 1){

myservo.write(pos);

write_angle_lcd(pos);

delay(50);

for(pos = 180; pos >= 0; pos -= 1){

myservo.write(pos);

write_angle_lcd(pos);

delay(50);

You might also like