10% Off For Your First Order, Code:Elephant10

Arduino Project: Driving Your Servo Motor

Posted by Fiona Su on


A servo motor allows for precise control of servo position. It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, an enclosure, a PCB and gears.
There are two ways for Arduino to control the engine. This time, we directly use servo function that comes with Arduino to control the engine.
Hardware required:

  • Arduino Board *1
  • USB Cable *1
  • Potentiometer*1
  • 9G Servo Motor*1
  • Breadboard *1
  • Breadboard Jumper Wires
  • Male to Female Dupont Line

Connection for REV4:


Connection for 2560 R3:

Sample Code:

#include <Servo.h>
Servo myservo;//define steering engine variable
int servo =0;
void setup()
 Serial.begin(9600); // 9600 bps
 myservo.attach(9);//define steering engine interface(alternative 9 and 10 but just able to control 2interfaces)
void loop()
servo=map ( analogRead(0) , 0 , 1023 , 0 , 180 )  ;
Serial.println(servo ,DEC);
myservo.write(servo);//set rotating angle

After connection and uploading codes, rotate potentiometer knob to adjust the engine’s rotating angle;open serial monitor, and we can see the value of rotating angle.


Leave a comment

Please note, comments must be approved before they are published