November 22, 2024

Arduino Motor Connections and Arduino Code for the Differential Drive Robot – Tutorial 3

Here, we explain the connections between Arduino, motor driver, and DC motors. We also provide the Arduino code for testing the motors and connections. Before reading this tutorial, you should read this tutorial that explains how to connect Arduino and motors to the L298N motor driver.

The differential drive robot is shown in the figure below.

The motor driver with general connections is shown below.

The connection diagram is shown below.

For a detailed explanation of this graph and the Arduino code, see the tutorial over here.

The Arduino code is given below.


int IN3 = 9;
int IN4 = 10;
int ENB = 11;

int ENA = 5;
int IN1 = 6;
int IN2 = 7;

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
 
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
 
  // Set all motors to OFF
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void loop() {
  // Set the speed of the motor (PWM signals) from 0 to 255
  analogWrite(ENA, 170);
  analogWrite(ENB, 170);

  digitalWrite(IN1,LOW);
  digitalWrite(IN2, LOW);
 
  // Set the direction and turn ON
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);

 
 
}