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

Bluetooth-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and Relay-Operated Accessories

View and Edit Project In Cirkit DesignerImage of Bluetooth-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and Relay-Operated Accessories

Circuit Documentation

Summary

This circuit is designed to control a mobile robot with various functionalities including motor control, distance measurement, Bluetooth communication, and relay switching for additional peripherals. The core of the circuit is an Arduino UNO microcontroller, which interfaces with an L298N DC motor driver for controlling two DC gearmotors, an HC-SR04 ultrasonic distance sensor for obstacle detection, a servomotor for directional control, an HC-05 Bluetooth module for wireless communication, and a 2-channel relay module for controlling external devices like a fan and a water pump. The circuit is powered by a 18650 3s2p battery pack.

Component List

Wiring Details

Arduino UNO

L298N DC Motor Driver

HC-SR04 Ultrasonic Distance Sensor

Servomotor SG90

HC-05 Bluetooth Module

Gearmotor DC Wheels (Left and Right)

Relay Module 2 Channel

Fan

Water Pump

18650 3s2p Battery Pack

Documented Code

#include <AltSoftSerial.h>

AltSoftSerial BT_Serial; // RX on pin 8, TX on pin 9

// Motor control pins
#define enA 10 // Enable1 L298 Pin enA
#define in1 7  // Motor1 L298 Pin in1
#define in2 6  // Motor1 L298 Pin in2
#define in3 5  // Motor2 L298 Pin in1
#define in4 4  // Motor2 L298 Pin in1
#define enB 3  // Enable2 L298 Pin enB

// Relay control pins
#define relay1 12 // Relay 1 control pin
#define relay2 11 // Relay 2 control pin

// Ultrasonic sensor pins
#define echo A2    // Echo pin
#define trigger A3 // Trigger pin

// Servo control pin
#define servo A4

int distance_F = 30;
long distance;
int set = 20;
int bt_data; // Variable to receive data from Bluetooth
int Speed = 130;
int mode = 0;

// Relay state variables
bool relay1State = false;
bool relay2State = false;

void setup() {
  // Set up motor pins
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB, OUTPUT);

  // Set up relay pins
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  // Set up ultrasonic sensor pins
  pinMode(echo, INPUT);
  pinMode(trigger, OUTPUT);

  // Set up servo pin
  pinMode(servo, OUTPUT);

  // Initialize serial communication
  Serial.begin(9600);
  BT_Serial.begin(9600);

  // Sweep the servo at startup
  for (int angle = 70; angle <= 140; angle += 5) {
    servoPulse(servo, angle);
  }
  for (int angle = 140; angle >= 0; angle -= 5) {
    servoPulse(servo, angle);
  }
  for (int angle = 0; angle <= 70; angle += 5) {
    servoPulse(servo, angle);
  }
  delay(500);
}

void loop() {
  // Read Bluetooth data
  if (BT_Serial.available() > 0) {
    bt_data = BT_Serial.read();
    Serial.println(bt_data);

    // Handle motor and relay control
    handleBluetoothCommands(bt_data);
  }

  // Update motor speeds
  analogWrite(enA, Speed);
  analogWrite(enB, Speed);

  if (mode == 2) {
    // Obstacle avoidance mode
    distance_F = Ultrasonic_read();
    Serial.print("S="); Serial.println(distance_F);
    if (distance_F > set) {
      forward();
    } else {
      Check_side();
    }
  }

  delay(10);
}

// Handle Bluetooth commands for motor and relay control
void handleBluetoothCommands(int command) {
  // Motor control
  if (command == 8) {
    mode = 0;
    Stop();
  } else if (command == 10) {
    mode = 2;
    Speed = 255; // Max speed for obstacle avoidance
  }

  if (mode == 0) {  // Manual Bluetooth control mode
    if (command == 1) {
      forward();
    } else if (command == 2) {
      backward();
    } else if (command == 3) {
      turnLeft();
    } else if (command == 4) {
      turnRight();
    } else if (command == 5) {
      Stop();
    }
  }

  // Relay control
  if (command == 11) {
    relay1State = !relay1State;
    digitalWrite(relay1, relay1State ? HIGH : LOW);
  }

  if (command == 12) {
    relay2State = !relay2State;
    digitalWrite(relay2, relay2State