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

An H-bridge is a circuit that lets you control the direction of a DC motor by switching the current flow. It’s essential for robotics because it allows you to drive motors forward and backward with just a few control signals.
The H-bridge gets its name from the circuit diagram, which looks like an “H” made of transistors or switches.
It allows current to flow in either direction through the motor:
When paired with PWM on the enable pin, you can also control the speed of the motor.
The L298N has two H-bridges inside — perfect for controlling two motors independently. Here’s a typical wiring setup:
GP pins are the GPIO pins on the Pico.
Don’t forget: Pico GND must connect to L298N GND.
Here’s a MicroPython script for controlling both motors:
from machine import Pin, PWM
from time import sleep
# Motor A
in1 = Pin(0, Pin.OUT)
in2 = Pin(1, Pin.OUT)
ena = PWM(Pin(2))
ena.freq(1000)
# Motor B
in3 = Pin(3, Pin.OUT)
in4 = Pin(4, Pin.OUT)
enb = PWM(Pin(5))
enb.freq(1000)
def motor_a_forward(speed=50000):
in1.high()
in2.low()
ena.duty_u16(speed)
def motor_a_backward(speed=50000):
in1.low()
in2.high()
ena.duty_u16(speed)
def motor_b_forward(speed=50000):
in3.high()
in4.low()
enb.duty_u16(speed)
def motor_b_backward(speed=50000):
in3.low()
in4.high()
enb.duty_u16(speed)
def stop_motors():
in1.low()
in2.low()
in3.low()
in4.low()
ena.duty_u16(0)
enb.duty_u16(0)
# Test
motor_a_forward()
motor_b_forward()
sleep(2)
motor_a_backward()
motor_b_backward()
sleep(2)
stop_motors()
With two motors under your control, you can:
Drive forward/backward
Turn left/right
Rotate on the spot (tank steering)
This forms the basic movement logic for most two-wheeled robots.
Try setting different speeds for left and right motors.
Create a function to “spin in place.”
Add buttons to manually control each motor.
You’re now ready to add some intelligence to your robot!
Next up: Line Following with IR Sensors
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments