{"id":1066,"date":"2014-07-21T19:50:27","date_gmt":"2014-07-21T23:50:27","guid":{"rendered":"https:\/\/itp.nyu.edu\/physicalcomputing\/?page_id=1066"},"modified":"2023-07-19T16:58:08","modified_gmt":"2023-07-19T20:58:08","slug":"servo-motor-control-with-an-arduino","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/servo-motor-control-with-an-arduino\/","title":{"rendered":"Lab:  Servo Motor Control with an Arduino"},"content":{"rendered":"\n<p>In this tutorial, you&#8217;ll learn how to control a servomotor&#8217;s position from a microcontroller using the value returned from an analog sensor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Servos are the easiest way to start making motion with a microcontroller. Servos can turn through a range of 180 degrees and you can use them to create all sorts of periodic or reciprocating motions. Check out some of the mechanisms at <a rel=\"noopener noreferrer\" href=\"https:\/\/www.robives.com\/mechanism\/\" target=\"_blank\">Rob Ive&#8217;s site<\/a> for ideas on how to make levers, cams, and other simple machines for making motion. The <a href=\"https:\/\/itp.nyu.edu\/physcomp\/resources\/\" data-type=\"page\" data-id=\"15\">resources<\/a> section of this site has links to other sites on construction, mechanics, and kinetics as well. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Youll_Need_to_Know\"><\/span>What You\u2019ll Need to Know<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is &nbsp;<a class=\"wikilink\" style=\"color: #37aad1;\" rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/analog-in-with-an-arduino\/\" target=\"_blank\">Analog Input with Arduino<\/a><\/li><li>What is an&nbsp;<a href=\"https:\/\/www.arduino.cc\/reference\/en\/libraries\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino Library<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Things_Youll_Need\"><\/span>Things You\u2019ll Need<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-gallery columns-4 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2224\" height=\"1668\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot.jpg\" alt=\"Photo of an Arduino Nano 33 IoT module. The USB connector is at the top of the image, and the physical pins are numbered in a U-shape from top left to bottom left, then from bottom right to top right.\" data-id=\"5921\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/digital-input-and-output-with-an-arduino\/pcomp-kit-f2019-arduino-nano-33-iot\/\" class=\"wp-image-5921\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot.jpg 2224w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">Arduino Nano 33 IoT<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"4217\" height=\"3163\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires.jpg\" alt=\"Photo of flexible jumper wires\" data-id=\"5908\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/digital-input-and-output-with-an-arduino\/pcomp-kit-f2019-jumper-wires\/\" class=\"wp-image-5908\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires.jpg 4217w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-jumper-wires-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">Flexible jumper wires. These wires are quick for breadboard prototyping, but can get messy when you have lots of them on a board.<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"3836\" height=\"2877\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard.jpg\" alt=\"Photo of a solderless breadboard\" data-id=\"5909\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/digital-input-and-output-with-an-arduino\/pcomp-kit-f2019-breadboard\/\" class=\"wp-image-5909\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard.jpg 3836w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-breadboard-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">A solderless breadboard with two rows of holes along each side. The . board is turned sideways so that the side rows are on top and bottom in this view. There are no components mounted on the board. <\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2882\" height=\"2162\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer.jpg\" alt=\"Photo of two potentiometers\" data-id=\"5924\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/resources\/parts-needed-for-physical-computing\/pcomp-kit-f2019-potentiometer\/\" class=\"wp-image-5924\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer.jpg 2882w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-potentiometer-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">Potentiometer<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2356\" height=\"1767\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor.jpg\" alt=\"Photo of a handful of 10-kilohm resistors\" data-id=\"5915\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/digital-input-and-output-with-an-arduino\/pcomp-kit-f2019-10k-resistor\/\" class=\"wp-image-5915\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor.jpg 2356w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-10k-resistor-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">10-kilohm resistors. These ones are 5-band resistors<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2890\" height=\"2168\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor.jpg\" alt=\"Photo of RC Servomotor\" data-id=\"5925\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/resources\/parts-needed-for-physical-computing\/pcomp-kit-f2019-servomotor\/\" class=\"wp-image-5925\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor.jpg 2890w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-servomotor-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">RC Servomotor<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2032\" height=\"1524\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor.jpg\" alt=\"Photo of a Force Sensing Resistor (FSR)\" data-id=\"5920\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/resources\/parts-needed-for-physical-computing\/pcomp-kit-f2019-force-sensing-resistor\/\" class=\"wp-image-5920\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor.jpg 2032w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-force-sensing-resistor-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">Force Sensing Resistor (FSR)<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"2293\" height=\"1720\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor.jpg\" alt=\"Photo of a handful of Phototransistors.\" data-id=\"5912\" data-full-url=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor.jpg\" data-link=\"https:\/\/itp.nyu.edu\/physcomp\/resources\/parts-needed-for-physical-computing\/pcomp-kit-f2019-phototransistor\/\" class=\"wp-image-5912\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor.jpg 2293w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor-768x576.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-phototransistor-1280x960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption class=\"blocks-gallery-item__caption\">Phototransistors. The short leg goes to voltage, and the long leg goes to the input pin of a microcontroller.<\/figcaption><\/figure><\/li><\/ul><figcaption class=\"blocks-gallery-caption\">Figures 1-8. The parts you&#8217;ll need for this exercise. Click on any image for a larger view.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prepare_the_breadboard\"><\/span>Prepare the breadboard<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections, as shown in Figures 9 and 10.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplate_bb.png\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"365\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplate_bb.png\" alt=\"An Arduino Uno on the left connected to a solderless breadboard, right. The Uno's 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno's ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. \" class=\"wp-image-2159\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplate_bb.png 559w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplate_bb-300x195.png 300w\" sizes=\"(max-width: 559px) 85vw, 559px\" \/><\/a><figcaption>Figure 9 Breadboard view of an Arduino Uno on the left connected to a solderless breadboard, right.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Figure 9. An Arduino Uno on the left connected to a solderless breadboard, right. The Uno&#8217;s 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno&#8217;s ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplateNanoShort_bb.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplateNanoShort_bb.png\" alt=\"Arduino Nano on a breadboard.\" class=\"wp-image-5903\" width=\"228\" height=\"359\"\/><\/a><figcaption>Figure 10. An Arduino Nano mounted on a solderless breadboard. The Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.<\/figcaption><\/figure><\/div>\n\n\n\n<p>The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano&#8217;s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano&#8217;s GND pin (physical pin 14) is connected to the left side black column. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.<\/p>\n\n\n\n<p><em style=\"font-size: 16px;\">Images made with <a href=\"http:\/\/fritzing.org\/home\/\">Fritzing<\/a><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Connect_an_Analog_Input_Sensor_and_a_Servo\"><\/span>Connect an Analog Input Sensor and a Servo<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Connect an analog input to analog pin 0 as you did in the <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/analog-in-with-an-arduino\/\" target=\"_blank\">Analog Input Lab<\/a> covered previously. A force-sensing resistor is shown in Figure 11-14 below, but you can also use a potentiometer, phototransistor, or any analog input you prefer. Then connect an RC servomotor to digital pin 9. The yellow wire of the servo goes to the pin, and the red and black wires go to +5V and ground, respectively.<\/p>\n\n\n\n<p>Most RC servomotors are rated for 4-6 volt power input. When you&#8217;re using a 3.3V microcontroller like the Nano 33 IoT, you can use the V<sub>in<\/sub> pin to power the motor if you&#8217;re running off USB power, or off a 5V source connected to the V<sub>in<\/sub>. <\/p>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372278570#t=0m0s\" target=\"_blank\">Related video:Intro to Servo Motors<\/a><\/p>\n\n\n\n<p><span style=\"color: #ff0000;\"><strong>Safety Warning!<\/strong>&nbsp;Not all servos have the same wiring colors. For example, the Hextronik servos that come with Adafruit&#8217;s ARDX kit use red for +5V, brown for ground, and mustard yellow for control. Check the specifications on your particular servomotor to be sure.<\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"587\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabServo_Uno_schem.png\" alt=\"Schematic view of an Arduino Uno connected to a voltage divider input circuit on analog in pin 0 and a servomotor on digital pin 9. On the left, a fixed 10-kilohm resistor is attached to analog in pin 0 and to ground on the Arduino. A variable resistor is attached to analog in pin 0 and to +5 volts. On the right, a servomotor's control wire is attached to digital pin D3. The motor's voltage input is attached to +5 volts, and its ground is attached to ground on the Arduino. A 10-microfarad capacitor is mounted across the +5V and ground buses close to where the motor voltage and ground wires are connected.\" class=\"wp-image-10360\"\/><figcaption>Figure 11. Schematic view of a servomotor and an analog input attached to an Arduino Uno.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"330\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabServo_Uno_bb.png\" alt=\"Breadboard view of a servomotor and an analog input attached to an Arduino Uno. The +5 volts and ground pins of the Arduino are connected by red and black wires, respectively, to the left side rows of the breadboard. +5 volts is connected to the left outer side row (the voltage bus) and ground is connected to the left inner side row (the ground bus). The side rows on the left are connected to the side rows on the right using red and black wires, respectively, creating a voltage bus and a ground bus on both sides of the board. A force-sensing resistor, or FSR, is mounted in rows 18 and 19 of the left center section of the breadboard. a 10-kilohm resistor connects one leg of the FSR to the left side ground bus. A blue wire connects the row that connects these two to analog in 0 on the Arduino. A red wire connects the other pin to the left side voltage bus. A servomotor's voltage and ground connections are connected to the voltage and ground buses on the left side of the breadboard. the servomotor's control wire is connected to pin D9 of the Arduino. A 10-microfarad capacitor is mounted across the +5V and ground buses close to where the motor voltage and ground wires are connected.\" class=\"wp-image-10361\"\/><figcaption>Figure 12. Breadboard view of a servomotor and an analog input attached to an Arduino Uno.<\/figcaption><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"559\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabServo_Nano_schem.png\" alt=\"Schematic view of an Arduino Nano 33 IoT connected to a voltage divider input circuit on analog in pin 0 and a servomotor on digital pin 9. On the left, a fixed 10-kilohm resistor is attached to analog in pin 0 and to ground on the Arduino. A variable resistor is attached to analog in pin 0 and to Vin pin (+5 volts). On the right, a servomotor's control wire is attached to digital pin D3. The motor's voltage input is attached to Vin, and its ground is attached to ground on the Arduino. A 10-microfarad capacitor is mounted across the 3.3V and ground buses.\" class=\"wp-image-10362\"\/><figcaption>Figure 13. <meta charset=\"utf-8\">Schematic view of a servomotor and an analog input attached to an Arduino Nano 33 IoT.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1352\" height=\"1110\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_bb_lab_bb.png\" alt=\"Breadboard view of an Arduino Nano 33 IoT connected to a voltage divider input circuit on analog in pin 0 and a servomotor on digital pin 9. A fixed 10-kilohm resistor is attached to analog in pin 0 and to ground on the Arduino. A variable resistor is attached to analog in pin 0 and to Vin pin (+5 volts). A servomotor's control wire is attached to digital pin D3. The motor's voltage input is attached to Vin, and its ground is attached to ground on the Arduino. A 10-microfarad capacitor is mounted across the 3.3V and ground buses.\" class=\"wp-image-10860\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_bb_lab_bb.png 1352w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_bb_lab_bb-768x631.png 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_bb_lab_bb-1200x985.png 1200w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption><meta charset=\"utf-8\">Figure 14. <meta charset=\"utf-8\">Breadboard view of a servomotor and an analog input attached to an Arduino Nano 33 IoT.<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>When you attach the servo, you&#8217;ll need a row of three male headers to attach it to a breadboard. You may find that the pins don&#8217;t stay in the servo&#8217;s connector holes. Put the pins in the servo&#8217;s connector, then push them down on a table gently. They will slide up inside their plastic sheaths, and fit better in your servo&#8217;s connector.<\/p>\n\n\n\n<p>Different RC servomotors will have different current requirements. The <a href=\"https:\/\/www.towerpro.com.tw\/product\/sg5010-4\/\">Tower SG5010 model servo<\/a>  sold by Adafruit draws more current than the HiTec <a href=\"https:\/\/www.servocity.com\/hs-311-servo\/\">HS311<\/a> and <a href=\"https:\/\/www.servocity.com\/hs-318-servo\/\">HS318<\/a> sold by ServoCity, for example. The Tower Pro servo draws 100-300 mA with no load attached, while the HiTec servos draw 160-180mA. The decoupling capacitor in the circuit will smooth out any voltage dips that occur when the servo turns on, but you will need an external 5V supply if you are using more than one servomotor. <\/p>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372278570#t=1m12s\" target=\"_blank\">Related video: Connect the Servo<\/a><\/p>\n\n\n\n<p>Figures 15-17 show steps of this in action.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_short_headers.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"300\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_short_headers-199x300.jpg\" alt=\"Photo of a servomotor connector with three header pins next to it. The header pins appear too short to connect properly to the servomotor connector.\" class=\"wp-image-1638\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_short_headers-199x300.jpg 199w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_short_headers.jpg 399w\" sizes=\"(max-width: 199px) 85vw, 199px\" \/><\/a><figcaption>Figure 15. Attaching header pins to a servomotor connector. If your header pins are too short, as shown here, you can lengthen them.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_headers_push.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"300\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_headers_push-199x300.jpg\" alt=\"Photo of a hand holding a servomotor connector with header pins pushed partway into the holes. The pins are being braced against a tabletop.\" class=\"wp-image-1640\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_headers_push-199x300.jpg 199w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_headers_push.jpg 399w\" sizes=\"(max-width: 199px) 85vw, 199px\" \/><\/a><figcaption>Figure 16. Push the short ends of the header pins into the servomotor connector&#8217;s holes and then brace the long ends against a tabletop while you push down on the connector. Do this gently and the header pins will move in their plastic mount.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_longer_headers.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"300\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_longer_headers-199x300.jpg\" alt=\"Photo of a servomotor connector with three header pins next to it. The header pins are now longer on top and shorter on bottom than they were in the first picture. \" class=\"wp-image-1639\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_longer_headers-199x300.jpg 199w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/servo_longer_headers.jpg 399w\" sizes=\"(max-width: 199px) 85vw, 199px\" \/><\/a><figcaption>Figure 17. Now your header pins will be longer on top and shorter on bottom, and will stay firmly in the servomotor connector.<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program_the_Microcontroller\"><\/span>Program the Microcontroller<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, find out the range of your sensor by using <code>analogRead()<\/code> to read the sensor and printing out the results.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid setup() {\n  Serial.begin(9600);       \/\/ initialize serial communications\n} \n\nvoid loop()\n{\n  int analogValue = analogRead(A0); \/\/ read the analog input\n  Serial.println(analogValue);      \/\/ print it\n}\n<\/pre><\/div>\n\n\n<p>Now, map the result of the analog reading to a range from 0 to 179, which is the range of the sensor in degrees. Store the mapped value in a local variable called servoAngle.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid setup() {\n  Serial.begin(9600);       \/\/ initialize serial communications\n} \n\nvoid loop()\n{\n  int analogValue = analogRead(A0); \/\/ read the analog input\n  Serial.println(analogValue);      \/\/ print it\n\n  \/\/ if your sensor's range is less than 0 to 1023, you'll need to\n  \/\/ modify the map() function to use the values you discovered:\n  int servoAngle = map(analogValue, 0, 1023, 0, 179);\n}\n<\/pre><\/div>\n\n\n<p>Finally, add the servo library at the beginning of your code, then make a variable to hold an instance of the library, and a variable for the servo&#8217;s output pin. In the <code>setup()<\/code>, initialize your servo using <code>servo.attach()<\/code>. Then in your main loop, use servoAngle to set the servo&#8217;s position.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n#include &quot;Servo.h&quot;      \/\/ include the servo library\n\nServo servoMotor;       \/\/ creates an instance of the servo object to control a servo\nint servoPin = 9;       \/\/ Control pin for servo motor\n\/\/ time when the servo was last updated, in ms\nlong lastMoveTime = 0;  \n\nvoid setup() {\n  Serial.begin(9600);       \/\/ initialize serial communications\n  servoMotor.attach(servoPin);  \/\/ attaches the servo on pin 9 to the servo object\n} \n\nvoid loop() {\n  int analogValue = analogRead(A0); \/\/ read the analog input\n  Serial.println(analogValue);      \/\/ print it\n\n  \/\/ if your sensor&#039;s range is less than 0 to 1023, you&#039;ll need to\n  \/\/ modify the map() function to use the values you discovered:\n  int servoAngle = map(analogValue, 0, 1023, 0, 179);\n\n  \/\/ move the servo using the angle from the sensor every 20 ms:\n  if (millis() - lastMoveTime &gt; 20) {\n    servoMotor.write(servoAngle);\n    lastMoveTime = millis();\n  }\n}\n<\/pre><\/div>\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372278570#t=2m37s\" target=\"_blank\">Related video: Code for the Servo &amp; Turn the Servo<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Get_Creative\"><\/span>Get Creative<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Servo motors give you the power to do all kinds of things.<\/p>\n\n\n\n<p>They can be used to push a remote control button, in a pinch, as shown in Figure 18.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/remote.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"225\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/remote-300x225.jpg\" alt=\"Photo of a remote control mounted in a wooden cradle. A servomotor mounted on the side of the cradle is positioned such that when it moves, its horn presses down on the power button of the remote control.\" class=\"wp-image-1646\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/remote-300x225.jpg 300w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/remote.jpg 320w\" sizes=\"(max-width: 300px) 85vw, 300px\" \/><\/a><figcaption>Figure 18. A servomotor can press remote control buttons The remote control is mounted in a wooden frame, and the servo is mounted on the side of the frame. The servo horn moves down to press the power button.<\/figcaption><\/figure><\/div>\n\n\n\n<p>You can play music with found objects like in this <a rel=\"noreferrer noopener\" href=\"https:\/\/www.nysoundworks.org\/soundart#\/objects-of-our-affection\/\" target=\"_blank\">Project<\/a> by Nick Yulman. You can build a frisking machine like in this <a rel=\"noopener noreferrer\" href=\"http:\/\/fletcherbach.com\/FRSK04\" target=\"_blank\">project<\/a> by Sam Lavigne and Fletcher Bach. If you&#8217;ve got 800 or so of them and a lot of time, you can build a wooden mirror like this <a rel=\"noopener noreferrer\" href=\"http:\/\/smoothware.com\/danny\/woodenmirror.html\" target=\"_blank\">Project<\/a> by Daniel Rozin.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to control a servomotor&#8217;s position from a microcontroller using the value returned from an analog sensor.<\/p>\n","protected":false},"author":5,"featured_media":0,"parent":1622,"menu_order":104,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[20,46,11,15,8,26],"tags":[],"class_list":["post-1066","page","type-page","status-publish","hentry","category-analog","category-arduino-fundamentals","category-code","category-lab","category-motors","category-programming"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1066"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":58,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1066\/revisions"}],"predecessor-version":[{"id":10862,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1066\/revisions\/10862"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1622"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}