Components


Hardware: ARDUINO UNO, power supply (5v), 100uF capacitor , buttons (two pieces), 1KΩ resistor (two pieces), Servo motor (which needed to be tested). .

Arduino Servo Motor Circuit Diagram and Explanation







In normal cases we need to go to the registers of controller for adjusting the frequency and for getting required duty ratio for accurate position control of servo, in ARDUINO we don’t have to do those things.

In ARDUINO we have predefined libraries, which will set the frequencies and duty ratios accordingly once the header file is called or included. In ARDUINO we simply have to state the position of servo that needed and the PWM is automatically be adjusted by UNO

First we need to set frequency of PWM signal and for that we should call “#include <Servo.h>” header file, on including this header file in the program, the frequency gets set automatically and we get to use some special conditions, which enables the user to enter needed position of servo directly without any fuzz.

Now we need to define a name for the servo “Servo sg90sevo”, here ‘sg90servo’ is the name chosen, so while writing for potion we are going to use this name, this feature comes in handy when we have many servos to control, we can control as many as eight servo by this.

Now we tell the UNO where the signal pin of servo is connected or where it needs to generate the PWM signal. To do this we have “Sg90.attach(3);”, here we are telling the UNO we connected the signal pin of servo at PIN3.

All left is to set the position, we are going set the position of servo by using “Sg90.write(30);”, by this command the servo hand moves 30 degrees, so that’s it. After that whenever we need to change the position of servo we need to call the command ”Sg90.write(needed_position_ angle);”. In this circuit we will have two buttons one button increases the position of servo and the other is for decreasing the position of servo.

The Arduino Servo Motor control tutorial is explained in step by step of C code given below.

Code
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);

                                }

                }

}


No comments

Theme images by Dizzo. Powered by Blogger.