By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to wrap an L298N or DRV8833 channel in a reusable motor class with speed, brake and coast methods.
Our test scripts work, but they are cluttered with pins and truth tables. Scout will have two motors, so letβs wrap the details in a class. After this lesson, the rest of the course just says left.speed(60).
We want:
speed(value) where value is -100 (full reverse) to 100 (full forward)brake() to stop quickly and coast() to let goinvert option, so a motor wired backwards can be fixed in code rather than with a screwdriverdeadzone option, from lesson 6The maths is kept in a plain function, speed_to_duty, with no hardware in it. That makes it easy to test, even on a laptop.
Create a new file on the Pico called motor.py and paste this in. It contains one class for each driver, with identical methods, so Scout does not care which you built.
# motor.py - DC motor classes for the L298N and DRV8833
from machine import Pin, PWM
MAX_DUTY = 65535
def speed_to_duty(speed, deadzone=0):
"""Turn a speed of -100..100 into (direction, duty_u16).
direction is 1 (forward), -1 (reverse) or 0 (stopped).
deadzone is the duty percent at which your motor first turns.
"""
speed = max(-100, min(100, speed))
if speed == 0:
return 0, 0
direction = 1 if speed > 0 else -1
percent = deadzone + (100 - deadzone) * abs(speed) / 100
return direction, int(percent * MAX_DUTY / 100)
class Motor:
"""One L298N channel: two direction pins plus a PWM enable pin."""
def __init__(self, in1, in2, en, freq=1000, deadzone=0, invert=False):
self.in1 = Pin(in1, Pin.OUT)
self.in2 = Pin(in2, Pin.OUT)
self.en = PWM(Pin(en))
self.en.freq(freq)
self.deadzone = deadzone
self.invert = invert
self.coast()
def speed(self, value):
if self.invert:
value = -value
direction, duty = speed_to_duty(value, self.deadzone)
if direction == 0:
self.coast()
return
self.in1.value(1 if direction > 0 else 0)
self.in2.value(0 if direction > 0 else 1)
self.en.duty_u16(duty)
def brake(self):
self.in1.value(1)
self.in2.value(1)
self.en.duty_u16(MAX_DUTY)
def coast(self):
self.in1.value(0)
self.in2.value(0)
self.en.duty_u16(0)
class DRV8833Motor:
"""One DRV8833 channel: PWM on both inputs, no enable pin."""
def __init__(self, in1, in2, freq=10000, deadzone=0, invert=False):
self.in1 = PWM(Pin(in1))
self.in2 = PWM(Pin(in2))
self.in1.freq(freq)
self.in2.freq(freq)
self.deadzone = deadzone
self.invert = invert
self.coast()
def speed(self, value):
if self.invert:
value = -value
direction, duty = speed_to_duty(value, self.deadzone)
if direction > 0:
self.in1.duty_u16(duty)
self.in2.duty_u16(0)
elif direction < 0:
self.in1.duty_u16(0)
self.in2.duty_u16(duty)
else:
self.coast()
def brake(self):
self.in1.duty_u16(MAX_DUTY)
self.in2.duty_u16(MAX_DUTY)
def coast(self):
self.in1.duty_u16(0)
self.in2.duty_u16(0)
Here is what happens when you call left.speed(60):
flowchart TD
C["left.speed(60)"] --> I{"invert?"}
I --> F["speed_to_duty gives direction and duty"]
F --> L["Motor: set IN1 and IN2, PWM duty on EN"]
F --> R["DRV8833Motor: PWM duty on the active input"]
speed_to_duty is the maths from lesson 6, extended for negative speeds. It returns a direction and a duty so the class only has to set pins.brake() and coast() match the truth tables from lesson 4.A short script to check it. Copy it into main.py or run it from Thonny. It assumes the L298N wiring from lesson 5:
from time import sleep_ms
from motor import Motor
# Pins from the wiring table: IN1=GP0, IN2=GP1, ENA=GP2
left = Motor(0, 1, 2, deadzone=30)
try:
for speed in (25, 50, 75, 100, 0, -50, 0):
print("speed", speed)
left.speed(speed)
sleep_ms(1000)
left.brake()
finally:
left.coast()
Using a DRV8833 instead? Swap one line. Everything else stays the same:
from time import sleep_ms
from motor import DRV8833Motor
left = DRV8833Motor(0, 1, deadzone=20)
try:
left.speed(50)
sleep_ms(1000)
left.speed(-50)
sleep_ms(1000)
left.brake()
finally:
left.coast()
Why this matters: The rest of the course, and any robot you build afterwards, only ever calls speed(), brake() and coast(). If you change drivers, you change one line.
invert=True rather than swapping wires.stop() method that calls brake(), and a __repr__ that returns the last speed. This is a useful debugging aid.ImportError: no module named 'motor'.Why: MicroPython can only import files that are in the Picoβs own filesystem.
Quick quiz 1 of 3
What range of values does speed() take?
Quick quiz 2 of 3
A wheel spins the wrong way. What is the tidy fix?
Quick quiz 3 of 3
What is the key difference between the Motor and DRV8833Motor classes?
Comments