Translate

DIY Arduino Robot with L298N

 1. Introduction

Building a mobile robot is a rite of passage for any electronics hobbyist. This project demonstrates how to create a 4-Wheel Drive (4WD) robotic chassis controlled by an Arduino Uno and an L298N Motor Driver. By the end of this guide, you will have a robot capable of moving forward, backward, and performing precise turns using basic programmed logic.

2. Components

To build this project, you will need the following parts:

  • Arduino Uno R3 (The brain of the robot)

  • L298N Dual H-Bridge Motor Driver

  • 4x DC Gear Motors (with wheels)

  • 9V Battery (or a 2S LiPo battery for better performance)

  • Jumper Wires (Male-to-Male and Male-to-Female)

  • Robot Chassis kit

3. Circuit and Connections

Circuit Diagram

Connection L298N


The L298N driver controls two sets of motors (Left and Right). Follow these pin assignments:

L298N PinArduino PinDescription
ENAPin 9Speed control for Left Motors
IN1Pin 8Direction for Left Motors
IN2Pin 7Direction for Left Motors
IN3Pin 6Direction for Right Motors
IN4Pin 5Direction for Right Motors
ENBPin 3Speed control for Right Motors

Power Connections:

  • 9V Battery (+) to L298N 12V terminal.

  • 9V Battery (-) to L298N GND.

  • Arduino GND to L298N GND (Common Ground is essential!).

  • Arduino Vin to L298N 5V terminal (to power the Arduino from the driver).

4. Circuit Working

The L298N Motor Driver acts as a high-current amplifier. The Arduino sends low-power signals (TTL) to the driver, which then switches the high-current battery power to the motors.

  • PWM (Pulse Width Modulation): Using pins 9 and 3 (ENA/ENB), we can control the speed of the motors by varying the duty cycle via analogWrite().

  • H-Bridge Logic: By switching the IN pins between HIGH and LOW, we change the polarity of the voltage sent to the motors, allowing them to spin clockwise or counter-clockwise.

5. Code

C++
int motor1 = 8; // IN1
int motor2 = 7; // IN2
int motor3 = 6; // IN3
int motor4 = 5; // IN4
int ENA = 9;
int ENB = 3;

void setup() {
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  pinMode(motor4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  
  stop(); // Ensure robot is still on startup
  Serial.begin(9600);
}

void forward() {
  analogWrite(ENA, 250);
  analogWrite(ENB, 250);
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, HIGH);
  digitalWrite(motor4, LOW);
}

void backward() {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, HIGH);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, HIGH);
}

void left() {
  analogWrite(ENA, 150);
  analogWrite(ENB, 150);
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, HIGH);
  digitalWrite(motor3, HIGH);
  digitalWrite(motor4, LOW);
}

void right() {
  analogWrite(ENA, 150);
  analogWrite(ENB, 150);
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, HIGH);
}

void stop() {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, LOW);
  digitalWrite(motor3, LOW);
  digitalWrite(motor4, LOW);
}

void loop() {
  forward();  delay(2000);
  backward(); delay(5000);
  left();     delay(3000);
  right();    delay(4000);
  stop();     delay(7000);
}

6. Code Working

The code is structured into functional blocks:

  1. Definitions: We assign variable names to the digital pins for readability.

  2. Movement Functions: Each function (forward, left, etc.) sets the direction pins. For example, in forward(), motor1 is HIGH and motor2 is LOW.

  3. Speed Control: analogWrite on the Enable pins (ENA/ENB) sets the power level from 0 to 255.

  4. Main Loop: The loop() function executes a sequence: it drives forward for 2 seconds, reverses for 5, turns, and eventually stops.

7. Tips

  • Common Ground: Always connect the Ground (GND) of the Arduino to the Ground of the Motor Driver. Without this, the signals won't have a reference point and the motors won't move.

  • Power Supply: Standard 9V "rectangular" batteries drain very quickly with four motors. Use a rechargeable Li-ion battery pack for longer run times.

  • Wiring Check: If a motor spins the wrong way, simply swap the two wires of that specific motor where they enter the L298N terminal blocks.

8. Uses

  • Obstacle Avoidance: Add an Ultrasonic sensor to make the robot navigate autonomously.

  • Line Follower: Add IR sensors to follow a black track.

  • Remote Control: Interface a Bluetooth (HC-05) or Wi-Fi module to control the robot via a smartphone.

9. Conclusion

You have successfully built a foundational 4WD robotic platform! This setup is the "Swiss Army Knife" of robotics—it's versatile, easy to program, and can be expanded with almost any sensor imaginable. Happy building!

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.