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

Arduino UNO PS2 Controlled Battery-Powered Robotic Car with L298N Motor Drivers

View and Edit Project In Cirkit DesignerImage of Arduino UNO PS2 Controlled Battery-Powered Robotic Car with L298N Motor Drivers

Circuit Documentation

Summary

This circuit is designed to control a four-wheel robot using an Arduino UNO microcontroller and a PS2 controller. The robot's movement is managed by two L298N DC motor drivers, each controlling two motors with reducers. The power is supplied by a 18650 Li-Ion battery. The PS2 controller adapter board is used to interface the PS2 controller with the Arduino UNO.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. L298N DC Motor Driver (2 units)

    • Description: Dual H-Bridge motor driver.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  3. Motor with Reducer (4 units)

    • Description: DC motor with a gear reducer.
    • Pins: 3 - 6 VCC, GND
  4. PS2 Controller Adapter Board

    • Description: Adapter board for interfacing a PS2 controller.
    • Pins: 5v, data, CMD, CLK, ATT, GND
  5. 18650 Li-Ion Battery

    • Description: Rechargeable lithium-ion battery.
    • Pins: Positive, Negative

Wiring Details

Arduino UNO

L298N DC Motor Driver (1st Unit)

L298N DC Motor Driver (2nd Unit)

Motor with Reducer (1st Unit)

Motor with Reducer (2nd Unit)

Motor with Reducer (3rd Unit)

Motor with Reducer (4th Unit)

PS2 Controller Adapter Board

18650 Li-Ion Battery

Code Documentation

#include <PS2X_lib.h>

PS2X ps2x;

int error = 0;
byte type = 0;
byte vibrate = 0;
bool joystickEnabled = false;
int speed = 255;

const int ledPin = 13;

// Motor Driver 1 (Front Wheels) pins
const int motor1pin1 = 2;  // Front-left IN1 red-OUT1
const int motor1pin2 = 3;  // Front-left IN2 black-OUT2
const int motor2pin1 = 4;  // Front-right IN3 red-OUT3
const int motor2pin2 = 5;  // Front-right IN4 black-OUT4
const int ENA = 6;         // Front enable pin
const int ENB = 7;         // Front enable pin

// Motor Driver 2 (Rear Wheels) pins
const int motor3pin1 = 8;  // Rear-left IN1  red-OUT1
const int motor3pin2 = 9;  // Rear-left IN2  black-OUT2
const int motor4pin1 = 10; // Rear-right IN3 red-OUT3
const int motor4pin2 = 11; // Rear-right IN4 black-OUT4
const int ENA2 = 12;       // Rear enable pin
const int ENB2 = 13;       // Rear enable pin

void setup() {
    Serial.begin(57600);

    // Configure all motor pins
    pinMode(motor1pin1, OUTPUT);
    pinMode(motor1pin2, OUTPUT);
    pinMode(motor2pin1, OUTPUT);
    pinMode(motor2pin2, OUTPUT);
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);

    pinMode(motor3pin1, OUTPUT);
    pinMode(motor3pin2, OUTPUT);
    pinMode(motor4pin1, OUTPUT);
    pinMode(motor4pin2, OUTPUT);
    pinMode(ENA2, OUTPUT);
    pinMode(ENB2, OUTPUT);

    // Initialize motor states
    stop();

    // Configure PS2 controller
    error = ps2x.config_gamepad(A5, A4, A3, A2, true, true);

    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);

    if (error == 0) {
        Serial.println("Controller configured successfully");
    } else {
        Serial.print("Error configuring controller: ");
        Serial.println(error);
    }

    type = ps2x.readType();
    switch (type) {
        case 0: Serial.println("Unknown Controller type"); break;
        case 1: Serial.println("DualShock Controller Found"); break;
        case 2: Serial.println("GuitarHero Controller Found"); break;
    }
}

// Unified movement functions for 4 wheels
void forward(int speed) {
    // Front wheels
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin2, LOW);

    // Rear wheels
    digitalWrite(motor3pin1, HIGH);
    digitalWrite(motor3pin2, LOW);
    digitalWrite(motor4pin1, LOW);
    digitalWrite(motor4pin2, HIGH);

    analogWrite(ENA, speed);
    analogWrite(ENB, speed);
    analogWrite(ENA2, speed);
    analogWrite(ENB2, speed);
}

void back(int speed) {
    // Front wheels
    digitalWrite(motor1pin1, LOW);