The basic wiring: what connects where
An Arduino cannot power a motor directly from its output pins — the pins supply only 40 milliamps at 5 volts, and most motors draw far more current. You need a motor driver, a separate circuit board that sits between the Arduino and the motor. The motor driver takes a small signal from the Arduino and uses it to switch a larger power supply on and off, sending that power to the motor.
The most common setup uses an L298N motor driver module, which costs a few dollars and works with DC motors up to 2 amps. Connect the Arduino's ground pin to the motor driver's ground. Connect two Arduino digital output pins (for example, pins 9 and 10) to the motor driver's input pins — these pins tell the driver which direction to spin the motor. Connect the motor's two wires to the motor driver's output terminals. Finally, connect a separate power supply (a battery pack or wall adapter) to the motor driver's power input — this power runs the motor, not the Arduino.
If your motor is very small (under 500 milliamps), you can use a simpler transistor-based driver with just a single transistor and a resistor, but an L298N module is more reliable and costs almost nothing more.
Key Takeaways
- Arduino pins cannot supply enough current to run a motor, so you must use a motor driver module as an intermediary between the Arduino and the motor.
- An L298N motor driver module is the standard choice for small DC motors and requires only two Arduino pins plus a separate power supply for the motor.
- Ground connections matter: the Arduino's ground must connect to the motor driver's ground so both devices share a common reference point.
- The motor draws power from a separate battery or power supply connected to the motor driver, not from the Arduino itself.
Wiring an L298N module step by step
Start with the power connections. Take a jumper wire from the Arduino's GND (ground) pin to the GND pin on the L298N. This creates a common ground between the two devices. Then connect your external power supply — a 9-volt battery pack, a wall adapter, or a USB power bank — to the +12V and GND terminals on the motor driver. The voltage you choose depends on your motor's rating; check the motor's label or datasheet.
Next, connect the signal pins. Plug a jumper wire from Arduino pin 9 to the L298N's IN1 pin, and another from Arduino pin 10 to IN2. These two pins control the motor's direction: sending power to IN1 spins it one way, sending power to IN2 spins it the other way. Finally, connect the motor's two wires to OUT1 and OUT2 on the motor driver. The order does not matter for direction control — if the motor spins backward, swap the two wires.
Double-check that the motor driver's power supply is disconnected before you plug in the Arduino. Once everything is wired, connect the Arduino to your computer via USB and upload your code.
Writing code to spin the motor
The simplest code sets up two pins as outputs and then writes HIGH or LOW to control the motor. Here is a basic example that spins the motor forward for two seconds, then backward for two seconds, then stops:
void setup() { pinMode(9, OUTPUT); pinMode(10, OUTPUT); } void loop() { digitalWrite(9, HIGH); digitalWrite(10, LOW); delay(2000); digitalWrite(9, LOW); digitalWrite(10, HIGH); delay(2000); digitalWrite(9, LOW); digitalWrite(10, LOW); delay(1000); }
In this code, setting pin 9 HIGH and pin 10 LOW tells the motor driver to spin the motor in one direction. Reversing those (pin 9 LOW, pin 10 HIGH) spins it the other way. Setting both pins LOW stops the motor. The delay() function pauses for a number of milliseconds — 2000 means two seconds.
To control the motor's speed, use PWM (pulse-width modulation) instead of digitalWrite. PWM pins on the Arduino are marked with a tilde (~) — pins 3, 5, 6, 9, 10, and 11 support it. Use analogWrite(pin, value) where value ranges from 0 (stopped) to 255 (full speed). For example, analogWrite(9, 128) spins the motor at half speed.
Choosing the right power supply for your motor
The motor driver can handle the switching, but the power supply must match your motor's needs. Check your motor's label or datasheet for its rated voltage and current draw. A small hobby motor might run on 6 volts and draw 200 milliamps; a larger one might need 12 volts and 1.5 amps. The L298N module itself can handle up to 2 amps and voltages from 5 to 35 volts.
A 9-volt battery pack works for small motors but drains quickly under load. A USB power bank rated for 2 amps or more is more practical for testing. For permanent installations, a wall adapter (sometimes called a "wall wart") rated for your motor's voltage and current is most reliable. Make sure the adapter's output matches — a 12-volt, 2-amp adapter is suitable for a 12-volt motor drawing up to 2 amps.
Never run the motor from the Arduino's 5-volt output or USB power. The Arduino's power supply is designed only for the microcontroller and small sensors, not for motors.
Troubleshooting: motor does not spin
If the motor does not spin after uploading your code, check the power supply first. Make sure the external power is actually connected to the motor driver and switched on. Use a multimeter to verify that voltage is reaching the motor driver's power input terminals.
Next, verify the ground connection. The Arduino's GND and the motor driver's GND must be connected by a wire — this is not optional. Without a common ground, the signal pins will not work correctly even if they appear to be wired right.
Then test the signal pins. Upload a simple sketch that sets pin 9 to HIGH and leaves it there. Use a multimeter or an LED with a resistor to check whether pin 9 is actually outputting 5 volts. If it is not, the Arduino pin may be damaged or the code may have an error. If it is, the motor driver itself may be faulty.
Finally, check the motor. Disconnect it from the motor driver and try spinning the shaft by hand — it should turn freely. If it is stuck, the motor may be damaged. If it spins freely, reconnect it and try swapping the two wires to the motor driver's output terminals; sometimes the motor is wired in a way that requires this.
Using a relay for high-power motors
If your motor draws more than 2 amps or runs at a voltage higher than 35 volts, an L298N will not work. Instead, use a relay module, which is a switch controlled by the Arduino. A relay can handle much larger currents and voltages because it uses an electromagnet to flip a mechanical switch — the Arduino controls the electromagnet, not the motor power directly.
A relay module wires similarly to an L298N: ground from Arduino to relay, signal pins from Arduino to relay inputs, and the motor power supply connected to the relay's power terminals. The trade-off is that a relay can only turn the motor fully on or fully off — it cannot control speed the way PWM does with an L298N. For speed control with high-power motors, you need a specialized high-current motor driver, which is more expensive.
Frequently Asked Questions
Can I connect a motor directly to an Arduino pin?
No. Arduino pins output a maximum of 40 milliamps at 5 volts. Most motors draw far more current and will either not spin or damage the Arduino pin. You must use a motor driver or relay module between the Arduino and the motor.
What is the difference between an L298N and a relay?
An L298N motor driver can control both speed and direction using PWM. A relay can only turn a motor fully on or off. An L298N works best for small to medium motors (under 2 amps); a relay is necessary for very high-power motors or when you need to switch large voltages.
Do I need a separate power supply for the motor?
Yes. The Arduino's power supply is designed only for the microcontroller and small sensors. Motors must have their own power source connected to the motor driver. This power supply should match your motor's voltage and current rating.
Why does my motor spin backward?
The motor is wired with its two leads reversed relative to the motor driver's output. Swap the two wires where they connect to the motor driver's output terminals. The direction will reverse.
Can I control multiple motors with one Arduino?
Yes. Each motor needs its own motor driver module, but a single Arduino has enough pins to control several motors. For example, an Arduino Uno has 14 digital pins, so you could control up to seven motors (using two pins per motor for direction control). Use PWM pins if you also need speed control.