Tutorial II: Motor With Encoder Control Demo 2

From Waveshare Wiki
Jump to: navigation, search

Modules Usage Tutorial

Motor With Encoder Control Demo 2

This tutorial is for controlling the forward and reverse, fast and slow rotation of a motor, the following provides demos for controlling the rotation of a motor.


Upload Demo

After downloading the motorCtrl.ino, use the USB cable to connect the multifunctional driver board and the computer (here inserted into the USB Type-C port of the multifunctional driver board), click on "Tools" → "Ports", and then click on the newly appeared COM port.
UGV1 doenload03EN.png
In Arduino IDE, click "Tools" → "Development Board" → "ESP32" → "ESP32 Dev Module", select the development board and the port and then upload the demo. After uploading the demo, connect the motor interface PH2.0 2P on the motor driver board to the motor without an encoder. Connect the XH2.54 power port to the power supply. Upon doing this, you will observe the motor rapidly rotating in the positive direction for 3 seconds, then slowly rotating in the opposite direction for 3 seconds, followed by a pause of 3 seconds, all in a continuous loop.

Demo Analysis

// The following defines the ESP32 pins used to control the TB6612
// Motor A
const uint16_t PWMA = 25;         // Motor A PWM control  Orange
const uint16_t AIN2 = 17;         // Motor A input 2      Brown
const uint16_t AIN1 = 21;         // Motor A input 1      Green

// Motor B
const uint16_t BIN1 = 22;        // Motor B input 1       Yellow
const uint16_t BIN2 = 23;        // Motor B input 2       Purple
const uint16_t PWMB = 26;        // Motor B PWM control   White

// PWM frequency of pins used for PWM outputs
int freq = 100000;

// Define PWM channel
int channel_A = 5;
int channel_B = 6;

// Define PWM accuracy, when it is 8, and PWM value is 0-255(2^8-1)
const uint16_t ANALOG_WRITE_BITS = 8;
// The maximum PWM value
const uint16_t MAX_PWM = pow(2, ANALOG_WRITE_BITS)-1;
// The minimum PWM value, due to the poor low-speed characteristics of DC motors, may not reach the motor's rotation threshold.
const uint16_t MIN_PWM = MAX_PWM/5;

void setup(){
  // Setting the operating mode of the ESP32 pin used to control the TB6612FNG
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  // Setting the channel, frequency, and accuracy of the ESP32 pin used for PWM outputs 
  ledcSetup(channel_A, freq, ANALOG_WRITE_BITS);
  ledcAttachPin(PWMA, channel_A);

  ledcSetup(channel_B, freq, ANALOG_WRITE_BITS);
  ledcAttachPin(PWMB, channel_B);

  // The pin used to control rotation should be set to a low logic level to stop the motor from rotating, thereby avoiding immediate rotation upon initialization
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, LOW);

// Motor A control  
void channel_A_Ctrl(float pwmInputA){
  // Round the pwmInput value to the nearest integer
  int pwmIntA = round(pwmInputA);
  if(pwmIntA == 0){
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, LOW);

  // Determine the direction of rotation by determining the positive or negative pwmInput value 
  if(pwmIntA > 0){
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, HIGH);
    // constrain() function is for limiting the pwmIntA value between MIN_PWM and MAX_PWM
    ledcWrite(channel_A, constrain(pwmIntA, MIN_PWM, MAX_PWM));
    digitalWrite(AIN1, HIGH);
    digitalWrite(AIN2, LOW);
    ledcWrite(channel_A,-constrain(pwmIntA, -MAX_PWM, -MIN_PWM));

// Motor B control  
void channel_B_Ctrl(float pwmInputB){
  int pwmIntB = round(pwmInputB);
  if(pwmIntB == 0){
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, LOW);

  if(pwmIntB > 0){
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, HIGH);
    ledcWrite(channel_B, constrain(pwmIntB, MIN_PWM, MAX_PWM));
    digitalWrite(BIN1, HIGH);
    digitalWrite(BIN2, LOW);
    ledcWrite(channel_B,-constrain(pwmIntB, -MAX_PWM, -MIN_PWM));

void loop(){
  // Motor stops for 3 seconds  

  // Motor reverses direction and turns at low speed for 

  // Motor positive direction and turns at high speed for 3 seconds