Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 9 of 47 · Motion · Beginner

DC motor with an H-bridge

Drive a small DC motor forward and backward through a motor driver board. The same wiring idea runs robot cars.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for DC motor with an H-bridge: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Motor driverL298N motor driver
FWDPush button
BACKPush button

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredMotor driver - ENAPin 32 (GPIO12)
2ColouredMotor driver - IN1Pin 29 (GPIO5)
3ColouredMotor driver - IN2Pin 31 (GPIO6)
4RedMotor driver - 5VPin 2 (5V)
5BlackMotor driver - GNDPin 6 (GND)
6ColouredFWD - SignalPin 13 (GPIO27)
7BlackFWD - GNDPin 9 (GND)
8ColouredBACK - SignalPin 15 (GPIO22)
9BlackBACK - GNDPin 14 (GND)

Python code (gpiozero)

from gpiozero import Motor, Button
from signal import pause

motor = Motor(forward=5, backward=6, enable=12)
fwd = Button(27)
back = Button(22)

fwd.when_pressed = motor.forward      # run forward while held
fwd.when_released = motor.stop
back.when_pressed = motor.backward
back.when_released = motor.stop

print("Hold FWD or BACK to drive the motor")
pause()

How the code works

You will learn

  • Why a Pi pin cannot drive a motor directly
  • H-bridge: two inputs choose the direction
  • Speed with PWM

Try this

  • Change motor.forward to a function that runs at half speed.
  • Make the motor ramp up smoothly to full speed.
  • Add a stop button that works even if another button is held.

On a real Pi

  • Never connect a motor to a GPIO pin. Use a driver (L298N, L293D, TB6612) with its own supply.
  • Put a flyback diode on bare relay or motor coils. Most driver boards already have them.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4Tactile push button (12 mm, with cap)2Robu.in Amazon.in ThinkRobotics
5L298N motor driver module1Robu.in Amazon.in ThinkRobotics
6TT gear motor with wheel, 6 V1Robu.in Amazon.in ThinkRobotics
74 x AA battery holder (6 V) or 9-12 V adapter
Motor supply, separate from the Pi
1Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 8. Servo motor sweepAll projects10. Temperature alarm with an analog sensor →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.