Arduino UNO Bluetooth-Controlled Obstacle-Avoiding Robot with Servo and Ultrasonic Sensor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a robotic system using an Arduino UNO microcontroller. The system includes a Bluetooth module (HC-06) for wireless communication, a servo motor for precise angular control, two DC motors for movement, an ultrasonic sensor for distance measurement, and a motor driver (L9110) to control the DC motors. The power supply is provided by a 9V battery and a 4xAA battery pack. A toggle switch is used to control the power to the circuit.
Component List
Toggle Switch (SPST)
- Description: Single Pole Single Throw switch
- Pins: L1, COM
4xAA Battery Pack
- Description: Battery pack holding four AA batteries
- Pins: POS, NEG
L9110 Motor Driver
- Description: Dual H-Bridge motor driver
- Pins: MOTOR B-A, MOTOR B-B, MOTOR A-A, MOTOR A-B, B-IA, B-IB, GND, VCC, A-IA, A-IB
DC Motor (x2)
- Description: Standard DC motor
- Pins: pin 1, pin 2
Servo Motor
- Description: Standard servo motor
- Pins: gnd, vcc, pulse
9V Battery
- Description: Standard 9V battery
- Pins: +, -
HC-06 Bluetooth Module
- Description: Bluetooth module for wireless communication
- Pins: RXD, TXD, GND, VCC
Arduino UNO
- Description: Microcontroller board
- 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
Ultrasonic Sensor
- Description: Sensor for measuring distance
- Pins: VCC, GND, Trig, Echo
Wiring Details
Toggle Switch (SPST)
- L1: Connected to NEG pin of 4xAA Battery Pack and - pin of 9V Battery
- COM: Connected to GND pin of Arduino UNO and GND pin of L9110 Motor Driver
4xAA Battery Pack
- POS: Connected to VCC pin of L9110 Motor Driver
- NEG: Connected to L1 pin of Toggle Switch
L9110 Motor Driver
- MOTOR B-A: Connected to pin 2 of DC Motor 1
- MOTOR B-B: Connected to pin 1 of DC Motor 1
- MOTOR A-A: Connected to pin 2 of DC Motor 2
- MOTOR A-B: Connected to pin 1 of DC Motor 2
- B-IA: Connected to D3 pin of Arduino UNO
- B-IB: Connected to D5 pin of Arduino UNO
- GND: Connected to COM pin of Toggle Switch
- VCC: Connected to POS pin of 4xAA Battery Pack
- A-IA: Connected to D6 pin of Arduino UNO
- A-IB: Connected to D11 pin of Arduino UNO
DC Motor 1
- pin 1: Connected to MOTOR B-B pin of L9110 Motor Driver
- pin 2: Connected to MOTOR B-A pin of L9110 Motor Driver
DC Motor 2
- pin 1: Connected to MOTOR A-B pin of L9110 Motor Driver
- pin 2: Connected to MOTOR A-A pin of L9110 Motor Driver
Servo Motor
- gnd: Connected to GND pin of Arduino UNO
- vcc: Connected to 5V pin of Arduino UNO
- pulse: Connected to D10 pin of Arduino UNO
9V Battery
- +: Connected to Vin pin of Arduino UNO
- -: Connected to L1 pin of Toggle Switch
HC-06 Bluetooth Module
- TXD: Connected to D13 pin of Arduino UNO
- RXD: Connected to D12 pin of Arduino UNO
- GND: Connected to GND pin of Arduino UNO
- VCC: Connected to 5V pin of Arduino UNO
Arduino UNO
- GND: Connected to COM pin of Toggle Switch, GND pin of HC-06 Bluetooth Module, GND pin of Ultrasonic Sensor, and gnd pin of Servo Motor
- Vin: Connected to + pin of 9V Battery
- D13: Connected to TXD pin of HC-06 Bluetooth Module
- D12: Connected to RXD pin of HC-06 Bluetooth Module
- D11: Connected to A-IB pin of L9110 Motor Driver
- D10: Connected to pulse pin of Servo Motor
- D9: Connected to Echo pin of Ultrasonic Sensor
- D8: Connected to Trig pin of Ultrasonic Sensor
- D6: Connected to A-IA pin of L9110 Motor Driver
- D5: Connected to B-IB pin of L9110 Motor Driver
- D3: Connected to B-IA pin of L9110 Motor Driver
Ultrasonic Sensor
- VCC: Connected to 5V pin of Arduino UNO
- GND: Connected to GND pin of Arduino UNO
- Trig: Connected to D8 pin of Arduino UNO
- Echo: Connected to D9 pin of Arduino UNO
Documented Code
#include <Servo.h>
#include <SoftwareSerial.h>
int A_1A = 6;
int A_1B = 11;
int B_1A = 5;
int B_1B = 3;
int trigPin = 8;
int echoPin = 9;
int btRxPin = 12;
int btTxPin = 13;
SoftwareSerial bluetoothSerial(btTxPin, btRxPin);
int servoPin = 10;
Servo servo;
int speed = 120;
int cheak;
void forward() {
analogWrite(A_1A, speed);
analogWrite(A_1B, 0);
analogWrite(B_1A, speed);
analogWrite(B_1B, 0);
}
void back() {
analogWrite(A_1A, 0);
analogWrite(A_1B, speed);
analogWrite(B_1A, 0);
analogWrite(B_1B, speed);
}
void left() {
analogWrite(A_1A, speed);
analogWrite(A_1B, 0);
analogWrite(B_1A, speed);
analogWrite(B_1B, 0);
}
void right() {
analogWrite(A_1A, 0);
analogWrite(A_1B, 0);
analogWrite(B_1A, speed);
analogWrite(B_1B, 0);
}
void stop() {
analogWrite(A_1A, 0);
analogWrite(A_1B, 0);
analogWrite(B_1A, 0);
analogWrite(B_1B, 0);
}
float getDistanceCM() {
digitalWrite(echoPin, LOW);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float distance = pulseIn(echoPin, HIGH, 8000) / 58;
return distance;
}
float getStableDistanceCM() {
int cnt = 0, cm = 0;
long int CmSum = 0;
for (int i = 0; i < 20; i++) {
cm = getDistanceCM();
if (cm == 0) continue;
cnt++;
CmSum += cm;
}
return (cnt == 0) ? 0 : CmSum / cnt;
}
void servoWrite(int deg) {
servo.attach(servoPin);
servo.write(deg);
delay(500);
servo.detach();
}
void setup() {
Serial.begin(9600);
bluetoothSerial.begin(38400);
pinMode(A_1A, OUTPUT);
pinMode(A_1B, OUTPUT);
pinMode(B_1A, OUTPUT);
pinMode(B_1B, OUTPUT);
digitalWrite(A_1A, LOW);
digitalWrite(A_1B, LOW);
digitalWrite(B_1A, LOW);
digitalWrite(B_1B, LOW);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servoWrite(85);
delay(1000);
}
void loop() {
int cm = getStableDistanceCM();
long leftDistance = 0;
long rightDistance =