Tutorials

How to control Servo motor with Arduino and how they work

Learn how to interface servo motor with Arduino and also learn what are servo motors and how do they work

Overview:

In this tutorial, learn how to connect a servo motor with Arduino microcontroller and control its movement through programming. Before getting started with Servo motors, It is also important to learn what is Servo motor and how servo motors work. So, lets learn everything.

What are servo motors? how Servo motors work?

Are you thinking to add movement to your project (example: robotics, remote control car turning) like rotating the device in particular direction at certain speed, to achieve this you might think about the motors but general DC motors rotate at high speeds in single direction with out the control of angle movement. So, servo motors are invented to serve this issue.

Servo motors is motors with rotatory actuator with control on its movement angle and precession which works with closed loop feedback control system. A servo motor has a simple DC motor whose shaft is connected to gears to control the speed of rotation and to add the precision.

servo motor internal parts and working

It also has a sensor generally a potentiometer which feedbacks the position of the shaft to the control board, when a signal is given to the control board for a rotation of +45°, the control board turns on DC motor till the sensor feedbacks the correct +45° position.

There are several models of servo motors. Now lets talk about a specific servo motor which we are using in this tutorial Micro Servo 9g SG90 from Tower Pro. It allows us to sweep in 180 degrees which is between -90° to +90° angle.

Servo motors has a resolution near to 1 degree, which means it is possible to move the shaft in 1 degree angle. this is the maximum resolution which we are going to achieve with the PWM signal of Arduino.

Also read: Arduino Tachometer to Measure Accurate RPM

These motors work with a PWM signal(pulse width modulation), with a work pulse between 1ms and 2ms and with a period of 20ms (50 Hz). What does it mean? This data indicates the maximum speed at which we can move the servomotor with Arduino. We can only change position every 20ms. Which depend on the type and brand of our servo.

Pulse Width modulation in servo motors:

A series of pulses are send to the control pin, generally servo motors are capable of receiving pulses for every 20ms, so we can change the position of the motor every 20ms so the signal should be sent with 50Hz frequency.

servo motor pulse width modulation angle range

The duration of the pulse determines the position of the servo motor. it ranges from 1ms to 2ms, but in some motors it is near 2.5ms. If the pulse is high of 1ms duration the servo angle will be 0°, if duration is 1.5ms the angle will be 90°, and if the duration is 2ms  the angle positions is 180°.

Servo motor pinout:

servo motor pinout diagram

As you can see from the above servo motor pinout image the brown wire is for Ground, Red wire is for Vcc 5v , and finally yellow or orange wire is for data or control signal.

Interfacing Servo motor with Arduino

Before connecting servo motor with Arduino, lets know what are the required components

Required components:

Product NameQuantityamazon logoamazon logo india
Arduino Uno Microcontroller1https://amzn.to/3H4cKxZhttps://amzn.to/3638aTS
SG90 9G Micro Servo Motor1https://amzn.to/3hA5jVihttps://amzn.to/368WjUE
Few Connecting Wireshttps://amzn.to/3H2BV4ehttps://amzn.to/3J0WVu2
Here are the required components along with Amazon buying links at best prices.

Circuit diagram:

interfacing servo motor with arduino circuit diagram

Connecting Servo motor with Arduino is very easy just connect the power wires red and brown of servo motor with 5V and GND pins of Arduino respectively. Connect the yellow wire which is control wire to pwm pin 9 of Arduino. That’s it for connection, now we can jump into the coding section.

Program code:

Now connect Arduino with PC where Arduino IDE is installed and install the Servo library through library manager or download the latest version through the below link and extract it into the Arduino libraries folder.

Arduino Servo Library Download: Link

Now to test it we are using 2 example codes one for moving from 0 to 90 and to 180 degrees and another one for sweeping from 0 to 180 and 180 to 0 degrees.

Program code to move servo from 0 to 90 and to 180 with 1 second interval.

Copy and paste the below code into Arduino IDE work space and choose the Board as Arduino UNO and select the correct port and then hit upload.

After uploading the code you can see the servo motor shaft moves from 0 to 90 and 90 to 180 with one second interval.

Program code to sweep servo from 0 to 180 and 180 to 0.

In this example we want the servo to sweep from 0 to 180º and then in the opposite direction degree by degree. So, here we are using the same methods but added two for loops , one for each direction.

After uploading the code the servo motor sweeps.

Project Ideas:

  • Can build a SunLight Tracker system for solar panels by rotating the panels towards sun to get more power efficiency.
  • Can be using in remote cars for front wheels turning.
  • Can build a robotic arm using servo motors and Arduino and control them with joystick.

And many more please share your creative projects if you had experienced with Servo motors in the below comment section. And if you like this tutorial please share it with your friends. Follow our Facebook channel “Circuit Schools” for more project updates.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button