# Controlling 24V dc Motors with BTS7960

Hi everyone, this is the first time I post something in the forum, I am working on controlling two 24V dc motors using 2 BTS7960 drivers for a rc car that I am doing, I am using PWM to control the speed of the motors nothing special, the problem is that when I start the motors you can hear a noise that annoys after a while, I found that this is cause because of the frequency used on the timers of the arduino, I am using pins 3 and 11 (timer 2) to control de PWM signal, I increase the frequency of the timer trying diferrent prescalers and the sound was gone, but now the PWM values dont work properly, the motor start spinning when a put a value of 120 and it is already suplying the 24Volts when the PWM value is 240, also the motors do not move at the same speed.

Both drivers are conected exactly the same way, the voltage suplied is exactly the same at both drivers but the voltage they suplied to the motors are different making that one motor runs faster than the other, my theory is that I am using a higher frequency that the Driver can, according to the datasheet of the BTS7960 it has a PWM capability up to 25kHz, but to be honest I haven't work with timers that much and I don´t know how to set timer to to 20kHz or 25kHz if some one could help me with this or thinks there is another poroblem I could really use your help, i have read other forums about increasing frequency on timer like this one:http://www.gammon.com.au/forum/?id=11504 but to be hones I don't have clear how to do it, I attach the code and a diagram for the conecction, thank you for reading

``````// MOTOR 1
int motor1Pin1 = 7;
int motor1Pin2 = 8;

int PWMmotor1= 3; // PWM del motor 1

// MOTOR 2
int motor2Pin1 = 9;
int motor2Pin2 = 10;

int PWMmotor2= 11; // PWM del motor 2

void setup(){
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(PWMmotor1, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(PWMmotor2, OUTPUT);
}

void loop(){

TCCR2B = TCCR2B & B11111000 | B00000001;     // set timer2 divisor to     1 for PWM frequency of 31372.55 Hz (Recomendado)

//TCCR2B = TCCR2B & B11111000 | B00000010;     // set timer2 divisor to     8 for PWM frequency of  3921.16 Hz

//TCCR2B = TCCR2B & B11111000 | B00000011;     // set timer2 divisor to    32 for PWM frequency of   980.39 Hz

digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);

analogWrite(PWMmotor1,velocidad);              // escribir el PWM en escala de 0 a 255 para la velocidad del motor1
analogWrite(PWMmotor2,velocidad);              // escribir el PWM en escala de 0 a 255 para la velocidad del motor2
}
``````

Please read topics like "How to..." in the beginning. Look for the way to attache code using code tags. Helpers using tablets or phones can't open. ino files, and help You.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom...

The speed of DC motors cannot be controlled easily. You need RPM feedback and a (PID...) regulator for each motor. Half the efforts if you only use 1 motor.

Thank you for the information and for being comprensive I modified the post with the a code, I only include the part in how I controlled the speed which is the one that is giving me problems, there is an image of me connection, thank you for the suport

Depending on the motor, it's inductive reactance may be too much for higher frequencies, did you try 3921Hz?

Wouldn't the timer setting code do fine in setup?

Wouldn't the timer setting code do fine in setup?

Missed that, yes, thats where it should be.

Hi,
Is there a reason you are not just using the UNO PWM facility from some of the controller pins?

analogWrite(PinNumber, 0 to 255)

The frequency is lower than what you are using I think?
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Have you swapped the motors over to check that it is not a motor problem?
Do they both run at the same speed when connected directly ot the 24V supply?

Tom...

JCA34F:
Depending on the motor, it's inductive reactance may be too much for higher frequencies, did you try 3921Hz?

I tried diferent frequencies included 3921Hz but it does not work either

You are right I would change it to the setup,

I am using some motors from China, they are 24V, 250W, here is mor information about the their specs https://www.ebikethaikit.com/uploads/443/files/MY1016Z250W.pdf

For the 24V supplied I am using two 12V lead-acid batteries of 12Amph in series and to supplied the arduino and the logic part of the driver I am using a regulator to get 5V from the batteries, the motors are not the problem because I switched them and the one the was moving faster now start moving slower and vice versa, I am using a variable for the speed to control de PMW values because later on that value will be given by a joystick maping the values from the joystick to PWM values, here is a simple sketch of the conection I have and a picture of the motors I am using

Hi,
Can I suggest that as you are using SLA batteries that do not have current limit, you please put a fuse in series with the positive lead.
At the moment if you have a short circuit in your project, the batteries are capable of basically blowing up components and or PCB tracks/wires.

Have you swapped the driver PCBs over to see if the problem swaps?
Have you double checked the gnd connections between UNO and motor controllers?

Can you please hand draw a full diagram with power supply and motors, then post an image.
Picture type images of layouts do not easily convey your circuit.

Thanks.. Tom...

Thank you for the sugestion you are right I need to protect the PCB in case a shortciruit, looks like I found a solution I would still be testing it but turns out that I needed a lower frecuency than the default frequency of the arduino uno, aparently I found the sweet spot on 122.5 Hz, the respond of the motors with that value is better, both motor spin at the same speed and most important that annoying sound dissapear, I will still going to be testing the respond but it seems tha the problem is solve, thank you to you Tom and everyone that respond, you really help me to clear my ideas

``````//TCCR2B = TCCR2B & B11111000 | B00000001;     // set timer2 divisor to     1 for PWM frequency of 31372.55 Hz (Recomendado)

//TCCR2B = TCCR2B & B11111000 | B00000010;     // set timer2 divisor to     8 for PWM frequency of  3921.16 Hz

//TCCR2B = TCCR2B & B11111000 | B00000011;     // set timer2 divisor to    32 for PWM frequency of   980.39 Hz

//TCCR2B = TCCR2B & B11111000 | B00000100;        // set timer2 divisor to    64 for PWM frequency of   490.20 Hz  (Lo trae por DEFAULT)

//TCCR2B = TCCR2B & B11111000 | B00000101;     // set timer2 divisor to   128 for PWM frequency of   245.10 Hz

TCCR2B = TCCR2B & B11111000 | B00000110;     // set timer2 divisor to   256 for PWM frequency of   122.55 Hz

//TCCR2B = TCCR2B & B11111000 | B00000111;     // set timer2 divisor to  1024 for PWM frequency of    30.64 Hz
``````

here is how I have connected the motors if you still have some suggestions I am all ears, thanks

Hi,
Good stuff, it looks like the impedance of the motors is a critical factor in their performance.

Tom...