top of page

Controlling an H-Bridge with Raspberry Pi: A Step-by-Step Guide


H-Bridges are fundamental components in electronics that allow for motor control, enabling direction change and offering speed variation through PWM (Pulse Width Modulation). When combined with the computational power of a Raspberry Pi, the possibilities for motor-driven projects expand significantly. This guide will walk you through setting up your Raspberry Pi to control an H-Bridge for motor applications.


  • Raspberry Pi (any model with GPIO pins)

  • An H-Bridge motor driver (like the L298N)

  • DC motor

  • Power supply for the motor (ensure compatibility with your motor's voltage)

  • Jumper wires

  • Breadboard (optional, for prototyping)


1. Wiring the Components

First, connect your Raspberry Pi to the H-Bridge. Here's a simple way to do it with an L298N H-Bridge:

  • Enable Pin: Connect to any GPIO pin (we'll use GPIO17 as an example).

  • Input Pins: These control the motor's direction. Connect Input1 to GPIO27 and Input2 to GPIO22.

  • Ground: Connect the GND of the Raspberry Pi to the GND of the L298N.

  • Motor Connections: Connect your DC motor to the output terminals on the H-Bridge.

  • Power: Connect your external power supply to the H-Bridge, ensuring it matches the motor's requirements.

Remember to double-check all connections to prevent any damage.

2. Setting Up Your Raspberry Pi

Ensure your Raspberry Pi is set up with the latest version of Raspberry Pi OS and has Python installed. You'll also need to enable GPIO control. This can be done by installing the RPi.GPIO library if it's not already installed:

sudo apt-get update
sudo apt-get install python3-rpi.gpio

The Python Script

Now, let's write a simple Python script to control the motor through the H-Bridge:

import RPi.GPIO as GPIO
import time

# Set the GPIO mode

# Define GPIO pins
EnablePin = 17
Input1 = 27
Input2 = 22

# Set up the GPIO pins
GPIO.setup(EnablePin, GPIO.OUT)
GPIO.setup(Input1, GPIO.OUT)
GPIO.setup(Input2, GPIO.OUT)

# Function to control the motor direction
def motor_control(direction):
    if direction == 'forward':
        GPIO.output(Input1, GPIO.HIGH)
        GPIO.output(Input2, GPIO.LOW)
    elif direction == 'reverse':
        GPIO.output(Input1, GPIO.LOW)
        GPIO.output(Input2, GPIO.HIGH)
        print("Invalid direction")

# Activate the motor
GPIO.output(EnablePin, GPIO.HIGH)
motor_control('forward')  # Change to 'reverse' to reverse the direction

# Run the motor for 5 seconds

# Stop the motor
GPIO.output(EnablePin, GPIO.LOW)

# Clean up the GPIO

This script initializes the Raspberry Pi's GPIO pins, defines a function to control the motor direction, and runs the motor in one direction for 5 seconds before stopping.


Controlling an H-Bridge with a Raspberry Pi opens up a world of possibilities for motor-driven projects. Whether you're building a robot, a custom vehicle, or any project that requires motor control, this setup provides a solid foundation. Experiment with the code to integrate motor control into your projects, and remember to always be cautious with electrical components.

Happy building!

29 views0 comments

Recent Posts

See All

Sending SMS with Raspberry Pi: A Step-by-Step Guide

Introduction: The Raspberry Pi's versatility extends beyond traditional computing tasks, making it a fantastic platform for various DIY projects. In this guide, we'll explore how you can harness the p


bottom of page