How to Use L298N Motor Driver With Pico W
by mahmoodmustafashilleh in Circuits > Microcontrollers
303 Views, 1 Favorites, 0 Comments
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.
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:
- 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.
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.
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.
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!