07
advanced+400 XP
Arduino PWM Motor Speed
Control DC motor speed and direction with Arduino and an L298N driver.
🎯 Objective
Use PWM to smoothly accelerate and decelerate a motor, and reverse its direction.
📖 Background Theory
**PWM Motor Control:**
analogWrite(ENA, speed) where speed is 0-255 controls the motor's speed.
**Acceleration ramp:**
Suddenly starting a motor at full speed can stress the gearbox. Ramping up gradually is better:
for (int speed = 0; speed <= 255; speed++) { analogWrite(ENA, speed); delay(10); }
**Reading motor current:**
In real circuits, you can read current with a current sensor (ACS712) and use it for load detection.
**PWM frequency:**
Arduino's default PWM frequency is ~490Hz (pins 3,9,10,11) or ~980Hz (pins 5,6). Higher frequency = smoother motor operation.
🔧 Components You'll Use
Arduino UnoL298nDc MotorPotentiometerPush ButtonBatteryGround
📋 Steps (5)
- 1Build on Mission 4 — Arduino + L298N + DC Motor
- 2Add a Potentiometer on A0 for speed control
- 3Add a Push Button on D2 for direction toggle
- 4Paste the advanced starter code
- 5Run simulation: turn pot for speed, press button for direction