DEV Community

Hedy
Hedy

Posted on

How to build a robot car using an L298N motor driver, DC motors, and Arduino?

Let’s build a robot car using an L298N motor driver, DC motors, and Arduino! Below is a complete step-by-step guide covering wiring, code, and troubleshooting.

1. Components Needed

  • Arduino Uno/Nano (or any Arduino-compatible board)
  • L298N Motor Driver Module (Dual H-Bridge)
  • 2x DC Motors (6V–12V, with wheels)
  • Chassis Kit (includes frame, wheels, screws)
  • Battery Holder (6V–12V, e.g., 4xAA or 2x18650)
  • Jumper Wires (Male-to-Male, Male-to-Female)
  • Power Switch (optional)

2. Wiring Diagram
L298N Connections

Key Notes:

  • Use PWM pins (~) for speed control (ENA, ENB).
  • Power motors separately from Arduino (L298N’s 12V input).
  • Connect Arduino GND to L298N’s GND for signal reference.

3. Arduino Code (Basic Movement)

cpp
// Pins for L298N
const int ENA = 9;   // Left motor speed (PWM)
const int IN1 = 8;   // Left motor direction
const int IN2 = 7;
const int ENB = 10;  // Right motor speed (PWM)
const int IN3 = 6;   // Right motor direction
const int IN4 = 5;

void setup() {
  // Set all pins as outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  // Move forward at 50% speed
  moveForward(128);
  delay(2000);

  // Turn right
  turnRight(200);
  delay(1000);

  // Move backward at full speed
  moveBackward(255);
  delay(2000);

  // Turn left
  turnLeft(200);
  delay(1000);
}

// Custom functions for movement
void moveForward(int speed) {
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void moveBackward(int speed) {
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void turnRight(int speed) {
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void turnLeft(int speed) {
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}
Enter fullscreen mode Exit fullscreen mode

4. How It Works
1. Direction Control:

  • IN1=HIGH, IN2=LOW → Left motor forward.
  • IN1=LOW, IN2=HIGH → Left motor backward.
  • Same logic for right motor (IN3, IN4).

2. Speed Control:

analogWrite(ENA, speed) sets PWM duty cycle (0–255).

3. Turning:

  • Right turn: Left motor forward, right motor backward.
  • Left turn: Right motor forward, left motor backward.

5. Troubleshooting

6. Upgrades & Next Steps
A. Add Wireless Control (Bluetooth/Wi-Fi)

  • HC-05 Bluetooth Module: Pair with smartphone (App: Arduino Bluetooth Controller).
  • ESP8266/ESP32: Use Wi-Fi for remote control (e.g., Blynk app).

B. Add Sensors
Ultrasonic Sensor (HC-SR04): Avoid obstacles.

cpp
// Example: Stop if obstacle < 20cm
if (distance < 20) {
  moveBackward(255);
  delay(500);
  turnRight(200);
  delay(500);
}
Enter fullscreen mode Exit fullscreen mode

IR Remote: Control with a TV remote.

C. Use a Motor Shield
Arduino Motor Shield: Simplifies wiring (plugs directly into Arduino).

7. Final Tips

  • Power Supply: Use a 7.4V LiPo battery for better performance.
  • Weight Distribution: Balance the chassis to avoid motor strain.
  • Code Optimization: Use functions for repetitive movements.

Now you have a fully functional Arduino robot car!

Top comments (0)