Published 2026-03-13
Do you happen to have a creative project at hand and want to activate theservo, but you are stuck at the step of "how to connect the ultrasonic sensor to theservo"? Don't worry, many friends who have just started tinkering with hardware have made mistakes here. This thing sounds professional, but once you understand the principles, it is as smooth as building blocks.
To take action, you must first see what your "teammates" look like. The ultrasonic module you use is usually a common model like HC-SR04, with four pins clearly marked on it: VCC, Trig, Echo, and GND. Theservousually leads to three wires, the brown or black one is the ground wire, the red one is the power supply, and the yellow or orange one is the signal wire.
The connection is actually to "make a group chat" for them. Connect the VCC (power supply) of the ultrasonic wave and the servo to the 5V of the development board, and connect the GND (ground wire) to the GND of the development board, so that everyone can have a common language. The most critical thing is signal transmission: connect the ultrasonic Trig and Echo to the two digital pins of the development board (such as 5 and 6), and then connect the signal line of the servo to a pin that can output PWM waves (such as pin 9).
After the wiring is connected, the servo is still "dumb", and you have to teach it how to work through code. IDE is your best helper. The core idea of the program is very simple: first let the ultrasonic wave make a sound, then listen to how long it takes for the echo to come back, and calculate the distance. Then map this distance value into the angle of steering gear rotation.
For example, you can set the servo to turn 0 degrees when the distance is 10 centimeters, and 180 degrees when the distance is 50 centimeters. Need to use in the codelibrary to simplify ultrasonic reading, andServolibrary to control servos. The key steps are: read the distance -> usemap()function to convert the distance into an angle -> use.write(angle)to turn the servo. By executing it in a loop, your servo will be able to move its head according to the distance of your hand.
If you have a classic 51 microcontroller on hand, don't panic, the method completely works. There is no ready-made Servo library on the 51 microcontroller, but the essence of servo control is to generate a PWM wave with a period of 20 milliseconds and a high level between 0.5 and 2.5 milliseconds. This can be "simulated" completely using a timer.
When writing code, you need to configure a timer to interrupt every 0.1 milliseconds. In the interrupt service routine, use a variable to accumulate, and when it accumulates to 200, it is 20 milliseconds. At the same time, based on the target angle you calculated, decide how many interrupts in the first 20 millisecond period the signal pin should output high level. For example, if you want the servo to turn 90 degrees (corresponding to a high level of about 1.5 milliseconds), pull the high level in the first 15 interrupts and pull the low level in the next 185 interrupts. In this way, your 51 microcontroller can also accurately command the servo.
You connect the cables excitedly, but the servo shakes like chaff, or doesn't move at all? In all likelihood, it's because it's hungry. When the servo is started and stalled, the current is very large, often hundreds of milliamps. The 500mA current from the USB port is simply not enough, especially if several servos are connected at the same time. Once the voltage drops, the microcontroller will also restart.
The solution is to "separate meals". Prepare an independent power supply for the servo! Use a few 18650 batteries or a suitable voltage regulator module to specifically power the red wire (VCC) and brown wire (GND) of the servo. Then, connect the ground wire (GND) of the development board and the ground wire (GND) of the external power supply together to keep the reference level consistent. Finally, you only need to let the signal line of the development board control the servo. This is like letting the steering engine take care of its own food, only listening to your command, and working hard without occupying public resources.
Put these two guys on a car and want it to run on its own? The key here is "how to react." The ultrasonic wave is equivalent to the eyes, and the steering gear is equivalent to the neck. Use the eyes to look left and right. The program logic must be designed well: the car goes straight, and at the same time, the steering gear uses ultrasonic waves to scan the range. If an obstacle is found directly ahead (for example, the distance is less than 30 cm), let it stop.
After stopping, turn the servo 90 degrees to the left to measure the distance, and then turn the servo 90 degrees to the right to measure the distance. Compare which side is more empty, and then control the car to turn to the empty side. After the steering is completed, return the steering gear and look straight ahead, and the car will continue to move forward. By repeating this cycle, your car will be able to bypass table legs and walls in the room like a little being with a brain.
Fully expecting the servo to accurately point to where to hit, but the result is always just a few degrees off, or even shaky? This situation is probably not a hardware failure, but a "small misunderstanding" in the signal. The problem may lie in two aspects: first, the ultrasonic module itself has an error of 1-2 cm in distance measurement; second, the mapping relationship ofmap()function in your code is not calculated correctly, or the response speed of the servo is not keeping up.
The solution is to add a "filter" and "buffer". In the code, you can read the distance 5 times in a row, remove the maximum and minimum values, and then average it, so that the data will be much more stable. In addition, don't let the servo jump directly from 0 degrees to 180 degrees. You can write a loop so that it only increases or decreases by 1 degree each time. This way, the rotation will be soft, smooth, and more accurate, and will not overshoot due to inertia.
Seeing this, you should have something in mind, right? From wiring to programming, from power supply to debugging, take it apart step by step. Don’t you think it’s not that mysterious anymore? Hurry up and give it a try and get your first smart hardware project running.
Finally, I want to ask you, in your creative project, what kind of interesting function do you plan to use this pair of "eyes" and "arms" to achieve? Welcome to leave a message and share it in the comment area, maybe your ideas can inspire more people! If you find the article useful, don’t forget to like and share it to let more friends join in.
Update Time:2026-03-13
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.