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

Arduino UNO Bluetooth-Controlled Obstacle-Avoiding Robot with Servo and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Bluetooth-Controlled Obstacle-Avoiding Robot with Servo and Ultrasonic Sensor

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

  1. Toggle Switch (SPST)

    • Description: Single Pole Single Throw switch
    • Pins: L1, COM
  2. 4xAA Battery Pack

    • Description: Battery pack holding four AA batteries
    • Pins: POS, NEG
  3. 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
  4. DC Motor (x2)

    • Description: Standard DC motor
    • Pins: pin 1, pin 2
  5. Servo Motor

    • Description: Standard servo motor
    • Pins: gnd, vcc, pulse
  6. 9V Battery

    • Description: Standard 9V battery
    • Pins: +, -
  7. HC-06 Bluetooth Module

    • Description: Bluetooth module for wireless communication
    • Pins: RXD, TXD, GND, VCC
  8. 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
  9. Ultrasonic Sensor

    • Description: Sensor for measuring distance
    • Pins: VCC, GND, Trig, Echo

Wiring Details

Toggle Switch (SPST)

4xAA Battery Pack

L9110 Motor Driver

DC Motor 1

DC Motor 2

Servo Motor

9V Battery

HC-06 Bluetooth Module

Arduino UNO

Ultrasonic Sensor

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 =