Actuator

โš™๏ธ L298N Motor Driver

๐Ÿ“Œ Description

The L298N is a special board that lets the robot control motors. It acts like a bridge between the brain (the microcontroller) and the motors, giving them the right amount of power and direction.

๐ŸŽฏ Use

In our projects, the L298N is used to:

  • Make wheels spin forward or backward.
  • Control the speed and direction of motors.
  • Drive one or two motors at the same time.

๐Ÿ‘‰ Think of the L298N as the robotโ€™s driverโ€™s license โ€” it allows the robot to move safely and correctly.

Module parametersย Click here to return to the table of contents

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
IN1 Motor control signal pin A1
IN2 Motor control signal pin A2
IN3 Motor control signal pin B1
IN4 Motor control signal pin B2
  • Supply voltage: 7V/26V
  • Connection: PH2.0 terminal wire
  • Installation method: double screw fixing


๐Ÿ–ฅ๏ธ Code Explanation

import machine
import time

pin16 = machine.Pin(16, machine.Pin.OUT)   # Motor control pin A1
pin17 = machine.Pin(17, machine.Pin.OUT)   # Motor control pin A2

while True:
    pin16.value(1)     # Motor spins forward
    pin17.value(0)
    time.sleep(2)      # Keep spinning for 2 seconds

    pin16.value(0)     # Motor spins backward
    pin17.value(1)
    time.sleep(2)      # Keep spinning for 2 seconds

Step by Step

  1. import machine & import time โ†’ Tools to control pins and add pauses.
  2. pin16 and pin17 โ†’ These two pins decide the motorโ€™s direction.
  3. while True: โ†’ Loop that repeats forever.
  4. pin16.value(1), pin17.value(0) โ†’ Motor spins forward for 2 seconds.
  5. pin16.value(0), pin17.value(1) โ†’ Motor spins backward for 2 seconds.
    ๐Ÿ‘‰ Together, the motor alternates between forward and backward movement.

โœ… Conclusion of the Test

  • If the motor spins forward for 2 seconds, then backward for 2 seconds, the program is working correctly.
  • This test shows how the L298N can control motor direction using two pins.
  • Later, you can add a second motor by connecting it to the other inputs of the L298N, allowing the robot to move both wheels independently.
On this page