Motor Control

ICT 361: Introduction to Robotics

Mr. Seng Theara

DC Motor

How to make the DC Motor rotate

Power Supply

DC Motor

How to make the DC Motor rotate

Power Supply

How to make the DC Motor rotate

Voltage 12V

Voltage 6V

How to make the DC Motor rotate

Forward Direction

Backward Direction

  • Red wire from power Supply to red wire of the dc motor
  • Black wire from power supply to black wire of the dc motor
  • Red wire from power Supply to black wire of the dc motor
  • Black wire from power supply to red wire of the dc motor

Why we can't use ESP32 to control DC Motor Directly?

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Why we can't use ESP32 to control DC Motor Directly

DC Motor

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA
  • DC Motor usually need 6V, 9V, 12V (more)
  • Needs hundreds of mA to several A

Why we can't use ESP32 to control DC Motor Directly

DC Motor

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA
  • DC Motor usually need 6V, 9V, 12V (more)
  • Needs hundreds of mA to several A

Result

  • ESP32 may reset, overheat, or get permanently damaged
  • Motor won’t spin or spin slowly

Why we can't use ESP32 to control DC Motor Directly

DC Motor

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA
  • DC Motor usually need 6V, 9V, 12V (more)
  • Needs hundreds of mA to several A

Result

ESP32 may reset, overheat, or get permanently damaged

Motor won’t spin or spin slowly

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

L298N

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

L298N

L293D

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

L298N

L293D

BTS7960

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

L298N

L293D

BTS7960

These drivers:

  • Supply high current

  • Protect ESP32

  • Allow speed & direction control

Correct Way to Control a DC Motor with ESP32

ESP32

  • Output Voltage is only 3.3V
  • Max Current from the pin ~12mA

Motor Driver

TB6612

L298N

L293D

BTS7960

These drivers:

  • Supply high current

  • Protect ESP32

  • Allow speed & direction control

Speed and Direction Control of Dc Motor

Driver Motor which is used in this class

ESP32 with Driver Motor

Speed and Direction Control of Dc Motor

Speed and Direction Control of Dc Motor

         Driver       State
A01       HIGH
A02       LOW
PWMA 8 bit (0-255)
B01       HIGH
B02       LOW
PWMB 8 bit (0-255)
    Motor
    
    

Forward

= \frac{12V \times \text{pwmA}}{255}
= \frac{12V \times \text{pwmB}}{255}

Forward

       State
      LOW
      HIGH
8 bit (0-255)
      LOW
      HIGH
8 bit (0-255)
    Motor
    
    

Backward

= \frac{12V \times \text{pwmA}}{255}
= \frac{12V \times \text{pwmB}}{255}

Backward

Pins Which are used with with ESP32 

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
  Motor2     IO14
    BIN1     IO32
    BIN2     IO27
  Motor3     IO5
    AIN3     IO21
    AIN4     IO18
  Motor4     IO19
    BIN3     IO23
    BIN4     IO22

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 8);
}

void loop() {
  analogWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 0);
}

void loop() {
  lcdcWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

First, We need to declare pin 25 and 26 as output because it is used to control direction of the motor

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 0);
}

void loop() {
  lcdcWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

First, We need to declare pin 25 and 26 as output because it is used to control direction of the motor

We set Channel 0 for Motor 1, and Frequency of 20Khz, with 8 bit resolution (0-255)

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 0);
}

void loop() {
  lcdcWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

First, We need to declare pin 25 and 26 as output because it is used to control direction of the motor

We set Channel 0 for Motor 1, and Frequency of 20Khz, with 8 bit resolution (0-255)

We declare pin 33 as pwm1 because we use this pin to control the speed of the motor

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 0);
}

void loop() {
  lcdcWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

First, We need to declare pin 25 and 26 as output because it is used to control direction of the motor

We set Channel 0 for Motor 1, and Frequency of 20Khz, with 8 bit resolution (0-255)

We declare pin 33 as pwm1 because we use this pin to control the speed of the motor

analogWrite to adjust the speed between 0-255

The pin 25 and 26 are used for switching the direction of the motor

Coding For control the speed and Direction of Motor1

  Motor1     IO33
    AIN1     IO26
    AIN2     IO25
void setup() {
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  ledcSetup(0, 20000, 8);
  ledcAttachPin(33, 0);
}

void loop() {
  ledcWrite(33, 70);
  digitalWrite(25, HIGH);
  digitalWrite(26, LOW);
}

First, We need to declare pin 25 and 26 as output because it is used to control direction of the motor

We set Channel 0 for Motor 1, and Frequency of 20Khz, with 8 bit resolution (0-255)

We declare pin 33 as pwm1 because we use this pin to control the speed of the motor

analogWrite to adjust the speed between 0-255

The pin 25 and 26 are used for switching the direction of the motor

Practice

Write code to control all the 4 motors 

Practice

Write code to control all the 4 motors