Raspberry Pi Pico - DC Motor

In this guide, we will show you how to control the DC motor using the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico DC motor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×L298N Motor Driver Module
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DC Motor

DC Motor Pinout

A DC motor comes with two wires.

  • Positive wire: usually red
  • Negative wire: usually black
DC Motor Pinout

How It Works

When you buy a DC motor, it is important to know the voltage it needs. For example, if you have a DC motor that requires 12 volts, you should connect it to a 12-volt power source.

  • Connect 12V to the positive wire and GND to the negative wire to make the motor rotate clockwise at full speed.
  • Connect 12V to the negative wire and GND to the positive wire to make the motor rotate counterclockwise at full speed.

When you swap the connections of two wires on the DC motor as explained before, the way it spins will change. This method controls the spinning direction of the DC motor. Instead of doing this manually, it is done by programming.

When we provide less than 12V to DC motors, they still operate but not at their maximum speed. This indicates that the motor's speed varies with different voltages. Directly adjusting the voltage can be difficult in practice. Therefore, a more straightforward way to control the speed of a DC motor is used. We maintain a constant voltage from the power supply and manipulate the speed using a PWM (Pulse Width Modulation) signal. The motor spins faster with a higher PWM duty cycle and slower with a lower one.

How to control DC motor

How to control DC motor using Raspberry Pi Pico

To manage a DC motor using a Raspberry Pi Pico, you must handle both its speed and its movement direction. The Raspberry Pi Pico generates a PWM signal which is not strong enough to operate the motor on its own. Consequently, you need a hardware driver that links the Raspberry Pi Pico to the DC motor. This driver has two important functions:

  • Boost the power and voltage of the PWM signal from the Raspberry Pi Pico to control speed.
  • Get the control signal from the Raspberry Pi Pico to switch the power supply's polarity to control direction.
Raspberry Pi Pico control DC motor

※ NOTE THAT:

  • This guide works for all DC motors. Here, we're using a 12V DC motor to show you how.
  • If you use a 5V DC motor, you'll need a driver between the Raspberry Pi Pico and the motor, even though both use 5V. The Raspberry Pi Pico pin can't provide enough current to run the motor by itself.

This guide will show how to use the L298N driver to control DC motors.

Overview of L298N Driver

This guide will show you how to control a DC motor using the L298N Driver.

L298N Driver Pinout

L298N Driver Pinout

The L298N Driver can control two separate DC motors, called motor A and motor B, at the same time. It has 13 pins.

The common pins for both motors:

  • VCC pin: gives power to the motor. The voltage can be from 5 to 35V.
  • GND pin: this is a common ground pin and should connect to GND (0V).
  • 5V pin: powers the L298N module using 5V from a Raspberry Pi Pico.

Motor A pins (Channel A):

  • ENA pins: They manage how fast Motor A moves. To change the speed, take off the jumper and attach the pin to a PWM input.
  • IN1 & IN2 pins: They decide which way Motor A rotates. If one pin is HIGH and the other is LOW, Motor A will rotate. If both pins are HIGH or LOW, Motor A will not move.
  • OUT1 & OUT2 pins: These are linked to Motor A.

Motor B pins (Channel B):

  • ENB pins: These adjust the speed of Motor B. Remove the jumper and connect these pins to a PWM input to control Motor B's speed.
  • IN3 & IN4 pins: These determine the spinning direction of Motor B. Motor B spins when IN3 is HIGH and IN4 is LOW, or the other way around. If both are HIGH or both are LOW, Motor B will not spin.
  • OUT3 & OUT4 pins: These pins connect to Motor B.

The L298N driver we talked about before uses two kinds of input power:

  • One for the DC motor (VCC and GND pins): between 5 and 35V.
  • One for the L298N module operation (5V and GGD pins): between 5 and 7V.

The L298N driver includes three jumpers for advanced functions. For simplicity, remove all jumpers from the L298N driver.

You can control two DC motors simultaneously with a Raspberry Pi Pico and an L298N Driver. To manage each motor, you need to connect three pins from the Raspberry Pi Pico.

※ NOTE THAT:

This guide will teach you how to control a DC motor using channel A. You can control another DC motor in a similar method.

