Arduino UNO-Based Obstacle-Avoiding Robot with Ultrasonic Sensor and H-Bridge Motor Control
View and Edit Project In Cirkit Designer
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
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
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
DC Motor (Motor 1)
- Description: Standard DC motor.
- Pins: pin 1, pin 2
DC Motor (Motor 2)
- Description: Standard DC motor.
- Pins: pin 1, pin 2
2x 18650 Battery Pack
- Description: Battery pack providing power to the circuit.
- Pins: vcc, gnd
HC-SR05
- Description: Ultrasonic sensor for distance measurement.
- Pins: VCC, TRIG, GND, ECHO, OUT
Wiring Details
Arduino UNO
- 5V connected to VCC of HC-SR05
- GND connected to GND of HC-SR05
- GND connected to gnd of 2x 18650 Battery Pack
- GND connected to GND of Puente H carrito
- Vin connected to vcc of 2x 18650 Battery Pack
- Vin connected to 12V of Puente H carrito
- D10 connected to ENA of Puente H carrito
- D9 connected to IN1 of Puente H carrito
- D8 connected to IN2 of Puente H carrito
- D7 connected to ECHO of HC-SR05
- D6 connected to TRIG of HC-SR05
- D5 connected to IN3 of Puente H carrito
- D4 connected to IN4 of Puente H carrito
- D3 connected to ENB of Puente H carrito
Puente H carrito
- OUT1 connected to pin 2 of DC Motor (Motor 2)
- OUT2 connected to pin 1 of DC Motor (Motor 2)
- OUT3 connected to pin 2 of DC Motor (Motor 1)
- OUT4 connected to pin 1 of DC Motor (Motor 1)
DC Motor (Motor 1)
- pin 1 connected to OUT4 of Puente H carrito
- pin 2 connected to OUT3 of Puente H carrito
DC Motor (Motor 2)
- pin 1 connected to OUT2 of Puente H carrito
- pin 2 connected to OUT1 of Puente H carrito
2x 18650 Battery Pack
- vcc connected to Vin of Arduino UNO
- vcc connected to 12V of Puente H carrito
- gnd connected to GND of Arduino UNO
- gnd connected to GND of Puente H carrito
HC-SR05
- VCC connected to 5V of Arduino UNO
- GND connected to GND of Arduino UNO
- TRIG connected to D6 of Arduino UNO
- ECHO connected to D7 of Arduino UNO
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.