Published 2026-03-20
Have you ever had such an experience: you took out the board with great expectations, connected the motor, and uploaded the code with full confidence, only to find that the motor did not move at all, or the board simply smoked? Don't worry, 90% of newbies will stumble here. Controlling a motor is actually not that mysterious. The key is to choose the right driver, connect the right wires, and write the right code. Now let’s break it apart and crush it to talk about how to make the motor rotate steadily.
Many people think that the motor will spin just by plugging it in, but the result is that it either doesn't spin or the pins are burned. Because the IO port can only provide a maximum current of 40mA, and any small motor needs several hundred milliamps when starting. So a motor driver board must be used, which is like a "current amplifier" that uses small signals to control large currents. The common L298N is suitable for DC motors and stepper motors. One module can carry two DC motors or one stepper motor. If you are working on low-power products, such as toy motors, use L293D or more cost-effectively; for high-power products, such as 12V DC reduction motors, L298N is more robust.
If you are playing with aservo, the situation is different. Theservohas its own control circuit, and you only need to provide it with 5V power supply and PWM signal to work. But please note that smallservos such as 9g can directly draw power from 5V. If you use such a large torque, it must be powered separately, otherwise it will restart instantly. There is also a kind of stepper motor, the common one is 28BYJ-48, which is enough with a driver board; for this kind of big one, you have to use A4988 or A4988. To sum up: when choosing a driver board, first look at the motor type and power. Don’t just look for a cheap one. Search for “[motor model] driver board” online, and you’ll be right to buy accordingly.
The power supply is the hardest hit area for overturning. I have seen too many people connect the motor and the battery to the same set of batteries. As a result, the screen goes black as soon as the motor starts. Why? When the motor starts, the instantaneous current is several times that of normal operation, which will instantly pull the voltage down, resulting in insufficient voltage for reset. The correct approach is to "power supply independently", use USB or 7-12V adapter, and connect the motor drive board to the battery pack separately. For example, if you use L298N to drive two DC motors, connect a 7.4V lithium battery to L298N. There is a 5V output on L298N, which can provide power as long as the power consumption is not large.
If you use a steering gear, be even more careful. A 5V voltage regulator tube that can draw 2A of current when blocked cannot handle it at all. My suggestion is: buy a 5V/3A voltage stabilizing module or use a power bank directly (note that it must be able to continuously output, do not use one with automatic sleep). Remember an iron rule when wiring: all GND (ground wires) must be connected together, otherwise the signal cannot form a loop and the motor will either not rotate or rotate randomly. You can connect the GND of the PC, the GND of the driver board, and the negative terminal of the battery with Dupont wires, so that the signals can be connected.
The wiring may look simple, but it actually has a hidden secret. Taking L298N as an example, the digital pins connected from IN1 to IN4 are no problem; but many people forget to connect ENA and ENB. If these two enable pins are left floating, the motor will never rotate. The correct way is to connect ENA and ENB to the PWM pins (such as 3, 5, 6, 9) so that you can adjust the speed. Also, if the motor wires are connected backwards, it is not a fault if the motor rotates reversely. For forward and reverse rotation, the high and low levels of the two IO ports are changed by the program. There is no need to unplug the wires and plug them in again. The most hidden pit is "common ground". As mentioned before, signals from different ground cannot get through.
When connecting the servo, pay attention to three wires: brown or black is the ground wire, red is the power supply, and orange or yellow is the signal wire. Don't confuse the signal wires with the power wires. Burning the servo is a trivial matter, but burning the pins is troublesome. If your servo vibrates or does not turn after being connected, it is most likely that the power supply is insufficient. Try connecting a separate 5V/2A power supply. If it works, it's a power supply problem. There is another detail: before powering on, it is best to check whether there is a short circuit, especially for metal-cased motors. Do not let the casing touch any lines, otherwise it will smoke every minute.
When writing code, don't make it complicated at the beginning. First write a simple "forward rotation for 2 seconds, stop for 1 second, and reverse for 2 seconds" to try it out. The basic framework is as follows: In the setup, set all the control pins to HIGH, and set the enable pin to HIGH (or use an initial value). Then in the loop, the control direction is through the combination of high and low levels of the two IN pins. For example, IN1 HIGH and IN2 LOW are forward rotation, and the reverse is reverse rotation. Use (, speed) to adjust the speed. The speed range is 0-255. The larger the number, the faster it is. Note that it is best to slowly increase the speed from 0 when first powered on, otherwise the motor will rush suddenly and the current will be too high.
There are two more tips when writing code. First, be sure to add a stop function, such as (in1, LOW); (in2, LOW); so that the motor is in a free state and will not continue to heat up. Second, don’t write too many delays in the code, especially when you want to control multiple motors at the same time, delays will cause all actions to get stuck. You can use a () timer, like a running light, to let the program run "non-blocking". The steering gear is even simpler, directly#
There are two core things to control speed and direction: PWM and H-bridge. PWM (Pulse Width Modulation) sounds sophisticated, but it is actually "fast switching". If you give it a number from 0 to 255, it corresponds to a duty cycle of 0% to 100%. 255 means always on, and 0 means always off. For example, if you set it to 128, it will be powered on half of the time and off half of the time. The average voltage of the motor will be half, and the speed will naturally be half. This is why we need to connect the enable pin to the PWM pin, because PWM can output such a rapidly changing signal.
The direction control relies on the H-bridge circuit. There are four switches inside the driver board. You control their opening and closing through the two IO ports, and the current can flow from one end of the motor to the other end, achieving forward rotation; vice versa. Never set both IN1 and IN2 to HIGH at the same time in the program. It is equivalent to a short circuit, and the driver board will get hot or even burn out. If you want to achieve a slow start, you can use a for loop, such as for(int i=0;i
The biggest difference between a steering gear and an ordinary motor is that it has a position feedback system inside it. You tell it how much to turn, and it will work hard to turn to that angle and maintain it. The servos are controlled using a 50Hz PWM signal, but the frequency is fixed and what we change is the pulse width. Fortunately, the Servo library has encapsulated all of this, and you only need to write the angle. But please note that not all servos can rotate 180 degrees, some can only rotate 90 degrees, and some can rotate continuously. When buying, read the parameters clearly and don’t buy the wrong one.
There are several pitfalls you need to be aware of when using a servo. The first is the "emergency stop" problem. If you suddenly make the servo jump from 0 degrees to 180 degrees, it will consume a huge amount of current in an instant. It is best to use a for loop to change the angle step by step. The second is reset. The servo will automatically go to the last stored position when the power is turned on. If that position is at the extreme angle, the gears may click. Therefore, it is recommended to write it in the setup first, such as 90 degrees, and then perform other actions. The third one is multi-server control. If you control more than 3 9g servos at the same time, don't draw power from it, use an external power supply module, otherwise it will restart randomly.
Having said so much, there is only one sentence: split the motor control into three parts: "select the driver, connect the wires, and write the program." Pay attention to the details that are easy to overturn in each part, and you will soon be able to play with various motors. Finally, I want to ask you: What was the most outrageous rollover situation you encountered when you used the control motor for the first time? Let’s chat in the comment area so that everyone can avoid pitfalls!
Update Time:2026-03-20
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.