Published 2026-03-10
You hold an SG90servoin your hand and want it to move. You can open the programming software and face the blank code box. Is your mind a mess? I don’t know what to do in the first step, but the code gets stuck in the middle of writing? In fact, as long as we go through the flow chart on paper or in our minds first, writing code is as easy as following a recipe.
Drawing the flow chart of SG90 is actually not that mysterious. To put it bluntly, it means drawing the thoughts in your mind using frames and arrows. Imagine you want theservoto turn from left to right and back again. The first step in the flow chart is always "start", followed by "initialization", which is to tell the microcontroller which pin should be connected to theservoand how fast it should be. The next step is to "turn the servo to 90 degrees", draw a processing box, then "wait for 1 second", and then draw a processing box "turn to 0 degrees". Finally, use an arrow to jump back to form a loop, or "end" directly. Write each step clearly, and the code will be translated according to this logic.
The program is written, but the servo doesn't move at all. This may be the most frustrating thing. Don't worry, nine times out of ten, a key step is missing from the flow chart - power supply. Although the SG90 is small, it is also a motor, and it cannot be driven by the current of a microcontroller. In your flow chart, you need to add the step "Check external power supply" after "Initialization". ️The solution is very simple, connect the red wire of the servo to 5V, the brown wire to ground, and the yellow wire to the signal. If it still doesn't work, check to see if the correct PWM wave is set in your flow chart. SG90 generally requires a frequency of 50Hz, which is a period of 20ms, and the high level time is between 0.5ms and 2.5ms. None of these conditions can be missing from the flow chart.
Why does my servo go to 60 degrees when it is turned to 45 degrees? This is probably a problem with the calculation logic in your flow chart. The SG90 we use controls the angle by changing the duration of the high level. A more accurate approach is not to memorize by rote 0 degrees is 0.5ms and 180 degrees is 2.5ms. You can do a "calibration" link in the flow chart first. For example, you first send a 0.5ms pulse and measure the actual angle, then send a 2.5ms pulse and measure again. Make a linear conversion between these two actual angles and the theoretical values, and put them into your program, so that you can hit them wherever you want. Many small errors are caused by the fact that actual individual differences are not considered in the flow chart.
It finally started to turn, but the servo was shaking like chaff and the sound was harsh. The problem usually lies in your process design being too "rough". Do you directly let the servo jump from 0 degrees to 180 degrees instantly? This is like asking a person to run from the door of his home to the door of his community in an instant. He will definitely stumble a few steps. The solution is to add "smooth transitions" to the flow chart. For example, if you want the servo to turn from 0 degrees to 90 degrees, you can break it into 10 steps, turn 9 degrees each time, and add a 10 millisecond delay between each step. In the flow chart, this is reflected as a loop structure: set the target angle, calculate the difference between the current angle and the target angle, and if the difference is greater than 1 degree, take another step. With this design, the steering gear moves much more smoothly.
If your project requires the servo to repeatedly perform a series of actions, such as turning 30 degrees first, waiting for a while, then turning 120 degrees, and then back. Never write a long list of codes in a flowchart. A better approach is to introduce the concept of a "state machine". You can define "state 0" as turning to 30 degrees, "state 1" as waiting for 1 second, and "state 2" as turning to 120 degrees. The main program only does one thing: determine which state it is currently in, then execute the action corresponding to that state, and decide what the next state is. With this design, your flow chart will be very clear. If you want to add or modify an action, you only need to change the state and jump conditions without touching the entire program.
Want to use one microcontroller to control several SG90s so that they can move in their own way? At this time, the design of the flow chart will test your level. If you control them one by one, you will find that when the second servo is about to move, the first one has stopped, and they cannot move "at the same time". The trick is not to let the program "get stuck" in the flow chart waiting for a certain servo to finish turning. You can refresh the PWM signal of each servo in turn in a timed interrupt. In the main loop, it is only responsible for calculating the next target angle of each servo. Just like the conductor of a symphony, he does not need to play himself, he only needs to give each musician a starting signal at the right time. Your flow chart should also be designed like this: one is responsible for calculation and decision-making, and the other (interrupt service routine) is responsible for precise execution. In this way, ten or eight servos can be used successfully.
What is the most troublesome problem you encounter when working on a steering gear project? Come and chat in the comment area, maybe I can help you optimize your flow chart. If you find this article useful, don’t forget to like and share it so that more friends who play servos can see it.
Update Time:2026-03-10
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.