Published 2026-02-24
Playing with theservo: From getting started to actual combat with the 51 MCU, this article will help you!
Are you thinking about adding a "joint" that can swing accurately to your innovative product? Steering gear sounds very professional, but it's actually not as difficult as you think. Especially connecting it with the classic 51 microcontroller is an excellent path for entry-level makers. When many friends first came into contact with it, the biggest headache was that they couldn't figure out how it moved and how I could use code to control it. Don’t worry, we’ll talk about it thoroughly today, and I’ll make sure you feel confident after reading it.
To put it bluntly, theservois like a particularly obedient "little rotating guard". It integrates a motor, reduction gear and a control circuit. You tell it which angle to turn to, and it will work hard to reach that position and stop steadily.
This command is conveyed through a signal called PWM (Pulse Width Modulation). You can think of it as a repetitive high and low level pulse, and the width of the pulse, that is, the duration of the high level, is the "action signal" for the steering gear.
Commonservos, such as small servos such as SG90, recognize pulses with a period of 20 milliseconds. Among them, the high level time of about 1.5 milliseconds corresponds to the middle position (90 degrees). When the pulse width changes between 0.5 milliseconds and 2.5 milliseconds, the servo axis will rotate within the range of 0 to 180 degrees. The more accurate the pulse width you give, the more accurate it will turn.
The 51 microcontroller itself will not directly output such a precise pulse width signal, but we can "simulate" it through programming. This requires the use of the timer inside the microcontroller, which is a very accurate "timer".
We can set the timer to generate an interrupt every 10 microseconds or 100 microseconds. In the interrupt service program, we use a variable to count, and according to the value of this counter, we change the level of the IO port (such as P1.0) connected to the servo.
For example, to output a high level for 1.5 milliseconds, if the timer interrupts once every 100 microseconds, the high level needs to be maintained for 15 interrupts. In this way, we can "piece together" the PWM wave that the servo can recognize. It may feel a bit confusing when you first start writing code, but once you successfully debug it yourself, the sense of accomplishment is unparalleled.
There are many types of servos on the market. Choosing a "partner" for the 51 microcontroller mainly depends on two points: working voltage and signal requirements. The most commonly used ones are analog servos like SG90. Their signal requirements are the standard PWM we mentioned above, which is very friendly.
These servos usually work at 5V voltage, which exactly matches the standard operating voltage of the 51 microcontroller. Can I directly use the IO port driver of the microcontroller? It is best not to, because the current when the servo is working is relatively large, which will damage the microcontroller pins.
A safer approach is to use a triode or a specialized level conversion chip for isolation. The IO port of the microcontroller outputs a control signal to the drive circuit, and then the drive circuit "commands" the steering gear to rotate. In this way, the microcontroller is only responsible for outputting "brain" instructions, leaving the hard work to the driver part, making the system more stable and reliable.
When many friends turn the servo for the first time, they may find that it shakes violently or responds half a beat slower. This is most likely a power supply issue. The current impact when the servo is started and locked cannot be underestimated. If the power supply is insufficient, the voltage will be pulled down, causing the microcontroller to reset or the servo to work abnormally.
The solution is simple: power the servo separately! Use a better quality 5V power supply and connect the power ground (GND) and the microcontroller ground together to ensure that they have a common reference potential. Then the power supply of the microcontroller and the power supply of the servo can be separated to effectively avoid interference.
Also, check that your PWM period is strictly stable at 20 milliseconds. If the program is blocked due to other tasks, causing the pulse period to be inaccurate, the servo will also vibrate. Ensuring that the timer interrupt priority is high enough is the key to generating a stable signal.
I want the servo to slowly turn from 0 degrees to 180 degrees and back again. How should I write the code? We can define an angle to pulse width mapping function. For example, 0 degrees corresponds to 0.5ms high level, and 180 degrees corresponds to 2.5ms. Then 90 degrees is 1.5ms.
In the main loop, we can use a for loop to increment the angle variable from 0 to 180, and reset the PWM comparison value every time it changes by one degree. Note that a little time should be left between each angle for the servo to physically rotate, such as a delay of 15-20 milliseconds. This way the action looks coherent and smooth.
In order to be more flexible, you can encapsulate the control of the servo into a function, such asvoid ( char angle), pass in the angle value, and the PWM parameters will be automatically calculated and updated inside the function. In this way, if you want to control what action it takes later, such as raising an arm or turning the camera, you only need to call this function, and the main program will become very refreshing.
When your project requires controlling multiple servos at the same time, such as making a multi-degree-of-freedom robotic arm, things are a little more complicated. Because the 51 microcontroller has limited resources, it is impossible to equip each servo with an independent hardware PWM.
But don’t worry, we can still use software simulation. The core idea is still to use the concept of "time slice" in the timer interrupt. For example, we set a 10 millisecond period, and within this period, each servo outputs the required pulse width in turn. After processing the pulse of the first servo, immediately switch to the pulse output of the second servo.
This requires that your timer interrupt processing function is very efficient and cannot do delays or complex calculations in it. The usual approach is to create an array to store the current target pulse width of each servo. In the interrupt, check the table to set the IO port according to the serial number of the servo currently in service, and accurately time it. In this way, one timer can "separately" manage multiple servos. Although they cannot be completely moved at the same time, the human eye cannot detect this tiny time difference.
Seeing this, do you already have a clear idea on how to use the 51 microcontroller to control the servo? From principle to selection to code implementation, every step is actually very interesting. If you happen to have an idea on hand right now, you might as well turn on your computer, take out your microcontroller development board, and try connecting it yourself. The wonderful feeling when the servo rotates accurately under the command of your code for the first time is the joy of creation.
I want to ask you, what kind of gadget would you most like to assemble using a servo and a microcontroller? Is it a gadget that automatically tracks sunlight, or a little robot that can draw? Welcome to leave a message in the comment area to share your thoughts. Let’s communicate together and maybe we can create better sparks! If this article is helpful to you, remember to like and share it with more friends who need it.
Update Time:2026-02-24
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.