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

Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensor and H-Bridge Motor Control

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensor and H-Bridge Motor Control

Circuit Documentation

Summary

This circuit is designed to control a robot using an Arduino UNO microcontroller. The robot is equipped with an ultrasonic sensor (HC-SR05) for obstacle detection and uses an H-Bridge motor driver (Puente H carrito) to control two DC motors. The robot moves forward, stops, reverses, and turns based on the distance measured by the ultrasonic sensor.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • 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
  2. Puente H carrito

    • Description: H-Bridge motor driver for controlling DC motors.
    • Pins: OUT1, OUT2, OUT3, OUT4, 12V, GND, 5V, ENA, IN1, IN2, IN3, IN4, ENB, 5VEN
  3. DC Motor (Motor 1)

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  4. DC Motor (Motor 2)

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  5. 2x 18650 Battery Pack

    • Description: Battery pack providing power to the circuit.
    • Pins: vcc, gnd
  6. HC-SR05

    • Description: Ultrasonic sensor for distance measurement.
    • Pins: VCC, TRIG, GND, ECHO, OUT

Wiring Details

Arduino UNO

Puente H carrito

DC Motor (Motor 1)

DC Motor (Motor 2)

2x 18650 Battery Pack

HC-SR05

Documented Code

/*
  Ten program ustawia piny, mierzy odległość za pomocą czujnika ultradźwiękowego i steruje silnikami w zależności od wykrytej odległości. Jeśli odległość do przeszkody jest mniejsza niż 20 cm, robot zatrzymuje się, cofa, a następnie skręca w prawo. W przeciwnym razie porusza się do przodu.
  Upewnij się, że odpowiednio podłączyłeś wszystkie komponenty zgodnie z Twoim schematem. Powodzenia!
*/

#include <IRremote.h>

// Definicje pinów
const int trigPin = 6;
const int echoPin = 7;
const int motorA1 = 9;
const int motorA2 = 8;
const int motorB1 = 5;
const int motorB2 = 4;
const int enableA = 10;
const int enableB = 3;

// Funkcja do pomiaru odległości
long measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;
  return distance;
}

// Funkcje do sterowania silnikami
void moveForward() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW);
}

void moveBackward() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}

void turnLeft() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW);
}

void turnRight() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}

void stopMotors() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}

void setup() {
  // Ustawienie pinów
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
  pinMode(enableA, OUTPUT);
  pinMode(enableB, OUTPUT);

  // Włączenie silników
  analogWrite(enableA, 255);
  analogWrite(enableB, 255);
}

void loop() {
  // Pomiar odległości
  long distance = measureDistance();

  if (distance < 20) {
    stopMotors();
    delay(500);
    moveBackward();
    delay(500);
    turnRight();
    delay(500);
  } else {
    moveForward();
  }
}

This code sets up the pins, measures the distance using the ultrasonic sensor, and controls the motors based on the detected distance. If the distance to an obstacle is less than 20 cm, the robot stops, reverses, and then turns right. Otherwise, it moves forward.