Motors and L298N driver
Two motors with wheels and the driver that controls them: forward, back, turns and speed, for a robot car.
The test program
Test this part on its own before you build the rest: if it works alone, the rest is built on something you know works.
10 MOVE 150 150
20 WAIT 1000
30 MOVE -150 -150
40 WAIT 1000
50 MOVE 150 -150
60 WAIT 1000
70 MOVE 0 0
80 ENDWhat you should see: With the wheels off the table: forward for a second, back for a second, a spin on the spot, then it stops.
▶ Try it in the IDEWhat to buy
- 2WD robot chassis (motors + wheels)The 2WD kit usually includes the motors, wheels and a 4x AA holder. TT gear motors, ~6V. Want 4 wheels? A 4WD chassis works with the SAME program: wire the two motors of each side in parallel on the L298N (OUT1/2 left, OUT3/4 right).
- L298N motor driverRemove the ENA/ENB jumper caps, or speed control won't work and MOVE 0 0 can't stop the motors.
- 4x AA battery holder
How to wire it to an ESP32-S3
- Battery + (red) → L298N: +12V (VMS)Your 4×AA box gives 6V: fine for the 3-6V motors.
- Battery − (black) → L298N: GND
- L298N: GND → ESP32-S3: GNDCommon ground: this link is mandatory.
- Left motor (2 wires) → L298N: OUT1 · OUT2
- Right motor (2 wires) → L298N: OUT3 · OUT4
- L298N: ENA → ESP32-S3: GPIO12Remove the little jumper cap over this L298N pin (ENA/ENB) so the speed is controlled.
- L298N: IN1 → ESP32-S3: GPIO10
- L298N: IN2 → ESP32-S3: GPIO11
- L298N: ENB → ESP32-S3: GPIO21Remove the little jumper cap over this L298N pin (ENA/ENB) so the speed is controlled.
- L298N: IN3 → ESP32-S3: GPIO13
- L298N: IN4 → ESP32-S3: GPIO14
If it does not work
- The board restarts by itself
- When they start, the motors pull the voltage down and the board restarts. Give the motors their own battery, wired to the driver, with GND in common with the board; a capacitor across the motors helps.
- The program runs but nothing happens
- The driver's battery is off, or the motor wires are not tight in the terminals. Switch the battery holder on and tighten the driver's OUT1, OUT2, OUT3 and OUT4 terminals.
What you can adjust
- The wheels' speed, from 0 to 255, and how long a turn lasts.
Which boards it works on
- ESP32-S3 Freenove: yes
- Nordic nRF54LM20 DK: yes
- Arduino UNO R4 WiFi: no, no motors