Looking for help to write a ESPHOME component for a BTS7960 motor controller

I’m working on using a 12V bilge pump as a water pump for a nutrient tank for a hydroponic system I’m putting together. The wiring component is quite simple once you know that in order to turn the pump in one direction, the other direction is also required otherwise the controller does not work (10 hours of life gone) There are 4 signals used for one direction ® and 4 for the other (L) direction (but a pump doesn’t run well backwards). but one only needs 4 in total to get the motor to work.

I used the Arduino library BTS7960 by Christoph Eickhoff to create some sketches for testing with an Uno R3, Nano, and D1Mini. The D1 mini doesn’t work with the BTS7960 library. I even broke down and bought an oscilloscope to find out why. Turned out the library capped the PWM values at 8 bit resolution from 0-255 whereas the D1mini uses 0-1023 and that was the reason PWM values in my sketch would not get the pump to even run.

Obviously, I’d prefer to use the esp for the controller as I also have 3 flow meters attached to the output that will be used to control the pump speed through HA. The pulse sensor is already available with ESPHome but the motor controller is not and I would like some help to create one.

This is what I ended up with for the D1 Mini to function with the BTS7960:

Function Pin Description
R_PWM D6 (GPIO 12) analogWrite(Pin, Speed) Right Turn PWM input to controller (required)
R_EN D3 (GPIO 0) digitalWrite(GPIO, HIGH or LOW) H to enable, L to disable (required)
R_IS Not Used Right turn current warning (Not Used)
L_PWM D4 (GPIO2) analogWrite(Pin, Speed) Left Turn PWM input to controller (required)
L_EN D5 (GPIO14) digitalWrite(GPIO, HIGH or LOW) H to enable, L to disable able (required)
L_IS Not Used Right turn current warning (optional at this time)

Controller Pin 7 Ground Not Used
Controller Pin 8 VCC (5v) Not Used

If the enable on R is enabled, the the left must be Disabled and speed set to 0. Reverse is true for opposite direction. In my case I don’t need a reverse direction so I didn’t wire it up. Did I say I wasted 10 hours to figure this out??

Arduino Sketch
Arduino Sketch

Modified Library BTS7960.cpp

void BTS7960::setSpeed(int16_t pwm){
  if (pwm < -1023 || pwm > 1023)
  {
    // out of range stop the motor
    stop();
  }else{
    if (pwm < 0 ){
     analogWrite(_LPWM,0);
     analogWrite(_RPWM,-pwm);
    }else{
     analogWrite(_RPWM,0);
     analogWrite(_LPWM,pwm);
    }
  }
}

Having a slider that runs from -100% duty cycle to +100% dudty cycle for the PWM would be ideal. Eventually for this project, I would like to use the flow rates to dynamically adjust the PWM to maintain a constant flow through the system. I have the pieces, I just need help putting them together. It would make life so much easier for using a motor controller.

Did you still need help with us?
I did something similar but I just used lambdas, delays, and outputs for controlling a waste pump, linear accutators, and a door lock. The advantage there is that you can actually disable the output once you get to where you’re going, so in case you don’t get to where you’re going you don’t burn your motor or controller.
I do the same thing for servos but it’s built into that library.

Ended up seeing this as I’m now doing it again for a projector screen…

You can think of these chips as just the MOSFET version of the L298 if that helps. You don’t lose 1.2 volts per channel like the L298 just due to the internal architecture, and so you can often use these (pair of) chips with no heat sink whatsoever for low loads. (< 5A).

I am very interested in how you solved this? Me and a friend are working on using the BTS7960 together with an ESP8266 and based on the documentation we thought that this was working straight out of the box? Just curious. We haven’t connected everything up yet and are interested in knowing if we should go another route with another hbridge.