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

Arduino UNO Bluetooth-Controlled Robot with L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO Bluetooth-Controlled Robot with L298N Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a pair of DC gearmotors through an L298N motor driver module, with commands received via a Bluetooth HC-06 module. The Arduino UNO serves as the central microcontroller, interfacing with the Bluetooth module for wireless communication and the motor driver to control the motors' speed and direction. The power supply for the motor driver is provided by two 18650 Li-ion batteries, and a rocker switch is included to control the power flow. The circuit is intended for a small robotic vehicle that can move forward, backward, turn left, and right, and adjust its speed.

Component List

Wiring Details

Arduino UNO

18650 Li-ion Battery x 2

L298N DC Motor Driver

Gearmotor DC / Motorreductor

Bluetooth HC-06

Rocker Switch

Documented Code

// Define motor driver pins
int IN1 = 13;  // Motor A direction pin 1 (left forward)
int IN2 = 12;  // Motor A direction pin 2 (left backward)
int IN3 = 11;  // Motor B direction pin 1 (right forward)
int IN4 = 10;  // Motor B direction pin 2 (right backward)
int ENA = 9;   // Motor A speed pin (PWM)
int ENB = 8;   // Motor B speed pin (PWM)

// Bluetooth commands
char command;
int speed = 255;  // Default speed (high)

// Speed levels
const int LOW_SPEED = 100;
const int MEDIUM_SPEED = 180;
const int HIGH_SPEED = 255;

void setup() {
  // Set up motor pins as outputs
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  
  // Initialize Bluetooth communication
  Serial.begin(9600);  // HC-06 baud rate

  // Set initial speed
  analogWrite(ENA, speed); 
  analogWrite(ENB, speed); 
}

void loop() {
  // Check if there's a command from the Bluetooth module
  if (Serial.available() > 0) {
    command = Serial.read();  // Read the command

    // Movement control
    if (command == 'F') {
      moveForward();
    }
    else if (command == 'B') {
      moveBackward();
    }
    else if (command == 'L') {
      turnLeft();
    }
    else if (command == 'R') {
      turnRight();
    }
    else if (command == 'S') {
      stopCar();
    }

    // Speed control
    else if (command == '1') { // Low speed
      setSpeed(LOW_SPEED);
    }
    else if (command == '2') { // Medium speed
      setSpeed(MEDIUM_SPEED);
    }
    else if (command == '3') { // High speed
      setSpeed(HIGH_SPEED);
    }
  }
}

// Movement functions
void moveForward() {
  digitalWrite(IN1, HIGH); // Left motor forward
  digitalWrite(IN2, LOW);  // Left motor backward
  digitalWrite(IN3, HIGH); // Right motor forward
  digitalWrite(IN4, LOW);  // Right motor backward
}

void moveBackward() {
  digitalWrite(IN1, LOW);  // Left motor forward
  digitalWrite(IN2, HIGH); // Left motor backward
  digitalWrite(IN3, LOW);  // Right motor forward
  digitalWrite(IN4, HIGH); // Right motor backward
}

void turnLeft() {
  digitalWrite(IN1, HIGH);  // Left motor backward
  digitalWrite(IN2, LOW); // Left motor forward
  digitalWrite(IN3, LOW); // Right motor forward
  digitalWrite(IN4, HIGH);  // Right motor backward
}

void turnRight() {
  digitalWrite(IN1, LOW); // Left motor forward
  digitalWrite(IN2, HIGH);  // Left motor backward
  digitalWrite(IN3, HIGH);  // Right motor backward
  digitalWrite(IN4, LOW); // Right motor forward
}

void stopCar() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

// Function to set speed
void setSpeed(int newSpeed) {
  speed = newSpeed;
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
}

This code is designed to run on the Arduino UNO microcontroller. It initializes the motor driver pins and sets up the Bluetooth communication. The loop function listens for commands from the Bluetooth module to control the movement and speed of the motors. The movement functions moveForward, moveBackward, turnLeft, turnRight, and stopCar control the direction of the motors, while the setSpeed function adjusts the PWM signal to the motor driver for speed control.