DC motor Control using Arduino USART serial communication

In this tutorial you will learn how to control two DC motors speed and direction using Arduino USART serial communication. The DC motors are connected to car wheels whose direction and speed is controlled via USART interface from PC to Arduino USART. The motor is controlled using L293D motor driver and the L293D is controlled using Arduino Nano. 

By entering various commands like f, r, a, d or s on the PC serial COM port terminal you can move the wheels forward, reverse(backward), accelerate, deaccelerate or stop respectively.  You can use any PC compactiable COM port terminal like Tera Term and others but here we will use the Arduino IDE serial monitor to send commands to the Arduino. The program code is provided below.

The motors are simple DC toy motors that can run from voltages ranging from above 5V. But here we suggest to use 9V battery with enough current producing capacity. In order to rotate the two motor you will need high enough voltage and above all high current.

In our previous tutorial DC Motor Control using ATmega32 and L293D we have shown how to control the speed using Potentiometer which when rotated generated varying PWM signal that in turn controlled the speed of the motor. In that tutorial we have already told you what L293D IC is and its pinout. L293D is basically a motor driver IC with operating voltage from 4.5V to 36V and output current of 600mA to peak output current of 1.2A peak. It is a 16 pin IC and has inside it 4 op-amps which can be used to connect two DC motors or one stepper motor. It has two enable pins which are used to enable each two op-amps. There are four pins for ground and two pins for two voltage input. One voltage input is the TTL logic +5V and the other voltage input pin is for motor. The voltage for motor pin used here is 9V.


Interfacing DC motors with L293D and Arduino Nano


The schematic wiring diagram of interfacing two DC with L293D motor driver and Arduino Nano is shown below.

schematic of Arduino Nano L293D Two DC motors

In the wiring diagram above, we have used the digital pin 9 and 8 to connect to the L293D inputs in1 and in2 for the first DC motor. We have connected the digital pin 7 to enable pin en1 of the L293D. Similarly, we have used the pin digital pin 6, 5 and 4 to connect to the enable pin en2, inputs in3 and in4 for the 2nd DC motor.

Programming Arduino Nano for DC motor control


The program code for controlling the direction and speed of the two dc motors is provided below.

int enA = 7;
int in1 = 9;
int in2 = 8;
int enB = 6;
int in3 = 5;
int in4 = 4;

void setup () {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  // Initial state Turn off motors 
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
  //Serial
  Serial.begin(9600);
}

void loop() {
if(Serial.available()){
   char msg = Serial.read();
   
   switch(msg){
      case 'f':
   forward();
   break;
      case 'r':
   reverse();
   break;
      case 's':
   stop();
   break;
      case 'a':
   accelerate();
   break;
      default:
   break;
   }
   }
}

void forward() {
  analogWrite(enA, 255);
  analogWrite(enB, 255);

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
}

void reverse(){ 
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void accelerate(){
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(2);
  }
}

void deaccelerate(){
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(2);
  }
}

void stop(){
  analogWrite(enA, 0);
  analogWrite(enB, 0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

First we have declared alias name for the digital pins connected to the L293D pins. In the setup() function we have made all the pins going from the Arduino Nano to the L293D pins as output. Also in the same function we have written a logic LOW signal to disable the motors. Then in the same function we have setup the serial communication interface with 9600 baud rate using the Serial.begin() function.

In the loop() function we have used the Serial.available() function to check if any byte is received or not via the USART port.  Then using the Serial.read() function we have read any incoming character from the USART and stored it in char variable msg. Using the switch/case statements we check whether the received character matches with any of our command character 'f', 'r', 'a', 'd' or 's' and take action according to the byte received. For example, if the received character is 'f', then the case with 'f' is executed. We have made five functions- forward(), reverse(), accelerate(), deaccelerate() and stop() which will move the two dc motor wheels either in forward or reverse direction or slowly increase or decrease speed or stop the rotation of the two DC motors. 

The forward() and reverse() function will move the DC motors in forward and backward direction. For forward direction, we send HIGH and LOW to to pins in1 and in2 repectively and for backward direction we revert the polarity of the signal by writing LOW and HIGH to pins in1 and in2. For speed control, we write PWM signal to the enable pins enA and enB using a for loop. PWM signal can be generated using the analogWrite() function. 


Video Demonstration of Two DC motor control using Arduino USART 




See related tutorials:


Post a Comment

Previous Post Next Post