Learn how to control a DC Motor speed and direction using the L298N motor driver with the Raspberry Pi Pico programmed with MicroPython. DC motors are popular in electronics for robot projects and beyond. You’ll understand how to make it move forward, backward, stop, and control its speed by creating your own MicroPython module.
New to the Raspberry Pi Pico? Read the following guide: Getting Started with Raspberry Pi Pico (and Pico W).
Table of Contents:
Throughout this tutorial, we’ll cover the following contents:
- Introducing the L298N Motor Driver
- L298N Motor Driver Pinout
- Control DC Motors with the L298N
- Wiring the DC Motor and L298N to the Raspberry Pi Pico
- Creating a MicroPython Module to Control the DC Motor
- Control a DC Motor with the Raspberry Pi Pico – MicroPython Code
Our Raspberry Pi Pico eBook: Learn Raspberry Pi Pico/Pico W with MicroPython
Prerequisites
Before continuing, make sure you follow the next prerequisites.
MicroPython Firmware
To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
Alternatively, if you like programming using VS Code, you can start with the following tutorial:
Parts Required
You’ll also need the following parts:
- Raspberry Pi Pico
- L298N motor driver
- Mini DC Motor
- 0.1 µF ceramic capacitor
- Jumper wires
- 4x AA Battery Holder
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Introducing the L298N Motor Driver
There are several ways to control DC motors. We’ll use the L298N motor driver, but any similar motor driver should work.
The L298N motor driver is shown in the following figure:
Using the L298N motor driver is suitable for most hobbyist motors that require 6V to 12V to operate. Additionally, it allows you to drive two DC motors simultaneously, perfect if you want to build a robot.
Already familiar with the L298N motor driver? Skip to this section.
L298N Motor Driver pinout
Let’s take a look at the L298N motor driver pinout and see how it works.
The motor driver has a two-terminal block on each side for each motor. OUT1 and OUT2 at the left and OUT3 and OUT4 at the right.
- OUT1: DC motor A + terminal
- OUT2: DC motor A – terminal
- OUT3: DC motor B + terminal
- OUT4: DC motor B – terminal
At the bottom, you have a three-terminal block with +12V, GND, and +5V. The +12V terminal block is used to power up the motors. The +5V terminal is used to power up the L298N chip. However, if the jumper is in place, the chip is powered using the motor’s power supply and you don’t need to supply 5V through the +5V terminal.
Important: despite the +12V terminal name, you can supply any voltage between 5V and 35V (but 6V to 12V is the recommended range).
Note: if you supply more than 12V, you need to remove the jumper and supply 5V to the +5V terminal.
In this tutorial, we’ll use 4 AA 1.5V batteries that combined output approximately 6V, but you can use any other suitable power supply. For example, you can use a bench power supply to test this tutorial.
In summary:
- +12V: The +12V terminal is where you should connect your power supply
- GND: power supply GND
- +5V: provide 5V if jumper is removed. Acts as a 5V output if jumper is in place
- Jumper: jumper in place – uses the motor power supply to power up the chip. Jumper removed: you need to provide 5V to the +5V terminal. If you supply more than 12V, you should remove the jumper
At the bottom right you have four input pins and two enable terminals. The input pins are used to control the direction of your DC motors, and the enable pins are used to control the speed of each motor.
- IN1: Input 1 for Motor A
- IN2: Input 2 for Motor A
- IN3: Input 1 for Motor B
- IN4: Input 2 for Motor B
- EN1: Enable pin for Motor A
- EN2: Enable pin for Motor B
There are jumper caps on the enable pins by default. You need to remove those jumper caps to control the speed of your motors. Otherwise, they will either be stopped or spinning at the maximum speed.
Control DC motors with the L298N Motor Driver
Now that you’re familiar with the L298N Motor Driver, let’s see how to use it to control your DC motors.
Enable pins
The enable pins are like an ON and OFF switch for your motors. For example:
- If you send a HIGH signal to the enable 1 pin, motor A is ready to be controlled and at the maximum speed;
- If you send a LOW signal to the enable 1 pin, motor A turns off;
- If you send a PWM signal, you can control the speed of the motor. The motor speed is proportional to the duty cycle. However, note that for small duty cycles, the motors might not spin, and make a continuous buzz sound.
Related content: Raspberry Pi Pico with MicroPython: Output PWM Signals.
SIGNAL ON THE ENABLE PIN | MOTOR STATE |
---|---|
HIGH | Motor enabled |
LOW | Motor not enabled |
PWM | Motor enabled: speed proportional to the duty cycle |
Input pins
The input pins control the direction the motors are spinning. Input 1 and input 2 control motor A, and input 3 and 4 control motor B.
- If you apply LOW to input1 and HIGH to input 2, the motor will spin forward;
- If you apply power the other way around: HIGH to input 1 and LOW to input 2, the motor will rotate backwards. Motor B can be controlled using the same method but applying HIGH or LOW to input 3 and input 4.
For example, for motor A, this is the logic:
Direction | Input 1 | Input 2 | Enable 1 |
Forward | 0 | 1 | 1 |
Backwards | 1 | 0 | 1 |
Stop | 0 | 0 | 0 |
You might also like reading: Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython).
Controlling 2 DC Motors – ideal to build a robot
If you want to build a robot car using 2 DC motors, these should be rotating in specific directions to make the robot go left, right, forward, or backward.
For example, if you want your robot to move forward, both motors should be rotating forward. To make it go backward, both should be rotating backward.
To turn the robot in one direction, you need to spin the opposite motor faster. For example, to make the robot turn right, enable the motor at the left, and disable the motor at the right. The following table shows the input pins’ state combinations for the robot directions.
DIRECTION | INPUT 1 | INPUT 2 | INPUT 3 | INPUT 4 |
---|---|---|---|---|
Forward | 0 | 1 | 0 | 1 |
Backward | 1 | 0 | 1 | 0 |
Right | 0 | 1 | 0 | 0 |
Left | 0 | 0 | 0 | 1 |
Stop | 0 | 0 | 0 | 0 |
Connecting a DC Motor to the Raspberry Pi Pico
To follow this tutorial, you need to wire one DC motor to the Raspberry Pi Pico via the L298N motor driver. It’s advisable to power up the motor using an independent power supply. Here we’ll be using 4 AA 1.5V batteries that combined output approximately 6V. You can use any suitable power supply that doesn’t exceed 12V.
Wiring the L298N Motor Driver to the Raspberry Pi Pico
The following table shows the connections we’ll make between the Raspberry Pi Pico and the L298N Motor Driver.
L298N Motor Driver | Input 1 | Input 2 | Input 3 | GND |
Raspberry Pi Pico | GPIO 3 | GPIO 4 | GPIO 2 | GND |
You can use any other digital pins. Learn more about the Raspberry Pi Pico GPIOs with our guide: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained.
Use the following diagram as a reference to wire the DC Motor and L298N Motor driver to the Raspberry Pi Pico.
Note: we recommend soldering a 0.1 µF ceramic capacitor to the positive and negative terminals of each motor, as shown in the figure below to help smooth out any voltage spikes.
Additionally, you can solder a slider switch to the red wire that comes from the battery pack. This way, you can turn the power that goes to the motors and motor driver on and off.
The Raspberry Pi Pico should be powered using another power supply (for testing purposes use your computer USB port).
Creating a MicroPython Module to Control a DC Motor
Writing the code to control the DC motor is not difficult, but it requires some repetitive commands. We’ll create a simple Python module with the basic commands to initialize a motor and control its speed and direction.
Creating the dcmotor Module
Before writing the code for this module, it’s important to write down its functionalities:
- The library will be called dcmotor.py, and it will have a single class called DCMotor with several methods.
- We should be able to initialize the motor using input pin 1, input pin 2, and the enable pin as follows:
motor1 = DCMotor(Pin1, Pin2, enable)
Pin1 and Pin2 should be initialized as output pins, and the enable should be initialized as a PWM pin.
- To move the motor forward, we want to use a method called forward() on the DCMotor object that accepts the speed as a parameter. For example:
motor1.forward(speed)
The speed should be an integer number between 0 and 100, in which 0 corresponds to the motor stopped and 100 to maximum speed.
- Similarly, to make the motor go backwards, we want a method called backwards():
motor1.backwards(speed)
- It is also handy to have a command to make the motor stop, as follows:
motor1.stop()
Importing the dcmotor.py Module
Create a new file called dcmotor.py and copy the following code into that file.
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-dc-motor-micropython/
class DCMotor:
def __init__(self, pin1, pin2, enable_pin, min_duty=15000, max_duty=65535):
self.pin1 = pin1
self.pin2 = pin2
self.enable_pin = enable_pin
self.min_duty = min_duty
self.max_duty = max_duty
def forward(self, speed):
self.speed = speed
self.enable_pin.duty_u16(self.duty_cycle(self.speed))
self.pin1.value(1)
self.pin2.value(0)
def backwards(self, speed):
self.speed = speed
self.enable_pin.duty_u16(self.duty_cycle(self.speed))
self.pin1.value(0)
self.pin2.value(1)
def stop(self):
self.enable_pin.duty_u16(0)
self.pin1.value(0)
self.pin2.value(0)
def duty_cycle(self, speed):
if speed <= 0 or speed > 100:
duty_cycle = 0
else:
duty_cycle = int(self.min_duty + (self.max_duty - self.min_duty) * (speed / 100))
return duty_cycle
Upload that file to your Raspberry Pi Pico with the following name: dcmotor.py.
If you’re using Thonny IDE, you can follow the next steps:
1) Copy the code provided to a new file on Thonny IDE.
2) Upload the code to your board. If you’re using Thonny IDE, go to File > Save as… and then select Raspeberry Pi Pico.
3) Save your file as dcmotor.py.
4) Finally, click OK to proceed. The dcmotor.py module should have been uploaded successfully to your board.
How the dcmotor Module Works
The dcmotor module contains a single class called DCMotor.
class DCMotor:
We use the constructor method (__init__) to initiate the data as soon as an object of the DCMotor class is instantiated.
def __init__(self, pin1, pin2, enable_pin, min_duty=15000, max_duty=65535):
self.pin1 = pin1
self.pin2= pin2
self.enable_pin = enable_pin
self.min_duty = min_duty
self.max_duty = max_duty
A DCMotor object accepts as parameters:
- pin1: GPIO (output) that connects to L298N input 1 pin.
- pin2: GPIO (output) that connects to L298N input 2 pin.
- enable_pin: GPIO (PWM pin) that connects to L298N enable 1 pin.
- min_duty: minimum duty cycle to make the motor move. This parameter is optional, and it’s set to 15000 by default. You may need to change this parameter depending on the frequency chosen to control your DC motor.
- max_duty: maximum duty cycle to make the motor move. This parameter is set to 65535 by default.
Then, create several methods to control the DC motor: forward(), backwards(), and stop(). The forward() and backwards() methods make the motor spin forward and backwards, respectively. The stop() method stops the motor. The forward() and backwards() methods accept the speed as a parameter. The speed should be an integer number between 0 and 100.
Let’s take a closer look at the forward() method:
def forward(self, speed):
self.speed = speed
self.enable_pin.duty_u16(self.duty_cycle(self.speed))
self.pin1.value(1)
self.pin2.value(0)
To make the motor move forward, set pin1 to 1 and pin2 to 0. The enable pin’s duty cycle is set to the corresponding speed. The speed is an integer number between 0 and 100, but the duty cycle should be a number between the max_duty and min_duty. So, we have another method at the end of the code, called duty_cycle() that calculates the corresponding duty cycle value based on the speed.
def duty_cycle(self, speed):
if self.speed <= 0 or self.speed > 100:
duty_cycle = 0
else:
duty_cycle = int (self.min_duty + (self.max_duty - self.min_duty)*((self.speed - 1)/(100-1)))
return duty_cycle
Learn more about PWM with the Raspberry Pi Pico: Raspberry Pi Pico: PWM Fading an LED (MicroPython).
The backwards() method works similarly, but pin1 is set to 0, and pin2 is set to 1.
def backwards(self, speed):
self.speed = speed
self.enable_pin.duty_u16(self.duty_cycle(self.speed))
self.pin1.value(0)
self.pin2.value(1)
The stop() method sets the duty cycle, pin1 and pin2 values to 0.
def stop(self):
self.enable_pin.duty_u16(0)
self.pin1.value(0)
self.pin2.value(0)
Controlling the DC Motor
Now that we understand how the DC motor module works, we can create a new script to control the DC motor using the library functionalities.
The following code demonstrates how to use the library’s functionalities to control the DC motor.
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-dc-motor-micropython/
from dcmotor import DCMotor
from machine import Pin, PWM
from time import sleep
frequency = 1000
pin1 = Pin(3, Pin.OUT)
pin2 = Pin(4, Pin.OUT)
enable = PWM(Pin(2), frequency)
dc_motor = DCMotor(pin1, pin2, enable)
# Set min duty cycle (15000) and max duty cycle (65535)
#dc_motor = DCMotor(pin1, pin2, enable, 15000, 65535)
try:
print('Forward with speed: 50%')
dc_motor.forward(50)
sleep(5)
dc_motor.stop()
sleep(5)
print('Backwards with speed: 100%')
dc_motor.backwards(100)
sleep(5)
print('Forward with speed: 5%')
dc_motor.forward(5)
sleep(5)
dc_motor.stop()
except KeyboardInterrupt:
print('Keyboard Interrupt')
dc_motor.stop()
Importing libraries
To use the library’s functionalities, you need to import the library into your code. We import the DCMotor class from the dcmotor library we’ve created previously.
from dcmotor import DCMotor
You also need to import the Pin and PWM classes to control the GPIOs and the sleep() method.
from machine import Pin, PWM
from time import sleep
Setting the frequency
Set the PWM signal frequency to 1000 Hz. You can choose other frequency values. Note that for lower frequency values, you may need to adjust the minimum duty cycle parameter (min_duty).
frequency = 1000
Initializing GPIOs
We create three variables that refer to the motor driver’s pin1, pin2, and enable pins. The pin1 and pin2 are output pins, and the enable is a PWM pin.
pin1 = Pin(3, Pin.OUT)
pin2 = Pin(4, Pin.OUT)
enable = PWM(Pin(2), frequency)
Then, initialize a DCMotor object with the pin1, pin2, and enable defined earlier:
dc_motor = DCMotor(pin1, pin2, enable)
Note: you may need to pass the min_duty and max_duty parameters depending on the motor and board you’re using. For example:
dc_motor = DCMotor(pin1, pin2, enable, 15000, 65535)
Now you have a DCMotor object called dc_motor. You can use the methods to control the motor. Make the motor move forward at 50% speed:
dc_motor.forward(50)
Stop the motor:
dc_motor.stop()
Move the motor backwards at maximum speed:
dc_motor.backwards(100)
Move the motor forward at 5% speed:
dc_motor.forward(5)
Testing the Code
Run this previous code on your Raspberry Pi Pico. If you’re using Thonny IDE, you just need to click on the green run button.
If you want to upload this code to your board and run it when it boots, you need to save it as main.py Go to File > Save as… > Raspberry Pi Pico. Save the file with the name main.py.
The motor will be spinning in different directions and speeds.
You can experiment with the methods and pass different speeds as an argument to see how your DC motor behaves. Now, you can use this library in your projects to create DCMotor objects and use the library methods to control the motor: forward(), backwards() and stop().
Wrapping Up
In this tutorial, you learned how to control a DC Motor using the L298N motor driver with the Raspberry Pi Pico programmed using MicroPython. After understanding how the L298N motor driver works, controlling the DC motor is as easy as controlling digital outputs to control the direction of the motors and producing PWM signals to control their speed.
We hope you’ve found this tutorial useful. We have similar guides for other microcontroller boards:
- MicroPython: ESP32/ESP8266 Control a DC Motor with the L298N Driver
- Raspberry Pi Pico: Control a Servo Motor (MicroPython)
- Raspberry Pi Pico: Control a Stepper Motor (MicroPython)
- ESP32 with DC Motor and L298N Motor Driver – Control Speed and Direction (Arduino IDE)
Finally, if you would like to learn more about Raspberry Pi Pico, make sure you take a look at our resources:
Hai, what I am missing is the possebility of feedback and a realy speedcontrol (PID). Maybe a next item?
Is Motor ramp control possible??