How to generate positive and negative pwm signals with nucleo-F746ZG when user gives frequency an run dc motor using bts7960b with it

I tried using

#include “mbed.h”
#include “FastPWM.h”
FastPWM fastpwm(PA_8);
int main() {
fastpwm.period(5.25e-8 );

But want to get frequency from user and run dc motor in that frequency using pwm singnals


what that mean? The PWM is switching between ON and OFF state (High/Low, VDD/ GND and so on). For what you need negative signal?

I suppose you use the popular module for arduino.

BTS7960                       Nucleo
- Pin1 Forward direction      any Nucleo Pwm pin
- Pin2 Reverse direction      another Nucleo Pwm pin with different channel
- Pin3 Forward enbable        a digital pin if you want to control if not then just Vcc
- Pin4 Reverse enable         a digital pin you want to control if not then just Vcc
- Pin5                        NC (just Forward current alarm )
- Pin6                        NC (just Reverse current alarm )
- Pin7                        Vcc
- Pin8                        Gnd

BTS7960 needs just two standart PWM signals. One for each direction of the motor (this is how H-bridge works). So how you can see you do not need any Negative signal…

BR, Jan

Hi @JohnnyK, Pwm signals so that user can select the frequency and by that pwm signals are generated and this signals send to BTS7960 and motor rotates.

First I asked for what you need Negative signal, so this reaction does not make sense. I asked becasue MCU does not generate negative signal and it is not possible without additional hardware. The BTS7960 also does not need it.
How I wrote above you need second PWM pin, that means also another one PWM object in your code. Then you just switch between both objects.

PWN1 = 0; PWM2 = X; //for one direction
PWN1 = X; PWM2 = 0; // for second direction
//The X represent your settings of motor speed

This is another non specific request. Via what will user select the frequency?

BR, Jan

@JohnnyK Range of frequency is 0-25khz

Ok, but how you will tell to the board this value? - Via a Pot and ADC, or directly value through Serial port or Ethernet?

BTW I found this library - IBT2 - Library for driving the IBT-2 H-bridge motor cont… | Mbed
Also with an example code - IBT 2 Example Code - | Mbed (just replace wait(0.1) with thred_sleep_for(100)) and it could work I think.

BR, Jan

@JohnnyK directly I give value through Serial port

Ok and how far are you? Do you achive something with the H-bridge?

For serial input you can try something like below (I found it in my archive from the past). The example read input from PC side and then set PWM for on board led.

#include "mbed.h"
#include "stdlib.h"

UnbufferedSerial pc(CONSOLE_TX, CONSOLE_RX);
PwmOut pwm(LED1);

int main()
    printf("Please enter a value 0-100%% in format like !50\n");

    int buffCounter = 0;
    size_t buff_size = 10;
    char buffer[buff_size];

    bool newComFlag = false;
    bool validFlag = false;

    while (true)
        //read data from serial
        if(pc.readable() && newComFlag == false){
            char c;
            if(c == '!') {
                // start of new command
                buffCounter = 0; 
                memset(buffer, 0, buff_size);
                validFlag = true;
            if(validFlag && c == '\n') { // string from PC side have to be ended with \n
                // command is complete set a flags
                newComFlag = true;
                validFlag = false;
            } else if (validFlag) {
                // store valid data
                if(c != '\r' && c != '!') buffer[buffCounter++] = c;
        // proced incoming complete data
            // convert char array to int value
            int userImput = atoi(buffer);
            printf("Value %d%%\n", userImput);
            // here set the value to PWM
            // ...........................
            newComFlag = false;

BR, Jan

@JohnnyK IBT2 library is working fine for my requirement. Thanks for your help!!!

You are welcome, good luck with your project.

BR, Jan

1 Like