Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Interfacing Tower Pro SG90 9G servo motor with 8051(89c51, 89c52) microcontroller.

By EG Projects

In this post i am going to teach you how to use and interface servo motors with 8051 microcontroller. 8051 series microcontroller which i am going to use for this project is ATMEL 89c52 microcontroller. Servo motors are different from ordinary motors. They can rotate from 0-degree to 180-degree depending on the motor specification. Servo motors works on PWM (Pulse Width Modulation) signal. Varying pwm signal duty-cycle rotates the Arm-Head of the servo motor. Servo Arm-Head can rotate from 0 to 180 degree. One can easily rotate the servo arm from 0 degree to a fixed angle(between 0 to 180 degree) and then back to 0 degree. Servo Arm-Head moving speed can also be controlled. I hope you people know about pulse width modulation technique. 

Servo motor main constraints

While going to use any servo motor first read its data sheet and look for two things

  • Duty Cycle of Servo motor
  • Operating Frequency or PWM Period of Servo

I am going to use the below servo motor in this tutorial. Its a small servo motor and requires only 5 volts for operation. Current consumption of this motor can drastically increase when moving loads. 

Tower Pro servo motor with active duty cycle and period

Tower Pro servo motor with active duty cycle and period

Tower pro servo motor

I am using Tower Pro Micro Servo SG90 9g in this tutorial(in above picture). Its a small servo and weights only 9 grams. Its a power full motor and can push weight around 35 kg. Servos can push heavy loads but they can not lift heavy loads. Tower Pro can push a maximum load of 35 kg.

PWM period given for tower pro servo motor is 20 ms(in frequency domain 50 Hz). Operating frequency is 50 Hz(in time domain 20 ms). To rotate the arm head one must have to generate a PWM signal having period of 20 ms and signal duty cycle in between 0 to 2 ms.

  • Tower Pro Micro Servo SG90 9g moves to 90 degree on duty cycle of 1.5 ms.
  • Moves to 180 degree on duty cycle of 2 ms.
  • Moves to 0 degree on duty cycle of 1 ms.

One can easily rotate the arm at fixed angle by just varying the duty cycle. To move servo motor arm at 135 degree duty cycle will be 1.75 ms. Speed of arm can be controlled by changing the duty cycle very slowly. If duty cycle increases 2 ms servo arm will not come back to its position it will go out of order. In some servo motors if the duty cycle increases the maximum limit it will have no effect on arm, those particular servos not rotate their arm on duty cycles greater than their specifications.

Main 8051 microcontroller servo motor project

I am going to drive four servo motors with Atmel 89c52 microcontroller. Their are four push buttons. When ever push button is pressed the corresponding servo will rotate. Servo arm will move from 0 degree to 90 and then 180, after reaching 180 degree it will bring it back to initial zero degree position. Push buttons are connected to Port-1 Pin#0,1,2,3 of 89c51 microcontroller. Servo motors are connected to Port-2 Pin#0,1,2,3 of 89c51 microcontroller.

Tower pro servo motor has three wires colored red, yellow, black(brown). Apply +5 volts to red wire. Make black(brown) ground. Apply PWM(Pulse Width modulated) signal to yellow wire. Circuit diagram of 8051 servo motor project is given below.

Tower Pro SG90 9g servo motor with atmel 89c52 microcontroller.

Tower Pro SG90 9g servo motor with atmel 89c52 microcontroller.

8051 microcontroller servo motor – Main project working

When any push button is pressed the corresponding servo will rotate. 8051 microcontroller Port-1 is declared as input port and push buttons are connected to this port. When signal is high at Port-1 pin#0,1,2,3 servos will be off. When push button is pressed corresponding pin to push button will be grounded and servo will be actuated. Port-2 is declared as output port and servo motors are connected to port-2 Pin#0,1,2,3 of microcontroller. When any push button is pressed the corresponding pin on port-2 outputs a PWM signal. This pwm signal is input to servo pwm wire. This whole process is written in the code.

How pwm signal is generated using 8051 microcontroller?

PWM is generated using timers of 89c52 microcontroller. Timer-0 of 89c52 microcontroller is used for generating PWM signal. Now how PWM is generated using timers? To understand it you need to know what is PWM(Pulse Width Modulation).

PWM is a signal whose cycle has a high period for some time and low period for some time. It is same like digital signal which has high and low waves. In PWM method the high wave of the signal is varied. This high wave is know as PWM duty cycle(see the below picture).

Picture

Tower Pro SG90 9G servo Motor Duty Cycle and PWM Period

Tower PRO SG90 9g motor has a PWM period of 20 ms. In which 18 ms is low signal and 2 ms is variable high signal. To generate delay using 89c52 microcontroller you need to calculate the values for delay and load them in the timer register. I calculated the values for generating 18 ms,1 ms,1.5,1.8 and 2 ms delay and loaded them in the timer registers. TH0 and TL0 are timer registers. Here is a small tutorial link on how to calculate delay values?

  • Using Timers of 89c51, 89c52 Microcontroller for generating delays.  

The above link is very important to understand the code. If you don’t go through the above tutorial you will be unable to understand the code below. Tower Pro servo SG90 9g has duty cycle from 1 ms to 2 ms. I calculated values for these delays. Made function for each value. When ever delay is needed i call the function.

The code in while(1) function is determining whether the push button is pressed? if pressed delay functions are called. These delay functions are actually PWM for the servo motor.  

More projects involving servo motors and microcontrollers. Each project is based on different microcontroller. Projects source code and circuits are open source. One can use and modify them according to his/her needs.

Servo motor controlled with 32 bit stm32 microcontroller

Servo motor controlled through WEB with nodemcu

Download the Project code and simulation. Folder contains the project code and hex file. Project is made in keil u-vision 4. Full keil project and files are in the folder. Simulation is made in Proteaus 8.0. You can see the PWM signal in the simulation. If you have any queries and questions write them below in the comments section.

Servo motor interfacing with 89c52 microcoontroller(code)


Filed Under: 8051 Microcontroller, Microcontroller Projects

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces
  • Renesas’ RA Family microcontrollers earn CAVP certification for cryptographic algorithms
  • MicroPython: Serial data communication in ESP8266 and ESP32 using UART

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • kt/c noise of sample and hold
  • PS4 chip replacement not going to plan
  • What is the function of the long stub?
  • How to use of GaN Transistor gates instead of IGBT in this circuit ?
  • Help with Verilog replicate operator

RSS Electro-Tech-Online.com Discussions

  • Audio equalizer
  • Background of Members Here
  • Adding Current Limit Feature to a Buck Converter
  • Passthrough charging-simple but impossible to achieve?
  • Opamp ciruit
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering