How to Use L298N Motor Driver With Pico W

by mahmoodmustafashilleh in Circuits > Microcontrollers

66 Views, 1 Favorites, 0 Comments

How to Use L298N Motor Driver With Pico W

IMG_0628-ezgif.com-video-to-gif-converter.gif
How to Control DC Motors with L298N and Raspberry Pi Pico W in MicroPython | Full Tutorial

In this tutorial, we’ll explore how to control DC motors using the L298N motor driver and the Raspberry Pi Pico W, with code written in MicroPython. The L298N is a powerful dual H-bridge motor driver that lets you control the direction and speed of two DC motors simultaneously. It’s commonly used in robotics and automation projects because it can handle higher currents and voltages than a microcontroller alone, making it an essential component for projects that involve motors.

In this project, we’ll connect the L298N to the Raspberry Pi Pico W, using GPIO pins to control the motor’s direction and a PWM (Pulse Width Modulation) pin to control the motor’s speed. By the end of this tutorial, you’ll have a functioning motor that can move forward and backward, with speed control based on your input.

Why Use the L298N Motor Driver?

The L298N motor driver makes it possible to control high-power motors using the low-power outputs of microcontrollers like the Raspberry Pi Pico W. It can independently control the speed and direction of two motors. The driver is particularly useful for handling larger currents and voltages that would otherwise damage a microcontroller’s GPIO pins. The H-bridge design allows you to control the direction of current flow, enabling you to easily reverse motor direction.

Supplies

Connecting the L298N to the Raspberry Pi Pico W


Before we dive into the code, let’s wire up the Pico W and the L298N motor driver:

Power Connections:

  1. Connect your 12V power supply to the 12V IN and GND pins of the L298N driver.
  2. Connect Pico W to separate power supply externally

Motor Control Connections:

  1. IN3 on the L298N to GPIO 14 on the Pico W (for motor direction control).
  2. IN4 on the L298N to GPIO 15 on the Pico W (for motor direction control).
  3. ENB on the L298N to GPIO 13 on the Pico W (for PWM speed control).

Motor Connection:

  1. Connect the DC motor’s leads to the OUT3 and OUT4 terminals on the L298N motor driver.

Writing the MicroPython Code

Now that the wiring is complete, let’s write the MicroPython code to control the motor. The following script allows us to move the motor forward and backward while controlling the speed using PWM.

from machine import Pin, PWM
from time import sleep

# Motor pins
in3 = Pin(14, Pin.OUT) # IN3 connected to GPIO 14
in4 = Pin(15, Pin.OUT) # IN4 connected to GPIO 15
enb = PWM(Pin(13)) # ENB connected to GPIO 13 for PWM control
enb.freq(1000) # Set PWM frequency to 1 kHz
# Motor functions
def motor_forward(speed):
in3.high() # Set IN3 high
in4.low() # Set IN4 low
enb.duty_u16(speed) # Set motor speed (0-65535 for duty cycle)
def motor_backward(speed):
in3.low() # Set IN3 low
in4.high() # Set IN4 high
enb.duty_u16(speed) # Set motor speed (0-65535 for duty cycle)
def motor_stop():
in3.low() # Stop motor
in4.low() # Stop motor
enb.duty_u16(0) # Disable motor (set PWM duty cycle to 0)
# Main loop to test motor
try:
while True:
print("Moving forward")
motor_forward(32768) # Set speed to 50% (PWM value)
sleep(2)
print("Stopping")
motor_stop()
sleep(2)
print("Moving backward")
motor_backward(32768) # Set speed to 50% (PWM value)
sleep(2)
print("Stopping")
motor_stop()
sleep(2)
except KeyboardInterrupt:
motor_stop()
print("Program stopped")

Code Breakdown:

  1. IN3 and IN4: These pins control the direction of the motor. If IN3 is high and IN4 is low, the motor moves forward. If IN3 is low and IN4 is high, the motor moves backward.
  2. ENB: This is the enable pin for the motor connected to OUT3 and OUT4. By using PWM (Pulse Width Modulation) on this pin, we can control the motor’s speed.
  3. duty_u16(): This function controls the duty cycle of the PWM signal. A value of 65535 represents 100% speed, while 0 represents no speed.

The motor_forward, motor_backward, and motor_stop functions allow us to easily control the motor’s movement and speed. You can modify the duty_u16 value to control the speed of the motor as needed.

Running the Code

Once the wiring and code are ready, you can upload this script to the Raspberry Pi Pico W using a MicroPython editor like Thonny. After running the code, the motor should alternate between moving forward and backward with a 2-second pause in between.

You can modify the motor_forward() and motor_backward() functions to adjust the speed and movement duration as per your project’s requirements.

Conclusion

This tutorial covers the basics of controlling a DC motor with the L298N and Raspberry Pi Pico W. From here, you can expand on this by adding additional motors, sensors, or controls to build more complex robotics projects. You could, for example, add a joystick or buttons to control the motor interactively or integrate line-following sensors to create a basic robot.

Motor control with MicroPython is a fun and practical way to get started with robotics, and the L298N motor driver offers the flexibility to drive motors with precision. I hope this tutorial helped you get your motor up and running. Stay tuned for more projects!