Raspberry Pi Pico - BTS7960 Motor Driver

Every power window in a car is moved by a small 12 volt DC motor with a gearbox, the window lifter motor. Take one out of an old car door, and you have a strong little motor for a sliding hatch, a coop door or a lifting platform. The Raspberry Pi Pico can tell it when to go up and when to go down. It cannot power it, though: a GP pin only sends a weak 3.3 volt signal, while the motor pulls many amps from a 12 volt supply. The BTS7960 motor driver sits between the two.

In this guide, we will learn to set up and use the BTS7960 43A motor driver with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico and BTS7960 motor driver turning a 12V DC motor both ways in MicroPython

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×BTS7960 Motor Driver Module
1×12V DC Motor
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of BTS7960 Motor Driver

The DIYables BTS7960 module is a board with a big heat sink under it. Many shops sell the very same board as "IBT-2" or "HW-039". Two Infineon BTS7960B chips sit on top of the board, each of them a half-bridge. Together they form one full H-bridge. That is why a single module runs exactly one brushed DC motor, both ways, with PWM for the speed. A small 74HC14 buffer chip sits between the control header and the two power chips. It takes the signals from the Pico and hands them on.

DIYables BTS7960 Module
Also Sold As IBT-2, HW-039
Power Chips 2 x Infineon BTS7960B half-bridge (one full H-bridge)
Motor Count one brushed DC motor, both directions
Voltage for the Motor (B+, B-) 5.5 volts to 27 volts DC, so 6, 12 or 24 volt motors
Peak Current 43 A, where the chips start to limit the current
Highest PWM Frequency 25 kHz (RPWM, LPWM)
Logic Supply (VCC) for the 74HC14 buffer chip and power LED only
Built-in Protection current limit, over-temperature

Read the 43 A as a ceiling, not as a goal. If your motor runs for minutes at a time, keep well under that number. For long runs, the heat sink sets the real limit: the better it sheds heat, the more current the board can keep up.

How does it work? The motor sits between the two chips. Each chip has two choices for its motor wire: 12 volts from B+, or ground from B-. When the right chip picks 12 volts and the left chip picks ground, current enters the motor from the right side. Let the chips trade their choices, and the current enters from the left side instead, so the shaft turns backward. PWM switches the 12 volts on and off very fast (1000 times per second in this guide). The longer the "on" part of each cycle (the duty cycle), the faster the motor turns.

Pinout

BTS7960 motor driver module pinout

The thin control wires go to a 2 x 4 pin header, and the thick power wires go to a 4-way screw terminal. On the header, each "R" pin has its "L" partner right below it:

  • Top row: VCC, R_IS, R_EN, RPWM.
  • Bottom row: GND, L_IS, L_EN, LPWM.

What each pin does in this guide:

  • RPWM pin: speed signal for the forward direction (we call it "up"). Connect to GP16 of the Pico.
  • LPWM pin: speed signal for the reverse direction ("down"). Connect to GP17.
  • R_EN pin: switches the right half-bridge on at HIGH (3.3 volts) and off at LOW (0 volts). Connect to GP19.
  • L_EN pin: does the same for the left half-bridge. Connect to GP18.
  • R_IS and L_IS pins: these report the motor current and faults. The silkscreen calls them "R_S" and "L_S". This guide does not use them, so no wire goes there.
  • VCC pin: connect to VBUS of the Pico (5 volts when the Pico runs on USB). It only powers the logic side: the 74HC14 chip and the power LED. The motor never takes power from it.
  • GND pin: connect to a ground pin (0 volts) of the Pico. Inside the module, it is tied to B-.
  • B+ and B- terminals: the 12 volt supply goes here, plus to B+ and minus to B-.
  • M+ and M- terminals: one motor wire in each.

Why 5 volts on VCC? VCC feeds only the small 74HC14 chip, and the maker of the module wants 5 volts there. The control inputs still accept 3.3 volt signals, so the GP pins can drive them as they are. Each input has a pull-down resistor, and R_IS and L_IS stay unconnected, so no 5 volt signal comes back from the module into the Pico.

How the Control Pins Move the Motor

RPWM (GP16) LPWM (GP17) R_EN and L_EN What the motor does
PWM 0 HIGH turns forward (up). A higher duty cycle gives more speed.
0 PWM HIGH turns in reverse (down). A higher duty cycle gives more speed.
0 0 HIGH brake. Both motor wires get tied together inside the driver, and the shaft halts quickly.
0 0 LOW coast. The driver lets go of the motor completely, and it spins down slowly on its own.

