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

Arduino UNO-Based Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Control

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

Circuit Documentation

Summary

This document provides a detailed overview of a circuit that includes an Arduino UNO microcontroller, a Tower Pro SG90 servo motor, an L298N DC motor driver, an HC-SR04 ultrasonic sensor, and two hobby motors. The circuit is designed to control the servo motor and the DC motors based on distance measurements from the ultrasonic sensor.

Component List

  1. Arduino UNO

    • Description: A 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. Tower Pro SG90 Servo

    • Description: A small servo motor commonly used in hobby projects.
    • Pins: Signal, +5V, GND
  3. L298N DC Motor Driver

    • Description: A dual H-Bridge motor driver that allows control of two DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  4. HC-SR04 Ultrasonic Sensor

    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  5. Motor amarillo motorreductor hobby

    • Description: A small DC motor with a gearbox, commonly used in hobby projects.
    • Pins: vcc, GND

Wiring Details

Arduino UNO

Tower Pro SG90 Servo

L298N DC Motor Driver

HC-SR04 Ultrasonic Sensor

Motor amarillo motorreductor hobby

Code Documentation

#include <Servo.h>

// Pin definitions
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 8;
const int motorIn1 = 4;
const int motorIn2 = 5;
const int motorIn3 = 6;
const int motorIn4 = 7;

Servo myServo;

void setup() {
    Serial.begin(9600);

    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(motorIn1, OUTPUT);
    pinMode(motorIn2, OUTPUT);
    pinMode(motorIn3, OUTPUT);
    pinMode(motorIn4, OUTPUT);

    // Attach servo
    myServo.attach(servoPin);
    myServo.write(90);
}

void loop() {
    int distance = measureDistance();
    // Distance measure program
}

int measureDistance() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    long duration = pulseIn(echoPin, HIGH);
    int distance = duration * 0.034 / 2;
    return distance;
}

// Direction check
int checkDirection(int angle) {
    myServo.write(angle);
    delay(500);
    return measureDistance();
}

This code initializes the pins for the ultrasonic sensor, servo motor, and DC motor driver. It measures the distance using the ultrasonic sensor and controls the servo motor based on the measured distance. The DC motors are controlled through the L298N motor driver.