Hi all, I am trying to control two brushed dc motors speed independently using esp32 + tb6612fng

Hi all, I am trying to control two brushed dc motors speed independently using esp32 devkit V1 + tb6612fng. I created a mobile apps with which I am able to control the motors in forward, backward, left and right direction.  My problem is that left, or right are moving the car at 90(one wheel forward while the other reverse) when the buttons are pressed but I want to be able to slow down one motor and accelerate the other when I am making a turn while both motors are still in a forward direction. 
I am good at programming, and this is my first time using the forum.  
while hopping for a solution, any idea is so appreciated.
Below is the code that I modified using multiple codes that I collected online.
Thanks in advance for your help
Djedra



int stb = 15;
// Right Motor 
int R_fwd = 13; 
int R_bkw = 12; 
int R_enable1Pin = 2; 

// Setting PWM properties
const int R_freq = 30000;
const int R_Channel = 0;
const int R_resolution = 12;
int R_dutyCycle = 20;
const int R_MAX_DUTY_CYCLE = (int)(pow(2, R_resolution) - 1);// Max duty cycle

// Left Motor 
int L_fwd = 27; 
int L_bkw = 14; ; 
int L_enable2Pin = 4;

// Setting PWM properties
const int L_freq = 40000;
const int L_Channel = 1;
const int L_resolution = 13;
int L_dutyCycle = 20;
const int L_MAX_DUTY_CYCLE = (int)(pow(2, L_resolution) - 1);// Max duty cycle
//
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
BluetoothSerial ESP_BT; 
String String;
int incoming;
int sp;

int RSP = 0;
int LSP = 0;
int val = 40;
void setup() {
   ESP_BT.begin("ESP32_Control");


  pinMode(R_fwd, OUTPUT);  //initialize rightmotor forward as output
  pinMode(R_bkw, OUTPUT);  //...  
  pinMode(R_enable1Pin, OUTPUT); // enable pin for Right motor 
  pinMode(L_fwd, OUTPUT);  
  pinMode(L_bkw, OUTPUT);   
  pinMode(L_enable2Pin, OUTPUT); //Enable pin for Left Motor
//  pinMode(stb, INPUT);
  // configure LED PWM functionalitites
  ledcSetup(R_Channel, R_freq, R_resolution);
  ledcSetup(L_Channel, L_freq, L_resolution); 
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(R_enable1Pin, R_Channel);
  ledcAttachPin(L_enable2Pin, L_Channel);
  Serial.begin(115200);
  

  // testing
  Serial.print("Testing DC Motor...");
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {


 
  while(ESP_BT.available()){
    delay(50);
    char c=ESP_BT.read();
    String+=c;

  }
  //////////////////////////////////////////////////////////
  // -------------------- Receive Bluetooth signal ----------------------
//  if (ESP_BT.available()) 
//  {
//    incoming = ESP_BT.read(); //Read what we receive 
//
//    // separate button ID from button value -> button ID is 10, 20, 30, etc, value is 1 or 0
//    int button = floor(incoming / 10);
//    int value = incoming % 10;
  
  if(String.length()>0){
    Serial.println(String);
    if (String =="FORWARD"){      // MOVE FORWARD
       Serial.println("car moving forward");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 

    digitalWrite(R_fwd, HIGH);
    digitalWrite(R_bkw, LOW); 
    digitalWrite(L_fwd, HIGH);
    digitalWrite(L_bkw, LOW);
    ledcWrite(L_Channel, 80);
    ledcWrite(R_Channel, 80); 
    }
    if (String =="BACKWARD"){     // MOVE BACKWARD
      Serial.println("car moving backward");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 

    digitalWrite(R_fwd, LOW);
    digitalWrite(R_bkw, HIGH); 
    digitalWrite(L_fwd, LOW);
    digitalWrite(L_bkw, HIGH);
    ledcWrite(L_Channel, 100);
    ledcWrite(R_Channel, 100); 
    }
    if (String =="LEFT"){     // MOVE LEFT SIDE
      Serial.println("car moving left");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 

    digitalWrite(R_fwd, HIGH);
    digitalWrite(R_bkw, LOW); 
    digitalWrite(L_fwd, HIGH);
    digitalWrite(L_bkw, LOW);
    RSP = map(RSP, 0, 1023, 0, 255);
    ledcWrite(R_Channel, RSP+60);
    LSP = map(LSP, 0, 1023, 0, 255);
    ledcWrite(L_Channel, LSP-60);
//  while ( R_dutyCycle <= 220){
//    ledcWrite(R_Channel, R_dutyCycle);   
//    Serial.print("Left Forward with duty cycle: ");
//    Serial.println(R_dutyCycle);
//    R_dutyCycle = R_dutyCycle + 20;
//    delay(500);
//  }
//  R_dutyCycle = 200;
    

  }
    if (String =="RIGHT"){     // MOVE RIGHT SIDE
      Serial.println("car turning right");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 

    digitalWrite(R_fwd, HIGH);
    digitalWrite(R_bkw, LOW); 
//    ledcWrite(R_Channel, 15);
    digitalWrite(L_fwd, HIGH);
    digitalWrite(L_bkw, LOW);

    RSP = map(RSP, 0, 1023, 0, 255);
    ledcWrite(R_Channel, RSP - 60);
    LSP = map(LSP, 0, 1023, 0, 255);
    ledcWrite(L_Channel, RSP+60);
//        ledcWrite(R_Channel, 200);
//  while ( L_dutyCycle <= 220){
//    ledcWrite(L_Channel, L_dutyCycle);   
//    Serial.print("Left Forward with duty cycle: ");
//    Serial.println(L_dutyCycle);
//    L_dutyCycle = L_dutyCycle + 10;
//    delay(500);
//  }
//  L_dutyCycle = 200;
//    ledcWrite(L_Channel, 80);
    }

    if (String =="ROTATELEFT"){     
      Serial.println("car rotating left");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 
    
    digitalWrite(R_fwd, HIGH);
    digitalWrite(R_bkw, LOW); 
    digitalWrite(L_fwd, LOW);
    digitalWrite(L_bkw, HIGH);
    delay(1000);
    }
    if (String =="ROTATERIGHT"){     
      Serial.println("car rotating right");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW); 

    digitalWrite(R_bkw, HIGH); 
    digitalWrite(L_fwd, HIGH);
    digitalWrite(L_bkw, LOW);
    delay(1000);
    }
    if (String =="STOP"){     // STOP
      Serial.println("car stopped");
    ledcWrite(L_Channel, LOW);
    ledcWrite(R_Channel, LOW);       


    digitalWrite(R_fwd, HIGH);
    digitalWrite(R_bkw, HIGH); 
    digitalWrite(L_fwd, HIGH);
    digitalWrite(L_bkw, HIGH);
    }

    readString="";
  }
}