One rule matters more than any other: RPWM and LPWM must never carry PWM at the same time. At least one of them stays at 0. The script keeps this rule by setting the other pin to 0 first, every time it changes direction.

Power and Ground

  • The motor is fed by the 12 volt adapter alone. The adapter's plug goes into the DC power jack, whose + screw is wired to B+ and whose - screw is wired to B-.
  • Your adapter should be able to supply more current than the motor draws.
  • B+ must never touch any pin of the Pico: not VSYS, not VBUS, not a GP pin.
  • A single ground wire does the job. Inside the module, GND is tied to B-, so the wire from a Pico ground pin to the module's GND puts the Pico, the module and the adapter on one shared ground.
  • Swapped plus and minus can ruin the module. Check the + and - screws of the jack twice before the adapter goes in.

Is the BTS7960 more than your motor needs? A DRV8833 board can run two small motors at about 1.5 A each, up to 10.8 volts. The BTS7960 runs just one motor, with far more current and up to 27 volts. For small motors, the Raspberry Pi Pico - DRV8833 DC Motor Driver and Raspberry Pi Pico - DC Motor tutorials, which both use the DRV8833, are a better fit.

Wiring Diagram

The Pico is plugged into the breadboard. In the drawing, its USB connector points to the right, so pins 21 to 40 run along the lower edge. Each jumper wire starts in the bottom row of holes, just below its Pico pin, and ends on the header of the BTS7960. The module itself stays off the breadboard: it has its own pin header, so the wires go straight to it. Four of the six wires leave close together, at the end far from the USB connector: GP16 (pin 21), GP17 (pin 22), GP18 (pin 24) and GP19 (pin 25). The GND wire comes from pin 38 on the same edge, near the USB end. The red 5 volt wire comes from VBUS, pin 40, the very last pin at the USB end.

The wiring diagram between Raspberry Pi and Pico BTS7960 motor driver

This image is created using Fritzing. Click to enlarge image

BTS7960 Module Raspberry Pi Pico / Other Part
RPWM GP16 (pin 21)
LPWM GP17 (pin 22)
R_EN GP19 (pin 25)
L_EN GP18 (pin 24)
VCC VBUS (pin 40)
GND GND (pin 38)
R_IS, L_IS not wired
B+ the plus (+) screw of the DC power jack (12 volt adapter)
B- the minus (-) screw of the DC power jack
M+, M- motor wire 1 and motor wire 2

※ NOTE THAT:

Keep the 12 volt side and the Pico side apart. The adapter connects only to B+ and B-, and the Pico gets its own power from the USB cable. Plug in the adapter last, when every jumper wire is already in place.

Raspberry Pi Pico Code - BTS7960 Motor Driver

This script is a bench test for the window motor. GP16 and GP17 become PWM outputs at 1 kHz, and GP18 and GP19 become plain on/off outputs. Speeds are written in percent. up(percent) and down(percent) turn 0 to 100 % into the 0 to 65535 range of duty_u16(), and each of them sets the opposite PWM pin to 0 before it starts. brake() and coast() are the two ways to stop from the table above, and enable() switches R_EN and L_EN together. One round of the loop goes like this:

  1. UP: the speed climbs from 0 % to 100 % in steps of 4 %.
  2. UP: full speed for 2 seconds.
  3. UP: the speed drops back to 0 %.
  4. BRAKE: 1 second.
  5. DOWN: half speed (50 %) for 2 seconds.
  6. COAST: the driver is off for 2 seconds, then on again for the next round.

