Find out more hereĪnd thats all the code you need to control the servo however you want! However, if your realized that your servo motor is not rotating the full range of motion of 180 °, you might need to calibrate your servo at the next step. servoMotor.write(0) : Sends a signal to the motor to go to the 0 ° position.servoMotor.attach(servoPin) : Tells the library which output pin on the Arduino to associate with the servoMotor object.Servo servoMotor : Creates a Servo object called servoMotor.#include : Imports the Servo library to easily interface with the servo motor.Let's go through some key lines of code used.
To control the servo motor, we can use Arduino's built-in Servo library. Here we have written some code to make the servo motor turn to the 0 ° position, wait for 3 seconds, turn to the 18 0 ° position, wait for 3 seconds and repeat the process. On the Mega, if more than 12 servos are used, the PWM functionality will be disabled on pins 11 and 12. NOTE: On Arduino boards other than the Mega, the use of the Servo Library will disable the PWM functionality on pins 9 and 10. Just make sure that the signal pin of the servo motor is connected to an output pin of the Arduino capable of pulse width modulation (PWM). I used an Arduino Mega here, but any other Arduino would work fine. (usually yellow, orange or white)Ī typical connection to an Arduino is shown below: Let's get started!Ĭommon servo motors such as the SG90 servo and the MG90S servo come with a connector with 3 pins, one for connection to a 5V DC power source (usually red), one for connection to ground (usually black or brown) and the last one for controlling the position of the servo arm. Need to move something in your project? Add a servo motor! In this tutorial we will cover the basics of controlling a servo motor and some tips on calibrating your servo motor to get the full range of motion.