Welcome! You have us at a disadvantage. You know how it is hooked up, posting a Schematic not a frizzy picture would help. Links to technical information on each of the hardware devices also helps a lot. Even more important what should it do and what is it not doing. Which ESP are you using? there are several.

In order to control a motor speed, you need to know the speed. I do not see anything in your code where you actually seem to know the motor speed.

hey guys thanks for your reply.
in fact the initial project was to make a micromouse; after making adjustment to some code that I collected the car did not work as desire. so I decided to try to control it remotely and here I am.
For the speed, I thought that the pow(2, resolution)-1 automatically determine the range of speed in which the motor will be running. And I want to be able to direct the car with an angle less than 90 degree  I am using a 6v GA12-N20+encoder that I purchase from amazon; it has a speed of 60rpm, not sure if you are referring to this speed. otherwise I am not sure how to  express the speed and you can help me with that I would appreciate it so much. below is my schematic made through easy eda. 
thanks 
Djedra

but I want to be able to slow down one motor and accelerate the other when I am making a turn while both motors are still in a forward direction.

connect the enable PINS to PWM outputs and use it to both control the speed wen going forward and run the inside motors slower when turning

1 Like

Thanks, that nice schematic got you a fast answer from gcjr.

hello thanks for your advice; I am not using an Arduino but an ESP32 and if I paid good attention I believe any GPIO can be configure as PWM. I using GPIO 5 and 4 for PWMA and PWMB, respectively.
beside Robojax's library that uses l298 with esp32, which could not compile, I looked to find a library for TB6612 + esp32 but my search has ended with not much success.

You're looking for a library that has turning algorithms?

I am looking forward to control the speed of my two motors separately so that when I press the button for the car to turn slightly right (while both wheels still in the forward motion) the right wheel should move slower than the left. Currently the turn is made when one of the wheels is in reverse. When i set the pwm value of a wheel to 0, it does not move, which I believe is ok. but no matter what pwm value I set above 0, the speed of the wheel does not change, and the car just move straight; that is why I would like to be able to set the speed, for example, of 40 for one wheel and 100 for the other. At default if I could find a library that does that with esp32+tb6612, I'd be more than happy to use. please help me if you can sorry for the long text and thanks in advance

thanks

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