Published 2026-03-27
Have you ever encountered this situation: you want to manually control the steering direction of theservo, but you don’t know how to use the simplest buttons to achieve it? Today we will talk about how STM32 controls the forward and reverse rotation of theservothrough buttons, so that you can easily meet this requirement.
The biggest advantage of using buttons to control the servo is that it is intuitive and convenient. Imagine you made a smart trash can. The lid opens when you press the button, and closes when you press the button again. Is this interactive experience natural? Button control does not require a computer or complicated interface. It responds when pressed. It is especially suitable for product prototypes or small batch equipment. Moreover, STM32's GPIO is very simple to read the button status. It can be achieved with just a few lines of code, and the development threshold is particularly low.
For those of you who have steering gear application needs, button control is simply "good news for lazy people". For example, if you want to make an automatic feeder, press to deliver food and press to stop; or make a remote control model car and use buttons to control the steering angle. This control method is not only low-cost, but also highly reliable. If the button is broken, it is cheap to replace it. The key is that the user learning cost is almost zero, and anyone can press the button.
There are various servos on the market, how to choose the right one? First, look at your usage scenario. If you just control the direction and don't need a lot of effort, a 9g small servo is enough, it's cheap and common. If you need to drive something heavier, such as a metal robotic arm, you have to use this high-torque servo. Pay attention to the working voltage of the servo. 5V is common, but some are 7.4V. It must match the power supply of STM32.
There is also the compatibility of control signals. Most servos are controlled by PWM signals with a period of 20ms and a high level time of 0.5ms to 2.5ms corresponding to 0 to 180 degrees. Therefore, when choosing a servo, be sure to confirm whether it is standard PWM control, otherwise the code will have to be changed significantly. In addition, remember to buy an extra spare when buying. The servo is easy to burn out when it is blocked, so having a spare makes you feel at ease.
The buttons and servo are connected to the STM32. Wiring is the first step and the most likely problem. The button is usually connected to a GPIO port and GND, and the pull-up resistor is turned on internally, so that when the button is released, it is high level, and when it is pressed, it is low level. Note that it is best to connect a 0.1uF capacitor at both ends of the button to eliminate jitter interference. The servo is connected to the 5V power supply and GND, and the signal line is connected to another GPIO port. The power supplies must be separated. The starting current of the servo is large, and sharing the power supply with STM32 can easily cause a reset.
Here is a little tip: power the servo separately, and the 5V port of the STM32 is only used for buttons and chips. If the power supply must be shared, the voltage will be lowered when the servo is started, causing the microcontroller to restart. Another thing is to connect all GNDs together to ensure that the signal reference potential is consistent. It is best to draw a simple diagram before wiring and confirm it is correct before turning on the power to avoid burning the board.
![]()
The code part is actually not complicated. The core is to poll the button status and change the PWM duty cycle of the servo. Initialize the timer to output PWM with a period of 20ms. First give an intermediate value such as 1.5ms high level to stop the servo at 90 degrees. Then the main loop detects the button. If the forward button is pressed, the duty cycle is increased to increase the servo angle; if the reverse button is pressed, the duty cycle is reduced. Remember to add a delay to eliminate jitter, otherwise it will jump several frames once you press it.
In order to prevent the servo from exceeding the limit angle, restrictions must be made in the code. For example, after it is turned to 180 degrees, it will not increase when pressed again; it will not decrease when it is reversed to 0 degrees. There is another small detail: it takes time for the servo to rotate. Pressing it continuously and quickly can easily cause the servo to fail to keep up. You can update the PWM the moment the button is released, or delay 200ms after each button press to allow the next action. This will feel much better.
You will definitely encounter problems when doing it for the first time. The most common one is that the servo will not move. Don't worry, use a multimeter to check whether the power supply has 5V and whether there is a waveform on the signal line. If you don't have an oscilloscope, you can write a simple program to change the high and low levels of the PWM pin and observe it with an LED. If the LED can flash, it means the timer is configured correctly. Look at the servo to see if there is a "sizzling" sound. If there is a sound, it means that it is receiving force but does not move, that is, the machine is stuck.
Key jitter is also a common problem, with one press feeling like several presses. The solution is to add software anti-bounce: after detecting that the button is pressed, delay 20ms and read it again. If it is still pressed, perform the action again. Also, if there are a large number of keys, you can use the interrupt method instead of scanning all the time in the main loop, which saves resources and is sensitive. There are ready-made codes for these problems online, which can be used with slight modifications.
This solution can be expanded into many interesting things. For example, make a manually controlled smart curtain, press the forward direction to open the curtain, and reverse the direction to close; or make a rotating platform, press the button to rotate 30 degrees for taking pictures. There are also simple tooling fixtures that can be made in industry, with buttons to control clamping and loosening, which are much cheaper than pneumatic ones. For those who make products, button-controlled servos are one of the lowest-cost human-computer interaction solutions.
If you are working on a smart home or DIY project, this feature will definitely come in handy. Think about it, using STM32 and adding a few buttons and servos can achieve many manual control needs, and it is stable and reliable. The key is that the code and hardware are simple, and the parameters can be changed to adapt to different projects, and the reusability is particularly strong. Many small products use this method to quickly verify their functions and then optimize and upgrade them.
What new tricks do you think you can use with the servo and buttons in your project? Welcome to share your ideas in the comment area, or directly search our official website to see more STM32 practical cases. Like and save this article so you won’t get lost next time you use it!
Update Time:2026-03-27
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.