Differences in PWM Width with D1 Mini vs Nano & R3 for BTS7960 Motor Controller.

I'm struggling with this project to the point I ended up buying an oscilloscope. I'm far from being an electronics expert but am an old mechanical engineer. I'm working on using a 12V bilge pump as a water pump for a nutrient tank for a hydroponic system. 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 2 signals needed for the right direction and 2 for the left direction (that never gets used for a pump but the BTS7960 will not work when disconnected). I'm using the <BTS7960.h> library to create some sketches for testing with an Uno R3, Nano, and D1Mini. The problem I have with the D1 mini is the PWM signal isn't long enough for the motor to function at all. The sketch works great with the Uno and Nano. I do have to use different pins for the different controllers

Obviously, I'd prefer to use the Di for the controller as I also have 3 flow meters attached to the output that will be used to control the pump speed through Home Assistant.
For the Nano
Controller Pin6 R_PWM - analogWrite(Pin, Speed) Right Turn PWM input to controller (required)
Controller Pin7 R_EN - digitalWrite(GPIO, HIGH or LOW) output Digital H to enable, Low to disable (required)
Controller Pin9 L_PWM - analogWrite(Pin, Speed) Left Turn PWM input to controller (required)
Controller Pin8 L_EN - digitalWrite(GPIO, HIGH or LOW) output Digital H to enable, Low to disable (required)

For the D1 Mini
Controller D0 Pin 16 R_PWM - analogWrite(Pin, Speed) Right Turn PWM input to controller (required)
Controller D3 Pin 0 R_EN - digitalWrite(GPIO, HIGH or LOW) output Digital H to enable, Low to disable (required)
Controller D4 Pin 2 L_PWM - analogWrite(Pin, Speed) Left Turn PWM input to controller (required)
Controller D5 Pin 14 L_EN - digitalWrite(GPIO, HIGH or LOW) output Digital H to enable, Low to disable (required)

The sketch runs waiting for an input from the user to manually set the PWM speed between -255 and 0 for

#include <BTS7960.h>
// Sketch for D1Mini Working with new pins 03/26/21
/***************************************************
Copyright (c) 2019 Luis Llamas
(www.luisllamas.es)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
 ****************************************************/

// PINOUT
/*
WeMos  Arduino C Pin BTS7960
D0      16        R_PWM (1)
D1       5        SCL (Serial Clock)  
D2       4        SDA (Serial Data) 
D3       0        R_EN (3)
D4       2        L_PWM (2)
D5      14        L_EN (4)
D6      12    
D7      13    
D8      15    
TX       1   
RX       3   
*/
 

BTS7960 m1(16,2,0,14);

int speed = 0;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Be sure to enable 'No line ending' in Monitor window");
//  analogWriteFreq(36790); Didn't work
}

void loop() 
{

  Serial.println("Enter Pump Speed Negative for Left, Positive for Right");
  Serial.println("between -255 and 255. Anything else to stop");
  while (Serial.available() == 0)
  {
  }
  speed = Serial.parseInt();

  if (speed <0){
     Serial.print("Setting Motor Speed to left at ");
     Serial.println(speed);
     Serial.println();
  }
  else {
    Serial.print("Setting Motor Speed to right at ");
    Serial.println(speed);
    Serial.println();
  }

  if (speed >= -255 && speed <= 255){
     m1.enable();
     m1.setSpeed(speed);
  }
  else
  {
    Serial.println("Pump Stopped");
    m1.stop();
    m1.disable();
    delay(2000);
  }

}

In the image you can see the timings of the signals and that is what I'm struggling with. The Signal for the D1 mini is way too short and I'm asking for some help.

In the image you can see the timings of the signals and that is what I'm struggling with. The Signal for the D1 mini is way too short and I'm asking for some help.

The difference is that the D1 uses 10 bit PWM and the Uno, Nano 8 bit PWM.

A search for "wemos d1 pwm" turned up this page

ESP8266 uses 10-bit resolution for PWM generation PWM value varries from 0 to 1023. Arduino Uses 8-Bit Resolution i.e.PWM range is 0-254.

Why a complex controller for a pump? Is the pump a brushed DC motor? A logic level MOSFET might be a simpler replacement.

OK, so I looked at the library I was using and it also limited it to 255 so I updated the library I was using to allow values to 1023 and now when I compiled it and run the library I don't get any output. I'm bench testing with a sparkfun motor and a fan.

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);
    }
  }
}

The reason for the controller is I had a 12 V bilge pump laying around and I have 3 different channels for my hydroponic system. Not all 3 channels may be active at the same time so I will need to adjust the flow rate down when I'm only running one channel. I have a flow meter installed into each channel's supply line

Nevermind I had a typo. That worked. When I changed it in my sketch I never looked at the library I was using. When I correctly changed those values, it made all the difference in the world.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.