---+ Arduino Project Report on Servo Motor <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">This report covers all that has been done from the beginning.<br /></font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">An Arduino is an open source electronics prototyping platform based on flexible easy to use hardware and software can sense environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors and other actuators.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We began with the blinking led light study</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">three connecting wires, and arduino Uno board, bread board, resistor and a led bulb.</font></font></font> ---++ How to connect <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">First, connect one end of a wire into the '5V' Power output on the Arduino., and the other end into the positive section of the breadboard.<br /> Then you need to plug one end of a wire into the ground ('GND') Power output, and the other end into the Negative section of the Breadboard.<br /> Connect one end of the resistor from the Negative section of the Breadboard, to any horizontal line on the main section.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">A resistor of about 330 ohms was used in order not to blow the LED.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Now to attach the Led into the circuit. The bulb will not do anything until you have uploaded the code to the board, which is done at a later stage. <br /> You have to put the cathode pin next to the wire connected to the port, and the anode pin next to the resistor. Finally plug the arduino pin into the computer and upload the code for it to run.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Code</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">// the setup function runs once when you press reset or power the board<br /> void setup() {<br /> // initialize digital pin LED_BUILTIN as an output.<br /> pinMode(LED_BUILTIN, OUTPUT);<br /> }<br /> <br /> // the loop function runs over and over again forever<br /> void loop() {<br /> digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)<br /> delay(1000); // wait for a second<br /> digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW<br /> delay(1000); // wait for a second<br /> }</font></font></font> ---++ Servo Movement <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Servo motors are regular DC motors used together with a potentiometer together with an arduino board. the arduino reads the voltage on the middle pin of the potentiometer and adjust the position of the servo shaft. Using an arduino you can tell a servo to go to a specified position.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">It has a built in motor a feedback circuit and a motor drive.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The two types of servo motor is the DC and AC servo motor.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">It is easy to control the speed and direction of a DC motor. Due to high ratio toque to inertia DC motor responses quickly to control signal. </font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We preceded to learn on how the servo works when connected to the arduino board.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We learnt about how to move the servo from 90 degrees to 180 degrees and also insteps of 1 degree to 180 degrees.</font></font></font> ---++ How to connect <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The servo motor has three wires, power, signal and the ground wire.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The ground wire (black)is connected to the ground pin on the Arduino board.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The power wire is connected to the 5V pin on the Arduino board.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Finally, the signal wire is connected to the digital pin on the Arduino board of which any port can be chosen but must be indicated in the program.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">A specification needs to be done with the servo pin used so that it is well programmed in the code to aid simulation. On the various port on the board u can use any port. </font></font></font> ---++ The Servo Library <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We also studied about the servo library and how it helps in the programming.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">To generate servo control signals the library is needed in the program which is #include<Servo.h></font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">You can also find it in the arduino IDE at the help menu and go to reference. Examples include attach (), writeMicoseconds (), detach (). etc. The Servo.h has all the control signals hence when included makes it easy. The attach() is used to indicate the servo library the pin it is connected to .The write function is used to give high to the servo with the appropriate angle.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The writeMicoseconds works like the delay, read function reads the current angle of the servo and also the attach function checks if the servo variable is attached to the pin. The detach is also used to disconnect a servo incase they are more than one connected.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The programs below cause the servo to move from 0 to 180 degrees and </font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#include <Servo.h> </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">//Servo library</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Servo myservo;</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Servo servo_test; //initialize a servo object for the connected servo </font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">int angle = 0; </font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void setup() </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">} </font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void loop () </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.write(angle); //command to rotate the servo to the specified angle</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(50); </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">} </font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(1000);</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.write(angle); //command to rotate the servo to the specified angle</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(50); </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">} </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(1000);</font></font></font> <verbatim><a name="_GoBack"></a></verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">The program below also causes the program to run from zero to 180.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#include <Servo.h></font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Servo servo_test; // create servo object to control a servo</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">// a maximum of eight servo objects can be created</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">int pos = 0; </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">int angle =0; // variable to store the servo position</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void setup()</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">myservo. attach (9); // attaches the servo on pin 9 to the servo object</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">}</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void loop()</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ // in steps of 1 degree<br /> }</font></font></font> ---++ <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif"> *Calculating the angle and the distance of the servo measured by the ultrasonic sensor* </font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We preceded to learn about how to calculate the distance of the servo with the ultrasonic sensor.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">We fixed the servo motor on a board and also fixed the sensor on it to. With all other connections in place we had the sensor turn together with the servo motor .The code below calculated the distance and the angle of the movement.</font></font></font> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#include <NewPing.h></font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#include <Servo.h> </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#define TRIGGER_PIN 13</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#define ECHO_PIN 12</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">#define MAX_DISTANCE 200</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Servo servo_test; //initialize a servo object for the connected servo </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">int angle = 0;</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void setup() {</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">pinMode(TRIGGER_PIN, OUTPUT);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">pinMode(ECHO_PIN, INPUT);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.begin(9600);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.attach(9);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">}</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">void loop() {</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.write(angle); //command to rotate the servo to the specified angle</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print("Obstacle is ");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(sonar.ping_cm());</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(" cm away at an angle of ");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(angle);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.println(" degrees");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(50); </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">} </font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(1000);</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">{ </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">servo_test.write(angle); //command to rotate the servo to the specified angle</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print("Obstacle is ");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(sonar.ping_cm());</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(" cm away at an angle of ");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.print(angle);</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">Serial.println(" degrees");</font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(50); </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">} </font></font></font><font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">delay(1000);</font></font></font> <verbatim> </verbatim> <font face="Liberation Serif, serif"><font size="3"><font face="Calibri Light, serif">}</font></font></font> Here are 2 photos of our setup | <img alt="distanceMeas.jpg" height="783" src="%ATTACHURL%/distanceMeas.jpg" title="distanceMeas.jpg" width="587" /> | <img alt="servo.jpg" height="780" src="%ATTACHURL%/servo.jpg" title="servo.jpg" width="586" /> | | Ultrasonic distance sensor mounted on the servo motor | Arduino with connection to servo motor | -- %USERSIG{UliRaich - 2017-05-01}% ---++ Comments %COMMENT%
Attachments
Attachments
Topic attachments
I
Attachment
History
Action
Size
Date
Who
Comment
jpg
distanceMeas.jpg
r1
manage
1781.2 K
2017-05-01 - 09:04
UnknownUser
jpg
servo.jpg
r1
manage
1970.2 K
2017-05-01 - 09:05
UnknownUser
This topic: Embedded_Systems
>
WebHome
>
StudentProjects
>
3WheelRobot
>
ServoMotorControl
>
ReportOnTheServoMotorControl
Topic revision: r1 - 2017-05-01 - uli
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback