Published 2026-04-29
Only when the hardware conditions are met can the drive be realized. The 51 microcontroller outputs a standard PWM, which is a pulse width modulation signal, and can control the steering angle. The core setting step is to generate a PWM signal with a period of 20ms and a high-level time in the range of 0.5ms to 2.5ms. The duty cycle corresponds to a rotation of 0° to 180°. This article provides the complete circuit connection and code configuration scheme.
The rotation of the steering gear is determined by the pulse width of the control signal. Standard servos, such as SG90 and MG995R, have the following requirements:
signal period:20ms(50Hz)
The relationship between high level time and angle:
0.5ms → 0°
1.0ms → 45°
1.5ms → 90°
2.0ms → 135°
2.5ms → 180°
Regarding the output requirements of the 51 microcontroller, one is that any I/O port can generate a PWM signal, and the other is that this generation process must accurately control its level duration.. Regarding the crystal oscillator frequency, the suggestion is that it is either 11.0592MHz or 12MHz. The purpose of this is to facilitate the timer to accurately time.
When the servo is working, its current is relatively large. When it is no-load, the current is in the range of 100mA to 200mA. Once a stall occurs, the current can reach more than 500mA. It is absolutely forbidden to directly obtain power from the I/O port of the 51 microcontroller, nor to obtain power from the 5V pin of the teaching board. Otherwise, a reset will occur or the chip will be burned.
Standard connection method:
1. signal line: Connect to any I/O port of the microcontroller (such as P1.0)
2. The positive terminal, which is VCC, should be connected to an external 5V power supply, such as the LM2596 voltage stabilizing module, and a 4.8V - 6V battery pack.

3. The negative electrode, that is, GND, must be in the same ground state as the GND of the microcontroller, that is, the negative electrode of the power supply must be connected to the GND of the development board.
The reference case about YPMFG is that an electronics enthusiast used STC89C52RC to drive the MG995R servo. At first, it directly obtained power from the development board, causing the screen to flicker. Later, it was changed to two 18650 batteries connected in series and then reduced to 5.5V for power supply. After that, the servo operated smoothly and without interference.
The core logic is to use a timer to generate a 100μs interrupt, and control the high-level time by accumulating the number of interrupts.
code steps(Take a 12MHz crystal oscillator as an example):
#includespitservo= P1^0; // Signal pin unsigned char count = 0; // Interrupt count unsigned char high_time = 15; // 1.5ms corresponds to 90° (15×100μs) void Timer0_Init() { TMOD = 0x01; // Mode 1, 16-bit timer TH0 = 0xFE; // 100μs initial value (12MHz: 65536-100=65436→0xFF9C) TL0 = 0x0C; EA = 1; // Turn on the total interrupt ET0 = 1; // Enable the timer 0 interrupt TR0 = 1; // Start the timer } void Timer0_ISR() interrupt 1 { TH0 = 0xFE; // Reload the initial value TL0 = 0x0C; count++; if(count++; servo = 1; // High level lasts for high_time×100μs } else if(count servo = 0; } else { count = 0; // End of cycle, reset count } } void main() { Timer0_Init(); high_time = 10; // Set angle: 10→45°, 15→90°, 20→135° while(1); }
Pulses are generated using _nop_() or delay loops, but the CPU usage is high and the angle is not accurate. Example:
servo = 1; delay_us(1500); // 1.5ms high level servo = 0; delay_ms(18.5); // Remaining cycle time
Disadvantages: Unable to handle other tasks at the same time, crystal oscillator error has a large impact.
Q1: What should I do if the 51 microcontroller is directly connected to the servo and there is no response?
A: Check to see if they have a common ground. The negative pole of the servo power supply and the GND of the microcontroller must be connected, otherwise there will be no signal loop.
Q2: How to solve the problem of the servo shaking and unable to fix the angle?
Improve the accuracy of the timer, use a 12MHz crystal oscillator, and reinstall the initial value every 100μs, so the interrupt frequency should remain stable.
Q3: Can one I/O port be used to control multiple servos at the same time?

A said no. Each servo needs a separate signal line. Multiple servos can share the positive and ground wires, but the power supply must have enough power.
Q4: What is the reason why it actually turns to 30° when it is set to 45°?
A: There is an error in the crystal oscillator frequency, or the high-level time is not accurate. Use an oscilloscope to measure the PWM pulse width, and then adjust the high_time value.
Q5: Tips for article writing: How to extend the life of the steering gear?
A: To prevent stalling, add a capacitor. Connect a 100μF electrolytic capacitor in parallel between the positive and negative terminals of the servo power supply to filter out ripples.
Data source: Official data manual of each steering gear (2023-2025 edition)
1. Obtain the measured waveform and use an oscilloscope or logic analyzer to check whether the PWM cycle is within the range of 20ms plus or minus 0.5ms.
2. Verification angle: Write 1.5ms high level, the servo should be still at 90° position
3. load test: Gently twist the servo arm by hand, the voltage fluctuation should not exceed 0.3V
4. Switch from multiple angles, give it a value of 1.0ms, then a value of 1.5ms, and then a value of 2.0ms in sequence, and then carefully observe its rotation to see if it has smooth characteristics.
Typical troubleshooting table:
The servo does not turn → Check the common ground and power supply voltage (4.8V-6V)
Trembling and shaking → Insufficient power or interrupt conflicts (turn off irrelevant interrupts)
It can only rotate 0° and 180°, which results in the high-level time not changing linearly. In this case, the count variable in the code needs to be checked.
Using a 51 microcontroller, with the help of two timers and an array, it can drive up to 8 servos. The key methods are:
Timer 0 triggers a 2.5 millisecond interrupt and controls the high-level starting moments of the 8 I/O ports in a sequential manner.
Each servo is stored independentlyhigh_timearray
Must use external 5V/10A or above switching power supply for power supply
code framework:
unsigned char servo_high[8] = {15,10,20,5,15,12,18,8}; //Pulse value of each servo unsigned char current_servo = 0; //In the interrupt, the previous servo is pulled down and the current servo is pulled up.
The CPU usage of this solution is about 40%, and it can still handle simple tasks such as key scanning.
With a 51 microcontroller, it is completely capable of driving the servo. The core success factors are independent power supply, precise timing, that is, 100μs interrupt, and common ground connection. For beginners, you should start with a single servo, 12MHz crystal oscillator, and timer interrupt solution, and avoid using the delay function directly.
Execute checklist now:
1. Prepare a regulated power supply of 5V/1A or above or a 4.8V battery pack
2. Write timer initialization code (100μs interrupt)
3. Use a multimeter to measure the positive and negative voltages of the servo (no load shall not be less than 4.5V)
4. Test the 90° position (1.5ms pulse) first, and then expand to other angles
Article writing tips: If the microcontroller is reset during the operation of the servo, immediately connect a 470μF capacitor in parallel to the power supply end of the servo and shorten the length of the power cord (less than 20cm). Follow this guide and you'll be able to achieve precise angle control within 30 minutes.
Update Time:2026-04-29
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.