Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Powered Two-Wheel Car with L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino-Powered Two-Wheel Car with L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a two-wheel car using an L298N motor driver and an analog joystick. The joystick allows for intuitive control of the car's movement and direction. The L298N motor driver interfaces with two DC gear motors, enabling forward, backward, and turning movements based on the joystick's position.


Component List

L298N Motor Driver

5V DC Power Supply

Arduino UNO

DC Gear Motor (3-6V)

Analog Joystick


Wiring Details

L298N Motor Driver


5V DC Power Supply


Arduino UNO


DC Gear Motor (3-6V)


DC Gear Motor (3-6V)


Analog Joystick


Documented Code

/*
 * Arduino Sketch for controlling a two-wheel car using an L298N motor driver
 * and an analog joystick. The joystick controls the direction and movement
 * of the car. Pushing the joystick forward moves the car forward, pulling it
 * back moves it in reverse. Moving the joystick left or right turns the car
 * in the respective direction by stopping one wheel and moving the other.
 */

const int ENA = 5;  // PWM pin for left motor speed control
const int ENB = 6;  // PWM pin for right motor speed control
const int IN1 = 8;  // Direction control for left motor
const int IN2 = 9;  // Direction control for left motor
const int IN3 = 10; // Direction control for right motor
const int IN4 = 11; // Direction control for right motor

const int VERT = A0; // Joystick vertical axis
const int HORZ = A1; // Joystick horizontal axis

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  // Initialize motors to stop
  stopMotors();
}

void loop() {
  int vertValue = analogRead(VERT);
  int horzValue = analogRead(HORZ);

  if (vertValue > 600) {
    // Move forward
    moveForward();
  } else if (vertValue < 400) {
    // Move backward
    moveBackward();
  } else if (horzValue > 600) {
    // Turn right
    turnRight();
  } else if (horzValue < 400) {
    // Turn left
    turnLeft();
  } else {
    // Stop
    stopMotors();
  }
}

void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 255);
  analogWrite(ENB, 255);
}

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

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

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 255);
}

void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

This documentation provides a comprehensive overview of the circuit, detailing each component, its wiring, and the code used to control the system.