Published 2026-02-24
Have you ever found that when you want to make a moving work, such as a smart car, an automatic gate, or a robotic arm, the most troublesome question is "Why doesn't the steering gear obey the orders?" In fact, this is usually because you haven't found the right way to "communicate" them. Combining the infrared sensor with theservocan perfectly solve this control problem and make your creative project truly "alive".
If you want to play with infrared control, the first step is of course wiring. Don't be afraid, it's much simpler than you think. Most infrared sensors (such as common infrared obstacle avoidance or tracking modules) have three pins: VCC, GND and OUT. You only need to use Dupont wire to connect VCC to the 3.3V or 5V interface of the expansion board, GND to GND, and finally connect the OUT signal line to any pin, such as Pin 0. This is like connecting the "power line" and "signal line" to the sensor, and it can tell the information it "sees".
Connecting the wires is only the first step. The next step is to understand the "language" of the sensor. The sensor's OUT pin outputs a high level under normal circumstances (equivalent to saying "everything is normal"), and when it detects an obstacle, it outputs a low level (equivalent to shouting "There is a situation!"). Therefore, you need to use the "infinite loop" module in the programming environment to continuously read the status of the connected pin. Once you read that the pin is low, it means that the infrared "sees" something, and you know it's time to do something.
Once the sensor has a signal, the next step is to activate the rudder. The steering gear is not an ordinary motor, it can accurately turn to the angle you specify. In programming, you need to use the extension library "servo". After adding, the "servoWrite" building block will appear. Its usage is very straightforward. For example, if you select pin P1 and then enter the number 90, the servo will immediately turn to the 90-degree position.
If you want the servo to play more tricks, you can set the angle as a variable. For example, if you want the servo to swing back and forth like an oscillating fan, you can use a "for" loop to slowly increase the angle from 0 to 180, and then decrease back to 0. In this way, you become a commander, telling the steering gear exactly where to point. The whole process is smooth and simple.
Now we connect the first two steps together. This is the most exciting moment. The entire logic is actually an "if... then..." sentence: If the infrared sensor detects an obstacle, then the servo will rotate at a specific angle. This logic can be easily implemented using the "if...then...else" building blocks. You put the status of "read digital pin" after "if" and the instruction to control the servo after "then".
To give a specific example, you can make a trash can that opens automatically. Place the infrared sensor in front of the trash can, and connect the servo to the lid. Write this when programming: If the infrared pin reads 0 (detecting someone reaching out), then the servo will turn to 180 degrees (open the lid); wait for 2 seconds, then turn the servo back to 0 degrees (cover the lid). Look, a practical smart home model is just born. The whole process is automatic. Isn’t it a great sense of accomplishment?
If the servo shakes or doesn't move at all, don't immediately suspect that the hardware is broken. It's most likely a power supply problem. The servo requires relatively large current when rotating, and its own USB power supply may not be able to carry it. It's like a person doing heavy work on an empty stomach, and he will definitely tremble. The solution is very simple. Equip the servo with a separate external power supply, such as a battery box, and connect the servo's power line and GND together to ensure that they are "common ground" so that the signal can be transmitted stably.
Another common reason is code logic conflicts. It is single-threaded. If there is a "pause" command in the program, the entire motherboard will stop and wait for you. At this time, if there is a new infrared signal, it will not be able to react. The solution is to minimize long-term "pause" instructions and replace them with "pause in milliseconds", or try to use more advanced "state machine" programming thinking so that the program can both wait and respond to new interrupt signals at any time.
Once you learn the basic controls, your creativity can take off. For example, make an intelligent flower watering system that uses an infrared sensor to detect the height of the soil in the flower pot (through a simple mechanical structure). When the detection rod in the soil drops to a certain position, the infrared is triggered, and the servo opens the water valve to drip a few drops of water. Or make an interactive electronic doorbell. When someone approaches, the infrared trigger servo drives a cartoon character to play gongs and drums, which is much more interesting than ordinary doorbells.
You can also take on complex projects, such as building an infrared-tracking solar panel. Use two infrared sensors side by side to compare which one of them detects the strong light source first, and then control the servo to adjust the direction of the solar panel to always aim at the position with the strongest light. These projects are not only fun, but also allow you to practice your physics, programming and problem-solving skills. It is really a sense of accomplishment to complete them.
If you want to upgrade your work from "toy" to "prototype", the stability of the program is the key. A practical technique is to add "anti-bounce" processing. Because the response of the infrared sensor is very sensitive, it may output a false signal for a moment. After detecting the signal, you can wait for tens of milliseconds and then detect it again to confirm that it is indeed a valid signal before executing the servo action. This can effectively prevent false triggering caused by environmental interference.
Another way to improve reliability is to add "status indicators" to your program. For example, when an infrared signal is triggered, let the dot matrix screen display a smiley face, or make the onboard LED light flash. In this way, you can intuitively know whether the sensor has received the signal and whether the program is running correctly. This kind of small details can help you quickly troubleshoot problems, so that you no longer have a blind eye when debugging, and the work will naturally be more stable and reliable.
What project are you working on? Have you encountered any particularly difficult bugs? Welcome to leave a message in the comment area to share your tossing experience, maybe everyone can help you come up with ideas! If you find this article useful, don’t forget to like it and share it with more friends who like to create.
Update Time:2026-02-24
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.