Bluetooth-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and Relay-Operated Accessories
View and Edit Project In Cirkit Designer
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
- Arduino UNO: The main microcontroller board that controls the robot's functions.
- L298N DC Motor Driver: Used to control the speed and direction of the DC gearmotors.
- HC-SR04 Ultrasonic Distance Sensor: Measures distances for obstacle avoidance.
- Servomotor SG90: Positions the ultrasonic sensor for scanning the environment.
- HC-05 Bluetooth Module: Enables wireless communication for remote control.
- Gearmotor DC Wheels (Left and Right): Provide locomotion for the robot.
- Relay Module 2 Channel: Controls power to the fan and water pump.
- Fan: Cools down components or provides airflow for the robot.
- Water Pump: Can be used for water-based tasks or cooling.
- 18650 3s2p Battery Pack: Supplies power to the circuit.
- IR Receiver: Receives signals from an IR remote control.
- HX1838 Infrared IR Wireless Remote Control: Sends commands to the IR receiver.
Wiring Details
Arduino UNO
- 5V: Powers the IR receiver, servomotor, relay module, ultrasonic sensor, and HC-05 Bluetooth module.
- GND: Common ground for the ultrasonic sensor, relay module, and L298N motor driver.
- Vin: Supplies voltage to the L298N motor driver.
- A2: Connected to the ECHO pin of the ultrasonic sensor.
- A3: Connected to the TRIG pin of the ultrasonic sensor.
- A4: Controls the servomotor.
- A5: Connected to the DATA pin of the IR receiver.
- D12: Controls IN2 of the relay module.
- D11: Controls IN1 of the relay module.
- D10: Connected to ENA of the L298N motor driver.
- D9: Connected to RXD of the HC-05 Bluetooth module.
- D8: Connected to TXD of the HC-05 Bluetooth module.
- D7: Connected to IN1 of the L298N motor driver.
- D6: Connected to IN2 of the L298N motor driver.
- D5: Connected to IN3 of the L298N motor driver.
- D4: Connected to IN4 of the L298N motor driver.
- D3: Connected to ENB of the L298N motor driver.
L298N DC Motor Driver
- OUT1/OUT2: Connected to the left gearmotor.
- OUT3/OUT4: Connected to the right gearmotor.
- 12V: Powered by the Vin pin of the Arduino UNO.
- GND: Common ground with the Arduino UNO.
- ENA/ENB: Speed control for the motors, connected to the Arduino UNO.
- IN1/IN2/IN3/IN4: Direction control for the motors, connected to the Arduino UNO.
HC-SR04 Ultrasonic Distance Sensor
- VCC: Powered by the 5V pin of the Arduino UNO.
- TRIG: Trigger pin connected to A3 on the Arduino UNO.
- ECHO: Echo pin connected to A2 on the Arduino UNO.
- GND: Common ground with the Arduino UNO.
Servomotor SG90
- SIG: Signal pin controlled by A4 on the Arduino UNO.
- VCC: Powered by the 5V pin of the Arduino UNO.
- GND: Common ground with the Arduino UNO.
HC-05 Bluetooth Module
- VCC: Powered by the 5V pin of the Arduino UNO.
- GND: Common ground with the Arduino UNO.
- TXD: Transmit pin connected to D8 on the Arduino UNO.
- RXD: Receive pin connected to D9 on the Arduino UNO.
Gearmotor DC Wheels (Left and Right)
- PIN1/PIN2: Connected to the L298N motor driver outputs.
Relay Module 2 Channel
- VCC: Powered by the 5V pin of the Arduino UNO.
- GND: Common ground with the Arduino UNO.
- IN1/IN2: Control pins connected to D11 and D12 on the Arduino UNO.
- COM: Common terminal for the relay switches.
- NO1/NO2: Normally open contacts connected to the positive terminal of the battery pack.
Fan
- 5V: Powered by the relay module.
- GND: Common ground with the Arduino UNO.
Water Pump
- Positive: Powered by the relay module.
- Negative: Common ground with the Arduino UNO.
18650 3s2p Battery Pack
-
- Charge/Discharge: Powers the relay module and the fan.
-
- Charge/Discharge: Common ground with the Arduino UNO.
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