Give full play to the role of the driver module . First of all, we need to understand PWM This concept .
PWM
   Pulse width modulation (PWM) Basic principles : The control mode is to control the switching device of inverter circuit , A series of pulses with equal amplitude are obtained at the output , Use these pulses instead of sine waves or required waveforms . That is to say, multiple pulses are generated in the half cycle of the output waveform , The equivalent voltage of each pulse is sinusoidal , The obtained output is smooth and has less low order harmonics . The width of each pulse is modulated according to certain rules , The output voltage of inverter circuit can be changed , The output frequency can also be changed . 
 
 tool / raw material 
 * 
 Driver module ,arduino modular , DuPont line , Bus bar , Car 
 * 
ArduinoIDE, wait 
 method / step 
 * 
 Drive module wiring 
   In the previous tutorial, I have said that if you want to control the output of the driver , Need to drive “ENA”“ENB” Control , Therefore, we need to pull out the two jumper caps of the selected part in the figure . And will “ENA” connect Arduino 
UNO Development board “5” Pin ,“ENB” connect “6” Pin . 
 
 * 
Arduino The code test is as follows :
 
int leftCounter=0,  rightCounter=0;
unsigned long time = 0, old_time = 0; //  Time stamp 
unsigned long time1 = 0; //  Time stamp 
float lv,rv;// Left , Right wheel speed 
 
#define STOP        0
#define FORWARD     1
#define BACKWARD    2
#define TURNLEFT    3
#define TURNRIGHT   4
#define CHANGESPEED 5
 
int leftMotor1 = 16;
int leftMotor2 = 17;
int rightMotor1 = 18;
int rightMotor2 = 19;
bool speedLevel=0;
 
int leftPWM = 5;
int rightPWM = 6;
 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  attachInterrupt(0,RightCount_CallBack, FALLING);
  attachInterrupt(1,LeftCount_CallBack, FALLING);
 
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
  pinMode(leftPWM, OUTPUT);
  pinMode(rightPWM, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  SpeedDetection();
 
  if(Serial.available()>0)
  {
    char cmd = Serial.read();
 
    Serial.print(cmd);
    motorRun(cmd);
    if(speedLevel)  // Output different speeds according to different gears 
    {
      analogWrite(leftPWM, 120);
      analogWrite(rightPWM, 120);
    }
    else
    {
      analogWrite(leftPWM, 250);
      analogWrite(rightPWM, 250);
    }
  }  
}
/*
 * * Speed calculation 
 */
bool SpeedDetection()
{
  time = millis();// In Milliseconds  , Calculate current time  
  if(abs(time - old_time) >= 1000) //  If the time is up 1 second 
  {  
    detachInterrupt(0); //  Turn off external interrupt 0
    detachInterrupt(1); //  Turn off external interrupt 1
    // Count the number of pulses per second from the encoder , Convert to current speed value 
    // What is the unit of speed per minute , Namely r/min. The encoder disk is 20 A void .
    Serial.print("left:");
    lv =(float)leftCounter*60/20;// Trolley wheel motor speed 
    rv =(float)rightCounter*60/20;// Trolley wheel motor speed 
    Serial.print("left:");
    Serial.print(lv);// Upload the current high speed of the left wheel motor to the upper computer , Low byte 
    Serial.print("     right:");
    Serial.println(rv);// Upload the current high speed of the left wheel motor to the upper computer , Low byte 
    // Return to the initial state of encoder speed measurement 
    leftCounter = 0;   // Clear the pulse count to zero , In order to calculate the next second pulse count 
    rightCounter = 0;
    old_time=  millis();     //  Record the time node of speed measurement per second    
    attachInterrupt(0, RightCount_CallBack,FALLING); //  Reopen external interrupt 0
    attachInterrupt(1, LeftCount_CallBack,FALLING); //  Reopen external interrupt 0
    return 1;
  }
  else
    return 0;
}
/*
 * * Interrupt service function of right wheel encoder 
 */
void RightCount_CallBack()
{
  rightCounter++;
}
/*
 * * Interrupt service function of left wheel encoder 
 */
void LeftCount_CallBack()
{
  leftCounter++;
}
/*
 * * Car motion control function 
 */
void motorRun(int cmd)
{
  switch(cmd){
    case FORWARD:
      Serial.println("FORWARD"); // Output status 
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      break;
     case BACKWARD:
      Serial.println("BACKWARD"); // Output status 
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
      break;
     case TURNLEFT:
      Serial.println("TURN  LEFT"); // Output status 
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
      break;
     case TURNRIGHT:
      Serial.println("TURN  RIGHT"); // Output status 
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      break;
     case CHANGESPEED:
      Serial.println("CHANGE SPEED"); // Output status 
      if(speedLevel)  // Switch gears when receiving shift command 
        speedLevel=0;
      else
        speedLevel=1;
      break;
     default:
      Serial.println("STOP"); // Output status 
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, LOW);
  }
}
 
 * 
 For the sake of your understanding , Here is a special explanation :
      In the main function void loop() Add in PWM Output function ,analogWrite(pin, 
value) Function “pin” Represents the pin used ,“value” Represents output PWM The size of the value , The scope is 0~255.
if(speedLevel)  // Output different speeds according to different gears 
    {      
analogWrite(leftPWM, 120);      analogWrite(rightPWM, 120);  
  }  
  else 
   {   
   analogWrite(leftPWM, 250);      analogWrite(rightPWM, 250); 
    }
 
Technology