{"id":31,"date":"2006-01-28T08:23:53","date_gmt":"2006-01-28T13:23:53","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/31"},"modified":"2007-08-06T23:39:47","modified_gmt":"2007-08-07T04:39:47","slug":"servo-motor-control-in-arduino","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/31\/","title":{"rendered":"Servo motor control in Arduino"},"content":{"rendered":"<p>This example controls a servomotor. It moves a servomotor based on the value of an analog input.   Thanks to Casey Reas for cleaning up the code. Thanks to Henryk Marstrander for the formula.<\/p>\n<p><!--more--><\/p>\n<pre>\n\n\/*\n  Servo control from an analog input\n \n The minimum (minPulse) and maxiumum (maxPuluse) values\n will be different depending on your specific servo motor.\n Ideally, it should be between 1 and 2 milliseconds, but in practice,\n 0.5 - 2.5 milliseconds works well for me.\n Try different values to see what numbers are best for you.\n \n This program uses the millis() function to keep track of when the servo was \n last pulsed.  millis() produces an overflow error (i.e. generates a number\n that's too big to fit in a long variable) after about 5 days. if you're\n making a program that has to run for more than 5 days, you may need to \n account for this.\n \n by Tom Igoe\n additions by Carlyn Maw\n Created 28 Jan. 2006\n Updated 7 Jun. 2006\n *\/\n\nint servoPin = 2;     \/\/ Control pin for servo motor\nint minPulse = 500;   \/\/ Minimum servo position\nint maxPulse = 2500;  \/\/ Maximum servo position\nint pulse = 0;        \/\/ Amount to pulse the servo\n\nint lastPulse = 0;    \/\/ the time in milliseconds of the last pulse\nint refreshTime = 20; \/\/ the time needed in between pulses\n\nint analogValue = 0;  \/\/ the value returned from the analog sensor\nint analogPin = 0;    \/\/ the analog pin that the sensor's on\n\nvoid setup() {\n  pinMode(servoPin, OUTPUT);  \/\/ Set servo pin as an output pin\n  pulse = minPulse;           \/\/ Set the motor position value to the minimum\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  analogValue = analogRead(analogPin);      \/\/ read the analog input\n  pulse = (analogValue \/ 10) * 19 + 500;    \/\/ convert the analog value\n                                            \/\/ to a range between minPulse\n                                            \/\/ and maxPulse. \n\n  \/\/ pulse the servo again if rhe refresh time (20 ms) have passed:\n  if (millis() - lastPulse >= refreshTime) {\n    digitalWrite(servoPin, HIGH);   \/\/ Turn the motor on\n    delayMicroseconds(pulse);       \/\/ Length of the pulse sets the motor position\n    digitalWrite(servoPin, LOW);    \/\/ Turn the motor off\n    lastPulse = millis();           \/\/ save the time of the last pulse\n  }\n}\n<\/pre>\n<p>Here&#8217;s a version that controls two servos.  Note that you have to reduce the refresh time to compensate for the pulse times of the servos as you add more servos. Thanks to Carlyn Maw for the update.\n<\/p>\n<pre>\n\/*\n  Two-servo control from an analog input\n\n  Moves two servos based on the value of one analog sensor, \n  in  opposite directions.\n\n  The minimum (minPulse) and maxiumum (maxPuluse) values\n  will be different depending on your specific servo motor.\n  Ideally, it should be between 1 and 2 milliseconds, but in practice,\n  0.5 - 2.5 milliseconds works well for me.\n  Try different values to see what numbers are best for you.\n\n  by Tom Igoe\n  Created 28 Jan. 2006\n  Repurposed for Dual Servos by Carlyn Maw\n  24 Mar. 2006\n*\/\n\nint servoPinL = 2;     \/\/ Control pin for servo motor - L\nint servoPinR = 3;     \/\/ Control pin for servo motor - R\nint minPulse = 500;   \/\/ Minimum servo position\nint maxPulse = 2500;  \/\/ Maximum servo position\nlong adjustedAV = 0;  \/\/ Analog value adjusted for determining pulse values\nint pulseL = 0;        \/\/ Amount to pulse the servoL\nint pulseR = 0;        \/\/ Amount to pulse the servoR\nint delayValueMax = 20000;  \/\/ the 20 microsecond maxiumum pulse range for the servo\nint delayValue = 20000;    \/\/ the actual value the code should wait, determined later\n\nint analogValue = 0;  \/\/ the value returned from the analog sensor\nint analogPin = 0;    \/\/ the analog pin that the sensor's on\n\nvoid setup() {\n  pinMode(servoPinL, OUTPUT);  \/\/ Set servo pins as an output pins\n  pinMode(servoPinR, OUTPUT);\n  pulseL = minPulse;           \/\/ Set the motor position values to the minimum\n  pulseR = maxPulse;\n}\n\nvoid loop() {\n  analogValue = analogRead(analogPin);      \/\/ read the analog input (0-1024)\n  adjustedAV = (analogValue \/ 10) * 19;     \/\/ convert the analog value to a number\n                                            \/\/ between 0-2000 (difference between min and max)\n\n  pulseL = adjustedAV + minPulse;           \/\/ set value for left motor\n  pulseR = maxPulse - adjustedAV;           \/\/ and the inverse for the right\n\n  digitalWrite(servoPinL, HIGH);     \/\/ Turn the L motor on\n  delayMicroseconds(pulseL);         \/\/ Length of the pulse sets the motor position\n  digitalWrite(servoPinL, LOW);      \/\/ Turn the L motor off\n\n  digitalWrite(servoPinR, HIGH);     \/\/ Turn the R motor on\n  delayMicroseconds(pulseR);         \/\/ Length of the pulse sets the motor position\n  digitalWrite(servoPinR, LOW);      \/\/ Turn the R motor off\n\n  delayValue = (delayValueMax - pulseL) - pulseR; \/\/ determine how much time you have before the 20 ms is over...\n  delayMicroseconds(delayValue);                  \/\/ 20 millisecond delay is needed between pulses\n                                                  \/\/ to keep the servos in sync\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It moves a servo from its minimum angle to its maximum, in 100 steps.  Note: this exam Thanks to Casey Reas for cleaning up the code.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}