KevsRobots Learning Platform
16% Percent Complete
By Kevin McAleer, 3 Minutes
Page last updated May 10, 2025

DC motors are the core of many robotics projects β they provide movement for wheels, arms, and even propellers. In this lesson, youβll learn how to control them using your Raspberry Pi Pico and a motor driver.
To follow along, gather the following components:
| Item | Description | Quantity |
|---|---|---|
| Raspberry Pi Pico | Raspberry Pi Pico / Pico 2 or Pico W / Pico 2 W Microcontroller | 1 |
| Motor Driver | L298N motor driver module | 1 |
| Motors | Small DC motors, either the yellow TT or N20 style | 2 |
| Power | External power supply (e.g. 4xAA batteries or 2S Li-ion) | 1 |
| Wires | Breadboard and jumper wires | As needed |
The Raspberry Pi Pico canβt drive a motor directly β it doesnβt output enough current. Instead, we use an L298N H-bridge motor driver, which allows the Pico to control the motorβs speed and direction using:
Hereβs how to connect a single motor to the L298N and Pico:
β οΈ Important: Do not connect motor power (VCC) directly to the Pico!
Hereβs a simple MicroPython script to control motor direction and speed:
from machine import Pin, PWM
from time import sleep
# Motor control pins
in1 = Pin(0, Pin.OUT)
in2 = Pin(1, Pin.OUT)
ena = PWM(Pin(2))
ena.freq(1000)
def motor_forward(speed=65025):
in1.high()
in2.low()
ena.duty_u16(speed)
def motor_backward(speed=65025):
in1.low()
in2.high()
ena.duty_u16(speed)
def motor_stop():
in1.low()
in2.low()
ena.duty_u16(0)
# Test
motor_forward()
sleep(2)
motor_backward()
sleep(2)
motor_stop()
You can adjust the speed by changing the speed parameter (range: 0β65535).
Now that you can control a motor, youβre one step closer to making your robot move!
Next up: Using H-Bridge Motor Drivers
You can use the arrows β β on your keyboard to navigate between lessons.
Comments