Arduino MicroPython BTS7960 Motor Driver

A big 12V cooling fan is great on a hot day, but at full speed it is loud, and its motor needs far more current than any pin can give. Speed control fixes the noise, and the DIYables BTS7960 43A Motor Driver Module takes care of the current. The Arduino Giga R1 WiFi only sends small 3.3V signals to the module, and the module switches the heavy 12V current for the motor. So a short MicroPython script can choose a soft breeze, a full blast, or anything in between. On this page, a 12V DC motor plays the part of the fan, and Thonny shows each step of the test. You will see:

Arduino Giga R1 WiFi running MicroPython controls a 12V DC motor through a BTS7960 driver

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×BTS7960 Motor Driver Module
1×12V DC Motor
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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 and Pinout

Small driver boards such as the L298N or the DRV8833 are fine for toy motors. A large gear motor, a wiper motor, a winch or the motor of a big fan needs a much stronger driver, and the BTS7960 module is made for this job. You may also find the same board sold as "IBT-2" or "HW-039". It carries two Infineon BTS7960B chips. One chip makes one half-bridge; together they form a full H-bridge. In short: one brushed DC motor, in both directions, with its speed set by PWM.

How does it work? Each half-bridge holds one wire of the motor and can connect that wire to B+ or to B-. When one side goes to B+ and the other side stays on B-, current flows through the motor, and the motor turns. When the two sides swap, the current flows the other way, and so does the motor. PWM turns the active side on and off very fast. The longer the ON part of each pulse, the more power the motor gets, and the faster it spins.

BTS7960 motor driver module pinout - RPWM, LPWM, R_EN, L_EN, R_IS, L_IS, VCC, GND and the B+, B-, M+, M- screw terminals
Item DIYables BTS7960 43A Motor Driver Module
Power chips 2 x Infineon BTS7960B half-bridge (together one H-bridge)
Motor 1 brushed DC motor, forward and reverse
Motor supply on B+ and B- 5.5V to 27V DC (6V, 12V or 24V systems)
Current up to 43A (the current limit of the chip, a peak value)
PWM on RPWM and LPWM up to 25 kHz
Logic side 74HC14 buffer chip, powered by the VCC pin
Protection current limiting, over-temperature protection
Control header 2 x 4 male pins

Read the 43A with care. It is the level where the chip starts to limit the current, not a value for long use. For a motor that runs for minutes or hours, stay far below it. For long runs, what counts is how well the big heat sink under the board can shed heat.

The control header has two rows of four pins. The names are printed next to the pins, but the board writes R_IS and L_IS as "R_S" and "L_S":

Row Pin 7 and 8 Pin 5 and 6 Pin 3 and 4 Pin 1 and 2
Top row (odd pins) VCC R_IS R_EN RPWM
Bottom row (even pins) GND L_IS L_EN LPWM

What each pin does, and where it goes on the Giga:

  • RPWM: the speed input for the forward direction (right half-bridge). A PWM signal here turns the motor forward. Wire it to D9.
  • LPWM: the speed input for the reverse direction (left half-bridge). Wire it to D10.
  • R_EN: switches the right half-bridge on when it is HIGH. Wire it to D12.
  • L_EN: switches the left half-bridge on when it is HIGH. Wire it to D11.
  • R_IS and L_IS: current-sense and fault outputs. This tutorial does not use them, so leave them free.
  • VCC: feeds the small 74HC14 buffer chip and the power LED; motor current never flows through it. Wire it to the 5V pin of the Giga.
  • GND: the logic ground. Wire it to a GND pin of the Giga.
  • B+ and B- (screw terminal): the motor supply. B+ takes the + of the 12V adapter, and B- takes the -.
  • M+ and M- (screw terminal): the two wires of the motor.

Moving the Motor With Four Signals

With R_EN and L_EN both HIGH, the two PWM pins decide what happens. In MicroPython, you set the PWM level with duty_u16(), from 0 (always LOW) to 65535 (always HIGH):

RPWM LPWM Result
duty 1 to 65535 0 forward, faster with a bigger duty
0 duty 1 to 65535 reverse, faster with a bigger duty
0 0 brake, the driver joins the two motor wires and the motor stops fast

Now set R_EN and L_EN LOW. Both half-bridges switch off, the motor gets no power, and it coasts: it rolls to a stop on its own. Keep one simple rule in your code: at least one of RPWM and LPWM must be 0. Never give PWM to both of them at the same time.

Power and Ground

  • VCC goes to the 5V pin of the Giga. It only powers the small buffer chip, and the maker of the module asks for 5V there. The control pins are fine with 3.3V signals, so the Giga pins can drive them straight. These inputs have pull-down resistors, so no 5V flows back into the Giga.
  • On the module, GND and B- are the same point. So the single GND wire from the Giga also gives a common ground with the 12V adapter. So you do not need a second ground wire.
  • The 12V on B+ must never reach the Giga. The Giga gets its power from the USB cable, and the motor gets its power from the adapter.

