小编
Published2025-10-15
Sure! Here's the first part of the article based on your request:
Introduction to DC Motors and Arduino
If you're stepping into the world of electronics and robotics, learning how to control a 12V DC motor using Arduino is an excellent project to begin with. DC motors are essential components in various applications, from robotics to simple mechanical systems. A DC motor is powered by a direct current, which gives it the ability to rotate and perform work. However, unlike small motors that are powered directly from a microcontroller like an Arduino, a 12V motor requires a different approach due to the higher voltage.

Arduino, with its ease of use and versatility, makes the process of controlling motors simple and accessible. By the end of this tutorial, you’ll have the skills to control the speed, direction, and even the rotation of a 12V DC motor with ease. But before jumping into the coding part, let’s first understand the basics of controlling a 12V DC motor.
Understanding Motor Drivers and Arduino's Limitations
A crucial point to understand is that Arduino boards like the Uno or Nano cannot provide the necessary power to directly drive a 12V DC motor. The microcontroller outputs 5V from its digital pins, which isn’t enough to control a 12V motor. Therefore, we need a motor driver.
A motor driver acts as an intermediary between the Arduino and the motor. It receives low-voltage signals from the Arduino and uses them to switch a higher voltage on and off, powering the motor. A commonly used motor driver for controlling a 12V DC motor is the L298N motor driver, which is capable of handling up to 2A of current and can drive two DC motors simultaneously. Using the L298N, you can control both the speed and direction of the motor.
Here’s a list of the essential components for this project:
Arduino board (Uno, Nano, etc.)
External 12V power supply
Diodes (optional, for back-emf protection)
Push buttons (optional for direction control)
Before you start building the circuit, it’s important to make sure you understand how each component works. The motor driver acts as the bridge between your low-power Arduino and the high-power 12V motor.
Wiring the Circuit: Step-by-Step
The wiring is a crucial part of this project. Let’s go through the connections needed to control the 12V DC motor using an Arduino and L298N motor driver.
Connecting the Motor Driver to the Arduino:
Pin IN1 on the L298N motor driver goes to Pin 9 on the Arduino.
Pin IN2 on the L298N goes to Pin 8 on the Arduino.
Pin ENA (Enable Pin) on the L298N should be connected to 5V. This pin controls the speed of the motor, and when set to 5V, the motor will be enabled.
Pin OUT1 connects to one terminal of the motor.
Pin OUT2 connects to the other terminal of the motor.
Powering the Motor and Arduino:
The 12V DC motor is powered by an external power supply. Connect the positive lead of the 12V supply to Pin 12V of the L298N, and the negative lead to GND.
The Arduino is powered through its USB port or by a separate 5V supply (if not using USB power).
For safety and protection from voltage spikes caused by the motor, you may add diodes across the motor terminals to prevent damage from back-emf.
Optionally, you can use push buttons for controlling the direction of the motor.
With these connections, the Arduino can control the motor’s speed and direction. The next step is to set up the code to make everything work.
Now that the hardware is set up, it’s time to program the Arduino. In this section, we will write code to control the motor’s rotation and speed. The key here is using PWM (Pulse Width Modulation) to vary the speed of the motor. PWM allows you to adjust the average voltage supplied to the motor by turning it on and off rapidly at a high frequency.
Here’s a basic example of Arduino code to control a 12V DC motor using the L298N driver:
const int motorPin1 = 9; // IN1 on L298N
const int motorPin2 = 8; // IN2 on L298N
const int enablePin = 5; // ENA on L298N (connected to 5V for max speed)
// Set motor pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
// Rotate the motor in one direction
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Keep the motor running for 5 seconds
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(2000); // Wait for 2 seconds
// Rotate the motor in the opposite direction
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(5000); // Run for 5 seconds in reverse
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(2000); // Wait for 2 seconds before the next cycle
This basic code snippet makes the motor rotate in one direction for 5 seconds, stop for 2 seconds, and then rotate in the opposite direction for 5 seconds. The motor will repeat this cycle indefinitely.
In the next part of this guide, we will discuss how to implement speed control using PWM and even how to use external buttons to control the direction. Stay tuned!
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.