What You Need to Connect a Motor to Arduino

You cannot connect a motor directly to an Arduino pin. Arduino pins output very little electrical power — about 40 milliamps at most — and most motors need far more. You need three things: a motor driver (a small circuit board that sits between the Arduino and motor), a power supply separate from the Arduino's own power, and jumper wires to connect them together.

The motor driver acts as a translator and amplifier. It takes the small signal from the Arduino pin and uses it to switch a much larger electrical current from the separate power supply through to the motor. Without it, you either get no motion or you damage the Arduino.

The most common motor driver for beginners is the L298N, which costs a few dollars and can handle motors up to about 2 amps. If your motor is smaller or you want something simpler, a transistor can work, but the L298N is more forgiving and lets you control motor direction and speed.

Key Takeaways

  • A motor driver is required between the Arduino and motor because Arduino pins do not supply enough power to run a motor.
  • The L298N motor driver is the most common choice for small projects and costs a few dollars.
  • You need a separate power supply for the motor, not the Arduino's own power.
  • Three Arduino pins control the motor: one for direction, one for speed, and one ground connection.
  • The motor spins when you send the right signal from your Arduino code, using digitalWrite() for on/off or analogWrite() for speed control.

Wiring the L298N Motor Driver to Arduino

The L298N has two sides: one side connects to the Arduino, the other to the motor and its power supply. Start with the Arduino side. Connect a GND pin on the Arduino to a GND pin on the L298N — this creates a shared ground so both devices speak the same electrical language. Then connect two Arduino digital pins (for example, pins 8 and 9) to the IN1 and IN2 inputs on the driver. These pins tell the motor which direction to spin.

If you want to control motor speed, also connect an Arduino pin that supports PWM (pulse width modulation) — pins 3, 5, 6, 9, 10, or 11 work — to the ENA pin on the driver. PWM lets you send a signal that turns the motor on and off very fast, making it spin slower or faster depending on the pattern.

On the motor side of the L298N, connect your motor's two wires to the OUT1 and OUT2 terminals. Then connect the positive wire from your separate power supply to the +12V (or +5V, depending on your motor) terminal, and the negative wire to a GND terminal on the driver. Make sure the ground from the power supply also connects back to the Arduino's GND — all three devices (Arduino, driver, power supply) must share the same ground.

Choosing the Right Power Supply

The power supply voltage and current depend on your motor. Small DC motors typically run on 5 to 12 volts. Check your motor's label or datasheet to find the rated voltage — if it says 6V, use a 6V supply; if it says 12V, use 12V. Using the wrong voltage can burn out the motor or make it spin too slowly.

Current is equally important. A motor's current draw depends on how hard it is working. A small hobby motor might draw 0.5 amps under load, while a larger one could draw 2 or 3 amps. Your power supply must be rated for at least as much current as the motor needs, or it will shut down or overheat. A USB power bank works for very small motors, but for anything larger, use a dedicated power supply rated for the voltage and current your motor requires.

Never power the motor from the Arduino's own power supply. The Arduino gets its power from USB or a small wall adapter, and drawing motor current through it will cause the Arduino to reset or behave erratically.

Writing Arduino Code to Control the Motor

Once the wiring is complete, the Arduino code is straightforward. Start by declaring which pins you used. If you connected IN1 to pin 8, IN2 to pin 9, and ENA to pin 5, your setup looks like this:

int motorPin1 = 8; int motorPin2 = 9; int enablePin = 5; void setup() {   pinMode(motorPin1, OUTPUT);   pinMode(motorPin2, OUTPUT);   pinMode(enablePin, OUTPUT); }

To make the motor spin forward, set one pin HIGH and the other LOW. To reverse direction, flip them. To control speed, use analogWrite() on the enable pin with a value from 0 (stopped) to 255 (full speed). For example, analogWrite(enablePin, 200) makes the motor spin at about 78 percent power.

A straightforward function to spin the motor forward at full speed looks like this:

void motorForward() {   digitalWrite(motorPin1, HIGH);   digitalWrite(motorPin2, LOW);   analogWrite(enablePin, 255); }

To stop the motor, set both pins LOW or set the enable pin to 0.

Troubleshooting When the Motor Does Not Spin

If you wire everything correctly but the motor does not move, check these things in order. First, verify the power supply is actually on and delivering voltage — use a multimeter if you have one, or check that an LED on the power supply is lit. Second, swap the motor wires at OUT1 and OUT2 — sometimes the motor just needs reversed polarity to work. Third, check that your Arduino code is actually running by adding an LED to one of the control pins; if the LED blinks, the code is working and the problem is in the motor or driver.

If the motor hums but does not spin, the power supply current is probably too low. The motor is trying to turn but does not have enough current to overcome friction. Upgrade to a power supply rated for more amps. If the motor spins but very slowly even at full speed, the voltage might be too low — check that your power supply matches the motor's rated voltage.

If the Arduino resets when you turn on the motor, the motor is drawing power from the Arduino's supply instead of the separate one. Check that the motor's power supply ground is connected to the Arduino's ground, and that the motor wires go to OUT1 and OUT2, not directly to Arduino pins.

Controlling Motor Speed Without PWM

If your Arduino board does not have enough PWM pins or you want a simpler approach, you can control speed by switching the motor on and off rapidly in code, though this is less smooth than true PWM. You can also use a potentiometer (a variable resistor) connected to an analog input pin to let a user dial the speed up and down in real time.

For most projects, though, using the PWM pins on the Arduino is simpler and gives better results. The L298N driver handles all the switching automatically, and your code just sends a number from 0 to 255 to set the speed.

Frequently Asked Questions

Can I connect multiple motors to one Arduino?

Yes. Each motor needs its own motor driver (or a driver with multiple channels), and each driver needs its own set of Arduino pins. An L298N can control two motors, so you could run two small motors with one driver and one Arduino. For more motors, add more drivers.

What if my motor is too big for the L298N?

The L298N handles motors up to about 2 amps. If your motor draws more current, you need a larger driver. Look for drivers rated for your motor's current — common options include the BTS7960 for high-current motors or a relay for very large motors. The wiring concept stays the same; only the driver changes.

Do I need a capacitor or resistor with the motor driver?

For small hobby projects, no. For reliable operation in noisy environments or with larger motors, a capacitor across the motor power supply (0.1 to 1 microfarad) reduces electrical noise. A resistor between the Arduino pin and the driver input can protect the Arduino if something goes wrong, but it is not required for basic setups.

Why does my motor driver get hot?

The L298N generates heat when current flows through it, especially at high current or low speeds. If it is too hot to touch, the motor is drawing more current than the driver is rated for, or the driver does not have a heat sink. Add a small aluminum heat sink to the driver chip, or upgrade to a driver rated for higher current.

Can I use a relay instead of a motor driver?

Yes, a relay is a switch that lets an Arduino pin control a larger electrical circuit. Relays work well for on/off control but do not let you adjust speed smoothly. A motor driver is better for speed and direction control, but a relay is simpler if you only need the motor to run or stop.