Building an obstacle avoidance robot with Arduino is an exciting and educational project. This type of robot uses sensors to detect and avoid obstacles in its path, making it an excellent hands-on experience for learning about robotics and electronics. In this article, we will guide you step by step on how to build your own obstacle-avoidance robot using an Arduino board.
What You Will Need
Before you start building your robot, it’s important to gather all the necessary components. Here’s a list of what you’ll need:- Arduino UNO board
- Ultrasonic sensor (HC-SR04)
- DC motors with motor driver module (L298N)
- Wheels and chassis
- Jumper wires
- Battery pack
- Breadboard (optional)
- Motor driver (L298N or similar)
- Power supply
Step 1: Set Up the Arduino Board
The Arduino UNO will serve as the brain of your robot. Begin by connecting the Arduino board to your computer using a USB cable. Install the necessary Arduino IDE on your computer to write and upload code to the board.Step 2: Wiring the Motor Driver
To control the motors, you’ll need a motor driver. The L298N is a popular motor driver that can control both the direction and speed of the DC motors.- Connect the power pins: Connect the VCC and GND pins of the L298N to your power source (usually 5V and ground).
- Motor connections: Connect the OUT1 and OUT2 pins of the L298N to the terminals of the first motor, and OUT3 and OUT4 to the second motor.
- Control pins: Connect the IN1, IN2, IN3, and IN4 pins to the Arduino digital pins (you will specify the exact pins in the code later).
Step 3: Connecting the Ultrasonic Sensor
The ultrasonic sensor (HC-SR04) is essential for detecting obstacles in front of your robot. It sends out a pulse and calculates the time it takes for the pulse to bounce back from an obstacle, thus determining the distance.- VCC and GND connections: Connect the VCC and GND of the ultrasonic sensor to the power and ground on the Arduino.
- Trigger and Echo pins: Connect the Trigger pin to an Arduino digital pin (for example, pin 9), and the Echo pin to another digital pin (for example, pin 10).
Step 4: Assemble the Robot Chassis and Motors
Now it’s time to assemble the physical body of the robot. Mount the motors and wheels onto the robot chassis. Make sure the motors are securely attached and aligned so the robot can move smoothly. The wheels will be powered by the DC motors and controlled by the motor driver.Step 5: Powering the Robot
You will need a power source to run both the Arduino and the motors. A typical setup involves using a battery pack that connects to the Arduino’s Vin pin (for the Arduino) and the motor driver module. Ensure that the power supply voltage matches the requirements of the components.Writing the Code for Your Obstacle Avoidance Robot
With all the hardware set up, the next step is to program the Arduino board. Below is a simple code that will enable the robot to avoid obstacles by moving forward until an obstacle is detected.cpp
#include <AFMotor.h> #define trigPin 9 #define echoPin 10 #define motor1Pin1 3 #define motor1Pin2 4 #define motor2Pin1 5 #define motor2Pin2 6 long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // Initialize motors AFMotor motor1(motor1Pin1, motor1Pin2); AFMotor motor2(motor2Pin1, motor2Pin2); } void loop() { // Send pulse from ultrasonic sensor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) * 0.0344; if (distance > 10) { motor1.setSpeed(255); motor2.setSpeed(255); motor1.run(FORWARD); motor2.run(FORWARD); } else { motor1.run(RELEASE); motor2.run(RELEASE); } }
How the Code Works
- The trigPin sends a pulse to the ultrasonic sensor to measure the distance to the nearest object.
- If the object is detected within 10 cm, the motors stop. Otherwise, the motors continue moving the robot forward.