Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Obstacle Avoidance with Arduino: A Practical Guide

Obstacle avoidance is a crucial feature in robotics, enabling autonomous robots to navigate environments without colliding with obstacles. This capability is essential for applications ranging from simple hobbyist robots to complex industrial automation systems. Using Arduino, a popular open-source electronics platform, we can develop an effective obstacle avoidance system. This article will guide you through the process, providing detailed instructions and code examples to help you get started.

Projeto: In this project, we will build a simple obstacle avoidance robot using an Arduino board and an ultrasonic sensor. The robot will be able to detect obstacles in its path and change direction to avoid collisions. The main objectives are:

  1. Detect obstacles using an ultrasonic sensor.
  2. Control the movement of the robot using DC motors.
  3. Implement a basic algorithm for obstacle avoidance.

Lista de componentes:

  • 1 x Arduino Uno
  • 1 x Ultrasonic Sensor (HC-SR04)
  • 2 x DC Motors
  • 1 x Motor Driver (L298N)
  • 1 x Chassis for the robot
  • 1 x Battery pack (appropriate for your motors and Arduino)
  • Jumper wires
  • Breadboard (optional)

Exemplos:

  1. Wiring the Components:

    • Connect the ultrasonic sensor to the Arduino:
      • VCC to 5V
      • GND to GND
      • Trig to digital pin 9
      • Echo to digital pin 10
    • Connect the motor driver to the Arduino and the motors:
      • IN1 to digital pin 2
      • IN2 to digital pin 3
      • IN3 to digital pin 4
      • IN4 to digital pin 5
      • ENA to digital pin 6
      • ENB to digital pin 7
      • Motor A to OUT1 and OUT2
      • Motor B to OUT3 and OUT4
      • VCC to 12V (or appropriate voltage for your motors)
      • GND to GND
  2. Arduino Code:

    // Define pins for ultrasonic sensor
    const int trigPin = 9;
    const int echoPin = 10;
    
    // Define pins for motor driver
    const int motorA1 = 2;
    const int motorA2 = 3;
    const int motorB1 = 4;
    const int motorB2 = 5;
    const int enableA = 6;
    const int enableB = 7;
    
    void setup() {
     // Initialize serial communication
     Serial.begin(9600);
    
     // Set motor driver pins as outputs
     pinMode(motorA1, OUTPUT);
     pinMode(motorA2, OUTPUT);
     pinMode(motorB1, OUTPUT);
     pinMode(motorB2, OUTPUT);
     pinMode(enableA, OUTPUT);
     pinMode(enableB, OUTPUT);
    
     // Set ultrasonic sensor pins
     pinMode(trigPin, OUTPUT);
     pinMode(echoPin, INPUT);
    }
    
    void loop() {
     // Get distance from ultrasonic sensor
     long duration, distance;
     digitalWrite(trigPin, LOW);
     delayMicroseconds(2);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);
     distance = (duration / 2) / 29.1;
    
     // Print distance to serial monitor
     Serial.print("Distance: ");
     Serial.println(distance);
    
     // Obstacle avoidance logic
     if (distance < 20) {
       // Stop motors
       digitalWrite(motorA1, LOW);
       digitalWrite(motorA2, LOW);
       digitalWrite(motorB1, LOW);
       digitalWrite(motorB2, LOW);
       delay(500);
    
       // Turn right
       digitalWrite(motorA1, HIGH);
       digitalWrite(motorA2, LOW);
       digitalWrite(motorB1, LOW);
       digitalWrite(motorB2, HIGH);
       delay(1000);
     } else {
       // Move forward
       digitalWrite(motorA1, HIGH);
       digitalWrite(motorA2, LOW);
       digitalWrite(motorB1, HIGH);
       digitalWrite(motorB2, LOW);
     }
    
     delay(100);
    }

    Explanation of the Code:

    • The ultrasonic sensor is used to measure the distance to the nearest obstacle.
    • If an obstacle is detected within 20 cm, the robot stops and turns right.
    • If no obstacle is detected, the robot moves forward.
    • The motor driver controls the direction and speed of the DC motors.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.