Home > Industry Insights >BLDC
TECHNICAL SUPPORT

Product Support

Arduino Control DC Motor Tutorial Driver Module Wiring Code Speed Adjustment Method

Published 2026-03-09

Want to control a DC motor, but face a bunch of lines and codes and don’t know where to start? Don’t worry, this is a hurdle that almost every maker encounters when getting started. In fact, as long as you understand the core principles, set up the circuit, and use a few simple lines of code, you can make the motor obedient and realize forward rotation, reverse rotation, and even speed regulation.

Is a drive module necessary to control a DC motor?

Many friends wanted to connect the motor directly to the pins, but found that not only could it not rotate, but the chip was also hot. This is mainly because the GPIO pins can only provide tens of milliamps of current, while the DC motor may require hundreds of milliamps or even more to operate. Direct driving may burn the pins at best, or damage the entire development board at worst. Therefore, the motor drive module has become an essential bridge. It is like a high-power switch, responsible for sending signals to the drive module, which then independently supplies energy to the motor, which is both safe and efficient. The commonly used L298N or modules are good choices. They have integrated H-bridge circuits inside, which facilitates us to control the direction of the motor.

How to choose the right DC motor for your project

When you walk into an electronics market or open a shopping website, you will be dazzled by the various models of DC motors. When choosing, you can't just look at the appearance. You must first clarify your project needs. For example, if you are making a small fan and do not have high torque requirements, just choose an ordinary 130 or 370 motor. But if you want to build a smart car, especially one that needs to climb hills, you have to consider a motor with a reduction gearbox, such as the common TT motor, which has high torque and moderate speed. Also don’t forget the working voltage, which usually outputs 5V, so try to choose a motor with a rated voltage between 3V and 9V, so that it can be driven directly with a battery box or a 5V output port, saving you the trouble of an additional power supply.

How to use code to realize forward and reverse rotation of the motor

It is easy for a motor to rotate, but to make it rotate accurately forward and reverse under your control, you have to rely on code to command the drive module. Take the commonly used L298N module as an example. It usually has two logical input pins, IN1 and IN2. In the code, you only need to set IN1 to HIGH and IN2 to LOW, and the motor will rotate in one direction. Conversely, if IN1 is set to LOW and IN2 is set to HIGH, the motor will rotate in the opposite direction. This is actually using the H-bridge circuit to switch the direction of current flowing through the motor. You can simply write a few lines inloopfunction, and combined withdelay, you can create a small program that first rotates the motor forward for a few seconds, and then reverses for a few seconds, which is very intuitive.

How to accurately control motor speed through PWM

Just being able to rotate is not enough. Many projects require the motor to slow down or run faster. This is where the PWM (Pulse Width Modulation) function is used. You can think of PWM as a switch that quickly turns the light on and off. The higher the ratio of the switch, the brighter the light, which corresponds to the faster the motor. On the drive module, in addition to the direction control pin, there is usually an enable pin (ENA). Connect this pin to a pin that supports PWM (pin marked~), then use the(pin, speed)function in the code to set the speed value between 0 and 255, and you can easily control the motor speed. For example, if you want it to run at half speed, just write 128, which is very convenient.

How to connect the power supply so that it is stable and does not burn the board

The circuit connection is the most likely to go wrong in the entire process. Remember one principle:separate strong electricity and weak electricity. It can be powered by USB or its 5V pin. The motor consumes a lot of power and must use an independent power source, such as several dry batteries or a lithium battery pack. Connect the positive and negative poles of the battery pack to the power input port of the drive module (such as the 12V and GND of the L298N), and don't forget to connect the ground (GND) and ground (GND) of the drive module together. It's like if two people want to lift something together, they must stand on the same level so that they are on the same ground so that the signal transmission can be stable and there will be no malfunction.

What are the common pitfalls to avoid when actually doing it?

Now that we're done talking on paper, there are still a few details that we need to pay attention to when we actually do it. The first is the diode. If you are using a slightly larger motor, it is best to connect a diode (freewheeling diode) in reverse parallel at both ends of the motor. It can absorb the reverse voltage generated when the motor starts and stops and protect the drive circuit. The second is that the wiring is firm and the motor vibrates greatly. The wires plugged into the breadboard can easily come loose, causing program execution errors. It is best to use a soldering iron to solder the wires. The third step is debugging. If the motor does not respond, don’t rush to change the code first. Use a multimeter to measure whether the logic voltage of the drive module and the motor supply voltage are normal. This can often help you find the problem the fastest.

After reading this, do you think that controlling a DC motor is not as complicated as you think? What interesting function would you most like to use motors to achieve in your own projects? Welcome to chat about your creativity in the comment area, like it and save it so you don’t have to worry about finding tutorials when you try it!

Update Time:2026-03-09

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
WhatsApp Message
+86 0769 8399 3238
 
kpowerMap