Wiring Diagram

Six jumper wires join the Giga to the control header. The four signal wires go to the digital pins D9 to D12. The VCC wire goes to the 5V pin, and the GND wire can use any GND pin of the Giga. The 12V side uses only the screw terminals of the module.

The wiring diagram between Arduino Giga R1 WiFi MicroPython BTS7960 motor driver

This image is created using Fritzing. Click to enlarge image

BTS7960 Module Connect To
RPWM D9 of the Giga
LPWM D10 of the Giga
R_EN D12 of the Giga
L_EN D11 of the Giga
VCC 5V of the Giga
GND GND of the Giga
R_IS, L_IS nothing, leave them free
B+ + of the 12V adapter, through the DC power jack
B- - of the 12V adapter, through the DC power jack
M+, M- the two wires of the 12V DC motor

Look at the two enable wires: R_EN goes to D12, and L_EN goes to D11. The order is crossed, so check these two wires twice.

※ NOTE THAT:

  • Check the + and - of the DC power jack before you plug in the adapter. A reversed adapter can ruin the module.
  • Pick a 12V adapter that gives more current than your motor needs.
  • Plug in the adapter last, after every wire is in place.

Using a real fan instead of the test motor? Pick one with a plain brushed DC motor. Many computer fans have a small control chip inside, and they are made to turn in one direction only. Do not run the reverse step of the script on such a fan.

How To Program BTS7960 With MicroPython

No library is needed. Everything comes from the machine module.

  • Make RPWM and LPWM PWM outputs at 1 kHz, and set both of them to 0 first:
rpwm = PWM(Pin('D9')) lpwm = PWM(Pin('D10')) rpwm.freq(1000) lpwm.freq(1000) rpwm.duty_u16(0) lpwm.duty_u16(0)
  • Make the enable pins outputs, and switch the driver on. Do this only after both PWM pins are at 0:
r_en = Pin('D12', Pin.OUT) l_en = Pin('D11', Pin.OUT) r_en.value(1) l_en.value(1)
  • A speed in percent is easier to think about than a number up to 65535. One line converts it:
duty = int(percent * 65535 / 100)
  • Forward at 60%: set LPWM to 0 first, then put the PWM value on RPWM. For reverse, swap the roles of the two pins:
lpwm.duty_u16(0) rpwm.duty_u16(39321)
  • Stop the motor. The first two lines alone are a brake, because the enables stay HIGH. Add the last two lines, and it becomes a coast:
rpwm.duty_u16(0) lpwm.duty_u16(0) r_en.value(0) l_en.value(0)

1 kHz is far below the 25 kHz limit of the module. At low speed, the motor may hum or whine a little at this frequency. That is normal.

Arduino MicroPython Code - BTS7960 Motor Driver

The script puts the lines above into small helper functions: spin_forward(), spin_reverse(), brake() and coast(). Each one takes the speed as a percent from 0 to 100. One cycle takes about 10 seconds and has six steps, and each step prints one line:

  1. Speed up forward, from 0% to 100%.
  2. Stay at 100% for 2 seconds.
  3. Slow down to 0%.
  4. Brake for 1 second.
  5. Run in reverse at 50% for 2 seconds.
  6. Coast for 2 seconds with the driver off, then switch it on again.