Wiring Diagram

Remove all three jumpers from the L298N module before starting the wiring.

The wiring diagram between Raspberry Pi and Pico DC Motor L298N Driver

This image is created using Fritzing. Click to enlarge image

How To Control the Speed of DC Motor via L298N Driver

You can change the speed of the DC motor by sending a PWM signal to the ENA pin on the L298N. Here is how you do it:

  • Connect a pin from the Raspberry Pi Pico to the ENA pin on the L298N module.
  • Use the ena.duty_u16() function to send a PWM signal to the ENA pin. The L298N driver will then amplify this PWM signal to control the DC motor.
ena.duty_u16(int(speed * 65535 / 255)) # Set motor speed on ENA_PIN, range 0-255

The speed can be set between 0 and 255. At 0, the motor will not move. At 255, the motor will move at its fastest speed.

How To Control the Direction of DC Motor via L298N Driver

You can change the rotation direction of a motor by adjusting IN1 and IN2 pins to HIGH or LOW. The table below explains how to control the direction for both channels.

IN1 pin IN2 pin Direction
LOW LOW Motor A stops
HIGH HIGH Motor A stops
HIGH LOW Motor A spins Clockwise
LOW HIGH Motor A spins Anti-Clockwise
  • Motor A turns to the right.
in1.value(1) in2.value(0)
  • Motor A rotates in the opposite direction to clockwise.
in1.value(0) in2.value(1)

※ NOTE THAT:

If you reverse the connections of the OUT1 and OUT2 pins with the DC motor pins, the motor will rotate the other way. To correct this, switch the OUT1 and OUT2 pins, or alter the control signals for the IN1 and IN2 pins in the program code.

How To Stop DC Motor Spinning

There are three ways to switch off a DC motor.

  • Sets the speed to zero.
ena.duty_u16(0) # Set motor speed to 0
  • Sets IN1 and IN2 pins to the LOW.
in1.value(0) in2.value(0)
  • Sets IN1 and IN2 pins to the HIGH.
in1.value(1) in2.value(1)

How to control a DC motor using L298N driver.

Raspberry Pi Pico Code

The code below performs the following actions:

  • Speed up the DC motor
  • Change the direction
  • Slow down the DC motor
  • Stop the motor
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-dc-motor """ from machine import Pin, PWM from time import sleep # Define pins ENA_PIN = 27 # The Raspberry Pi Pico pin GP27 connected to the ENA pin of L298N IN1_PIN = 29 # The Raspberry Pi Pico pin GP29 connected to the IN1 pin of L298N IN2_PIN = 19 # The Raspberry Pi Pico pin GP19 connected to the IN2 pin of L298N # Initialize pins ena = PWM(Pin(ENA_PIN)) # PWM for speed control ena.freq(1000) # Set frequency to 1kHz for PWM in1 = Pin(IN1_PIN, Pin.OUT) in2 = Pin(IN2_PIN, Pin.OUT) def set_motor_speed(speed): ena.duty_u16(int(speed * 65535 / 255)) # Convert 0-255 speed to 0-65535 for PWM def motor_forward(): in1.value(1) in2.value(0) def motor_backward(): in1.value(0) in2.value(1) def motor_stop(): in1.value(0) in2.value(0) while True: motor_forward() for speed in range(256): set_motor_speed(speed) sleep(0.01) sleep(1) motor_backward() sleep(1) for speed in range(255, -1, -1): set_motor_speed(speed) sleep(0.01) motor_stop() sleep(1)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Take off all three jumpers from the L298N module.
  • Connect the Raspberry Pi Pico to the DC motor via L298N module according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Observations:
    • The DC motor speeds up, then rotates at maximum speed for 1 second.
    • The direction of the DC motor changes.
    • The DC motor rotates at maximum speed for 1 second in the reverse direction.
    • The DC motor slows down.
    • The DC motor pauses for 1 second.
    • This cycle keeps repeating.

    ※ NOTE THAT:

    In this guide, we will learn how to change the speed of a DC motor compared to its fastest speed. To manage the exact speed in rotations per second, we need to use a PID controller and an encoder. We will discuss how to control the exact speed of the DC motor in another guide.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!