How to Use L298N Motor Driver with Pico W

 

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.

What You’ll Need:

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.

Step 1: 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:

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

Motor Control Connections:

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

Motor Connection:

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

Step 2: 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:

  • 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.
  • 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.
  • 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.

Step 3: 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!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Implementing Google reCAPTCHA in a Simple React and Node.js App

Implementing Google reCAPTCHA in a Simple React and Node.js App

Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.