The try / finally part turns the driver off when you click Stop in Thonny, so the motor does not keep running by itself.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-bts7960-motor-driver """ # Raspberry Pi Pico + BTS7960 (IBT-2) 43A motor driver: one 12V DC motor # UP = forward: PWM on RPWM (right half-bridge), LPWM = 0 # DOWN = reverse: PWM on LPWM (left half-bridge), RPWM = 0 # R_EN and L_EN HIGH = driver on, LOW = driver off (the motor coasts) from machine import Pin, PWM import time RPWM_PIN = 16 # The Raspberry Pi Pico pin GP16 connected to the RPWM pin of the BTS7960 LPWM_PIN = 17 # The Raspberry Pi Pico pin GP17 connected to the LPWM pin of the BTS7960 R_EN_PIN = 19 # The Raspberry Pi Pico pin GP19 connected to the R_EN pin of the BTS7960 L_EN_PIN = 18 # The Raspberry Pi Pico pin GP18 connected to the L_EN pin of the BTS7960 PWM_FREQ = 1000 # 1 kHz PWM (RPWM and LPWM accept up to 25 kHz) # PWM outputs for the two directions, both silent (duty 0) at the start rpwm = PWM(Pin(RPWM_PIN)) lpwm = PWM(Pin(LPWM_PIN)) rpwm.freq(PWM_FREQ) lpwm.freq(PWM_FREQ) rpwm.duty_u16(0) lpwm.duty_u16(0) # The enable pins are plain on/off outputs r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) def enable(state): # 1 = both half-bridges on, 0 = both off r_en.value(state) l_en.value(state) def up(percent): # Forward. LPWM is cleared first, so the two PWM pins are never on together lpwm.duty_u16(0) rpwm.duty_u16(percent * 65535 // 100) # 0..100 % -> 0..65535 def down(percent): # Reverse. RPWM is cleared first rpwm.duty_u16(0) lpwm.duty_u16(percent * 65535 // 100) def brake(): # RPWM = LPWM = 0 with the driver on: the motor wires are joined, quick stop rpwm.duty_u16(0) lpwm.duty_u16(0) def coast(): # PWM pins 0 and driver off: the motor gets no power and rolls to a stop rpwm.duty_u16(0) lpwm.duty_u16(0) enable(0) def ramp_forward(first, last, step): # Change the forward speed a little at a time for percent in range(first, last, step): up(percent) time.sleep_ms(60) enable(1) print("Driver enabled, test cycle starts") try: while True: print("UP | RPWM 0% -> 100%") ramp_forward(0, 101, 4) print("UP | RPWM 100%, hold 2 s") time.sleep(2) print("UP | RPWM 100% -> 0%") ramp_forward(100, -1, -4) print("BRAKE | RPWM 0%, LPWM 0%, 1 s") brake() time.sleep(1) print("DOWN | LPWM 50%, 2 s") down(50) time.sleep(2) print("COAST | R_EN = 0, L_EN = 0, 2 s") coast() time.sleep(2) enable(1) # driver on again before the next UP finally: coast() # Stop in Thonny (or any error): leave the driver off print("Script stopped, driver off")

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • Never set up a Pico before? The Raspberry Pi Pico - Getting Started tutorial explains both steps.
  • Wire the Pico, the BTS7960, the motor and the DC power jack as the diagram shows. Keep the 12 volt adapter out of the socket for now.
  • Clamp or tape the motor down on the table. A geared window motor has a lot of force when it starts.
  • Test on the bench, not inside a car door. This test runs up for longer than it runs down, so in a door the glass would soon reach the top and the motor would stall.
  • Connect the Raspberry Pi Pico to your computer with the micro USB cable.
  • Open Thonny IDE and go to Tools Options.
  • In the Interpreter tab, pick MicroPython (Raspberry Pi Pico) and the port of your Pico (for example COM4 on Windows or /dev/ttyACM0 on Linux).
  • Paste the code above into the Thonny editor.
  • Store it on the Pico:
    • Click the Save button, or press Ctrl+S.
    • In the dialog, pick Raspberry Pi Pico, not This computer.
    • Name the file main.py.
  • Click the green Run button (or press F5). The Shell starts to print, but the motor stays still, because it has no power yet.
  • Now plug in the 12 volt adapter.
  • Watch the motor, and follow the lines in the Thonny Shell:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Driver enabled, test cycle starts UP | RPWM 0% -> 100% UP | RPWM 100%, hold 2 s UP | RPWM 100% -> 0% BRAKE | RPWM 0%, LPWM 0%, 1 s DOWN | LPWM 50%, 2 s COAST | R_EN = 0, L_EN = 0, 2 s UP | RPWM 0% -> 100% UP | RPWM 100%, hold 2 s UP | RPWM 100% -> 0% BRAKE | RPWM 0%, LPWM 0%, 1 s
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Compare each line with the motor. Over the three UP lines, the motor speeds up smoothly, runs flat out for 2 seconds, and slows down again. On BRAKE, it stops almost at once. On DOWN, it turns the other way at half speed. On COAST, it spins on for a moment and slows down by itself, which is a much softer stop than the brake. At low duty values, the motor may hum or whine. That sound comes from the 1 kHz PWM, and it is normal.

Does the motor turn the wrong way on UP? Swap the M+ and M- wires on the screw terminal. The code stays as it is.

When you click the red Stop button, the Shell prints "Script stopped, driver off" and the motor stops.

Because the file is stored as main.py on the Pico, it starts on its own every time the Pico is powered on or reset, even with no computer attached (for example, on a USB phone charger).

Ready to put the motor into a real window or hatch? Then the motor must stop at the two ends of its travel. A switch at each end is a simple way to do that, see Raspberry Pi Pico - Limit Switch.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!