{"id":8767,"date":"2018-04-12T02:49:45","date_gmt":"2018-04-12T06:49:45","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=8767"},"modified":"2018-04-25T02:28:47","modified_gmt":"2018-04-25T06:28:47","slug":"arduino-tutorials-for-testers-servo-motors","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/arduino-tutorials-for-testers-servo-motors\/","title":{"rendered":"Arduino Tutorials for Testers: Servo Motors"},"content":{"rendered":"<p>This is the third tutorial of Arduino tutorial series. In this tutorial, you are going to learn about how to interface Servo Motor with Arduino and how to control the movement of Servo Motor from the Serial Monitor.<\/p>\n<hr \/>\n<h3>Background about this tutorial series<\/h3>\n<p>We think that testers who get comfortable with hardware are going to have many more testing opportunities open to them. Learning even a little bit of hardware and electronics will make software testers valuable to many companies that have started making smart devices. To that end, we spent time training software testers at <a href=\"https:\/\/www.qxf2.com\/?utm_source=arduino&amp;utm_medium=click&amp;utm_campaign=From%20blog\">Qxf2<\/a> on hardware. The results were good. So we thought we should share the training material we used on our blog.<\/p>\n<hr \/>\n<h3>Components Required:<\/h3>\n<p>You can get the components listed here fairly easily at many places (including online). We bought our engineers <a href=\"https:\/\/www.amazon.in\/dp\/B01KNR581U\/ref=ox_ya_os_product\">this Arduino super-starter kit<\/a>.<\/p>\n<ol>\n<li>Arduino Board<\/li>\n<li>Jumper Wires<\/li>\n<li>Servo Motor (In this tutorial, we used micro-servo SG-90)\n<figure id=\"attachment_8768\" aria-describedby=\"caption-attachment-8768\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Micro_servo.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8768 size-medium\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Micro_servo-300x200.jpg\" alt=\"Micro-Servo Motor\" width=\"300\" height=\"200\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Micro_servo-300x200.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Micro_servo-768x512.jpg 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Micro_servo-1024x683.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-8768\" class=\"wp-caption-text\">Fig. 1 Micro-Servo Motor<\/figcaption><\/figure>\n<p>If you are hearing about Servo Motor for the first time, few questions may come to your mind. What is Servo Motor? How does it work? A Servo Motor is a special motor that gives you greater control over how much it rotates.<\/p>\n<p>Technically (you can ignore the rest of this section if you don&#8217;t want to get too technical), a Servo Motor is nothing but a simple DC motor with feedback circuitry and comparator\/error detector. Here comparator\/error detector helps to set the position of Shaft to the required angle. To know more about the construction and working of a servo motor, watch the animated video <a href=\"https:\/\/www.youtube.com\/watch?v=ZZhuD78BLDk\" data-rel=\"lightbox-video-0\">here<\/a>.<\/p>\n<p>For this tutorial, we used Micro-Servo SG-90. It rotates approximately 180 degrees. I mentioned approximately because practically, all hobby Micro-Servo Motor will not rotate complete 180 degrees, few of motors vibrate more at higher and lower angle. We need to figure out the angular area of Servo Motor where motor works fine without any vibrations. <strong>Note:<\/strong>\u00a0The reset position of Servo SG-90 is 90 degree (Actually servo moves from +90 degree to -90 degree angle).<\/p>\n<p>Before, moving forward to interface Servo Motor with Arduino Uno. You need to know pinout\/wiring details of a Servo Motor.<\/p>\n<p><strong>Pinout\/Wiring Details of Micro-Servo Motor:<\/strong><\/p>\n<ol>\n<li><b>Black\/<\/b><span style=\"color: #993300;\"><b>Brown Colored\u00a0<\/b><\/span><b>Wire = GND Wire<\/b><\/li>\n<li><b><span style=\"color: #ff0000;\">RED Colored<\/span> Wire = Vcc Wire<\/b><\/li>\n<li><b><span style=\"color: #ff6600;\">Orange Colored<\/span> Wire = Control Signal Wire<\/b><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<hr \/>\n<h3>For what kind of applications do we need servo motor?<\/h3>\n<p>You can use the servo motor in any application where you need an angular\/linear moment with velocity and acceleration. Below are the few applications where you can use the servo motor<\/p>\n<ol>\n<li>In Robotics, for any angular moment (for example, movements of Robotic Arm)<\/li>\n<li>For Toll Gate<\/li>\n<li>For Car Wiper<\/li>\n<li>We used servo motors for our\u00a0<a href=\"https:\/\/qxf2.com\/blog\/a-configurable-pothole-for-testing-autonomous-cars-part-1\/\"><span style=\"font-weight: 400;\">automated pothole testing project<\/span><\/a>\u00a0to achieve linear movement.<\/li>\n<\/ol>\n<hr \/>\n<h3>Step 1. Interfacing of Servo Motor with Arduino:<\/h3>\n<figure id=\"attachment_8769\" aria-describedby=\"caption-attachment-8769\" style=\"width: 655px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Interfacing-micro-servo.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8769 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Interfacing-micro-servo.png\" alt=\"Interfacing of Micro-Servo Motor with Arduino Uno\" width=\"655\" height=\"394\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Interfacing-micro-servo.png 655w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/Interfacing-micro-servo-300x180.png 300w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/a><figcaption id=\"caption-attachment-8769\" class=\"wp-caption-text\">Fig 2. Interfacing of Micro-Servo Motor with Arduino Uno<\/figcaption><\/figure>\n<p>Look at above fig. 2 for interfacing Micro-Servo Motor with Arduino Uno. Here, you need to use a male to male jumper wires for connection. Connect Vcc of Arduino to Vcc of a motor, GND of Arduino to GND of a motor and pin 10 of Arduino to control signal wire of a servo motor.<\/p>\n<hr \/>\n<h3>Step 2. Write a code to control angular movement of the Servo Motor:<\/h3>\n<p>Refer below flowchart to understand the flow of code.<\/p>\n<figure id=\"attachment_8771\" aria-describedby=\"caption-attachment-8771\" style=\"width: 618px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/control-servo-position-from-serial-monitor-4.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8771 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/control-servo-position-from-serial-monitor-4.png\" alt=\"Flowchart to control a position of the servo motor\" width=\"618\" height=\"691\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/control-servo-position-from-serial-monitor-4.png 618w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2018\/03\/control-servo-position-from-serial-monitor-4-268x300.png 268w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><figcaption id=\"caption-attachment-8771\" class=\"wp-caption-text\">Fig. 3 Flowchart to control a position of the servo motor<\/figcaption><\/figure>\n<p>The above flowchart is very helpful to understand the complete code to set Servo Motor position from the serial monitor.<\/p>\n<h4>Code:<\/h4>\n<pre lang=\"Arduino\">#include <Servo.h>  \/\/ load the Servo library \r\n\r\nServo angularArm;  \/\/ create servo object to control a servo  \r\n\r\nint position_target = 0;    \/\/ variable to store the servo position\r\nint position_last_set = 0;  \/\/variable to hold previous servo position\r\nint servoPin = 10; \/\/variable to hold servo signal\/control pin number\r\n\r\nvoid setup()\r\n { \r\n  Serial.begin(9600);  \/\/Initialize serial monitor\r\n  angularArm.attach(servoPin);  \/\/ attaches the servo signal\/control pin  to the servo object \r\n} \r\n\r\nvoid loop() \r\n{ \r\n  Serial.print(\"Set servo position (1-180): \");  \/\/ print message on monitor for user\r\n  while(Serial.available()==0) { }     \/\/ wait here until user enter input data\r\n  position_target= Serial.parseInt();   \/\/ Read user entered integer value and store it in a variable\r\n  Serial.println(position_target); \/\/ Print user entered value on monitor\r\n\r\n  if (position_target != position_last_set and position_target != 0)\r\n {\r\n  angularArm.write(position_target);  \/\/ send control signal to servo motor to take targeted position\r\n  position_last_set = position_target;  \/\/ hold servo targeted position in a variable\r\n  delay(1000); \/\/ waits for the servo to get the position\r\n  }  \r\n} \r\n\r\n<\/pre>\n<p>In above code, we used Arduino&#8217;s standard Servo library and to know more about this library refer link <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/Servo\">here<\/a>. We created a Servo object\u00a0&#8216;angularArm&#8217; and attached it to pin number 10 as Servo Motor control wire is connected to pin 10 of Arduino. The script prompts the message on serial monitor for the user to get the targeted position of the Servo Motor. We are looking for targeted servo position angle between (1 to 180). If the user provides the valid targeted position, then above script\/code executes servo\u00a0write function (i.e. angularArm.write(position_target)) to set the position of the Servo Motor.<\/p>\n<p>Note:\u00a0<span style=\"font-weight: 400;\">Here, by using if condition, we are avoiding to set the control signal of the Servo Motor when we get last targeted value and a zero (0) value due to improper settings in the serial monitor. When the user sets serial monitor to a new line or a carriage return or Both NL &amp; CR mode, serial monitor receives zero(0) value and that pulls Servo Motor shaft\/rotor angle back to zero.<\/span><\/p>\n<p>Upload above code and open the serial monitor, you will see a prompted message on the screen. Provide servo position value between 1 to 180 from serial monitor and check the change in servo shaft position. Provide different servo position value and observe the change in servo shaft position. In the same way, you can figure out the angular area of Servo Motor where motor works fine without any vibrations.<\/p>\n<hr \/>\n<h3>Practice Task:<\/h3>\n<ol>\n<li><b>Control position of Servo motor using Potentiometer:\u00a0<\/b>\n<ul>\n<li>Hint:\n<ul>\n<li>Refer tutorial-2 for potentiometer interfacing with Arduino<\/li>\n<li>Use Map function to convert POT values to Servo position angle<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<p>Hope you enjoyed the tutorial. In this tutorial, you learned about how to interface and control the Servo Motor. Look at our next tutorial <a href=\"https:\/\/qxf2.com\/blog\/arduino-tutorials-testers-lcd-display\/\">here<\/a>, you will learn about how to interface LCD Display and print user-friendly messages along with the sensors\/actuator values. If you follow along, at the end of this <a href=\"https:\/\/qxf2.com\/blog\/arduino-tutorial-series-for-testers\/\">series<\/a>, you will distinguish\/understand how the hardware devices\/instruments\/products are designed, you can also guess how they were programmed, you will get ideas about how to test hardware and you can build and help others in building products.<\/p>\n<p><strong>Learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2 Services<\/a>.<\/strong><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>This is the third tutorial of Arduino tutorial series. In this tutorial, you are going to learn about how to interface Servo Motor with Arduino and how to control the movement of Servo Motor from the Serial Monitor. Background about this tutorial series We think that testers who get comfortable with hardware are going to have many more testing opportunities [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159,117],"tags":[119,160],"class_list":["post-8767","post","type-post","status-publish","format-standard","hentry","category-arduino","category-hardware-testing","tag-arduino","tag-servo-motor"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=8767"}],"version-history":[{"count":29,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8767\/revisions"}],"predecessor-version":[{"id":9306,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/8767\/revisions\/9306"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=8767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=8767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=8767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}