""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-bts7960-motor-driver """ from machine import Pin, PWM import time # Arduino Giga R1 WiFi pins wired to the control header of the BTS7960 module RPWM_PIN = 'D9' # The Arduino Giga WiFi pin D9 connected to the RPWM pin (forward speed) of BTS7960 LPWM_PIN = 'D10' # The Arduino Giga WiFi pin D10 connected to the LPWM pin (reverse speed) of BTS7960 R_EN_PIN = 'D12' # The Arduino Giga WiFi pin D12 connected to the R_EN pin (right half-bridge on/off) of BTS7960 L_EN_PIN = 'D11' # The Arduino Giga WiFi pin D11 connected to the L_EN pin (left half-bridge on/off) of BTS7960 PWM_FREQ = 1000 # 1 kHz. The BTS7960 accepts PWM up to 25 kHz RAMP_STEP = 5 # Speed change per ramp step, in percent RAMP_DELAY_MS = 80 # Wait between two ramp steps, in milliseconds # Speed pins: one PWM output each. Set them to 0 before the driver is switched on 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) # Enable pins: plain digital outputs r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) def percent_to_duty(percent): # 0-100 % -> 0-65535, the range of duty_u16() return int(percent * 65535 / 100) def driver_enable(on): # True: R_EN and L_EN HIGH, both half-bridges on # False: R_EN and L_EN LOW, both half-bridges off r_en.value(1 if on else 0) l_en.value(1 if on else 0) def spin_forward(percent): lpwm.duty_u16(0) # Reverse input to 0 first: never PWM on both inputs rpwm.duty_u16(percent_to_duty(percent)) # PWM on RPWM = forward def spin_reverse(percent): rpwm.duty_u16(0) # Forward input to 0 first lpwm.duty_u16(percent_to_duty(percent)) # PWM on LPWM = reverse def brake(): # Both inputs 0 while the driver is on: the two motor wires are joined, so the motor stops fast rpwm.duty_u16(0) lpwm.duty_u16(0) def coast(): # Both inputs 0 and the driver off: the motor rolls to a stop by itself rpwm.duty_u16(0) lpwm.duty_u16(0) driver_enable(False) driver_enable(True) # Both PWM pins are already 0, so the driver can be switched on now print("BTS7960 ready (R_EN = L_EN = HIGH)") try: while True: print("1/6 forward : 0% -> 100%") for percent in range(0, 101, RAMP_STEP): spin_forward(percent) time.sleep_ms(RAMP_DELAY_MS) print("2/6 forward : hold 100% for 2 s") time.sleep(2) print("3/6 forward : 100% -> 0%") for percent in range(100, -1, -RAMP_STEP): spin_forward(percent) time.sleep_ms(RAMP_DELAY_MS) print("4/6 brake : RPWM = LPWM = 0 for 1 s") brake() time.sleep(1) print("5/6 reverse : LPWM at 50% for 2 s") spin_reverse(50) time.sleep(2) print("6/6 coast : R_EN = L_EN = LOW for 2 s") coast() time.sleep(2) driver_enable(True) # Switch the driver on again before the next forward run except KeyboardInterrupt: # The script was stopped (Stop button or Ctrl+C in Thonny): leave the motor off, not running coast() print("Stopped: driver off, motor free")

Detailed Instructions

To run the script on the Arduino Giga with Thonny IDE:

  • Have Thonny IDE ready on your computer.
  • Your Arduino Giga must already run the MicroPython firmware.
  • First time with MicroPython on this board? Read Getting Started with Arduino and MicroPython before you go on.
  • Wire everything as in the diagram, but leave the 12V adapter unplugged for now.
  • Connect the Giga to your computer with the USB Type-C cable.
  • In Thonny IDE, click Tools Options.
  • In the Interpreter tab, set MicroPython (generic).
  • Choose the COM port of the Giga (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Paste the script into Thonny's editor.
  • Save it on the board:
    • Press Ctrl+S or click the Save button.
    • Pick MicroPython device and type the name main.py.
  • Click the green Run button (or press F5). Lines appear in the Shell, but the motor stays still, because B+ has no power yet.
  • Only now push the 12V adapter plug into the DC power jack. Watch the motor, and read the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot BTS7960 ready (R_EN = L_EN = HIGH) 1/6 forward : 0% -> 100% 2/6 forward : hold 100% for 2 s 3/6 forward : 100% -> 0% 4/6 brake : RPWM = LPWM = 0 for 1 s 5/6 reverse : LPWM at 50% for 2 s 6/6 coast : R_EN = L_EN = LOW for 2 s 1/6 forward : 0% -> 100% 2/6 forward : hold 100% for 2 s 3/6 forward : 100% -> 0% 4/6 brake : RPWM = LPWM = 0 for 1 s 5/6 reverse : LPWM at 50% for 2 s Stopped: driver off, motor free
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Step 4 and step 6 both end with a still motor, but not in the same way. In step 4 (brake), the driver joins the two motor wires, and the motor stops almost at once. In step 6 (coast), the driver lets go, and the motor rolls on for a moment. With a fan blade on the shaft, the difference is easy to see.

Does the motor turn the wrong way in the "forward" steps? Swap the M+ and M- wires in the screw terminal. Nothing in the code needs to change.

The last line of the Shell above came from the red Stop button in Thonny. When you stop the script, its last part switches the driver off. Without that part, the PWM pins could keep their last value, and the motor would keep running.

※ NOTE THAT:

The file is named main.py, so the Giga starts it by itself at every power-up. This means the motor can start as soon as the Giga and the 12V adapter both have power. Keep your hands and loose cables away from the shaft or the fan blade before you plug in.

BTS7960 or a Smaller Driver?

For a small toy motor, this module is more than you need. A short comparison:

  • L298N: two motors, about 2A each. It loses about 2V inside, so the motor gets less than the supply voltage.
  • DRV8833: two small motors on a low voltage. Tiny and handy, but not for big loads.
  • BTS7960: only one motor, but a much higher current, and very little voltage is lost inside. Pick it for big fans, wiper motors, winches and heavy robots.

Does your fan only need ON and OFF, with no speed control? A relay is simpler. See Arduino MicroPython Controls Fan.

Two ideas to take the fan project further:

Video Tutorial

Learn More

※ 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!