Upload the Code:
Connect the Arduino to your computer and upload the code using the Arduino IDE.
Power the Robot:
Power the robot with the battery pack or a suitable power source.
Test Your Robot:
Place obstacles in front of the robot and observe how it navigates to avoid collisions.
This simple project demonstrates the basics of robotics, sensor integration, and autonomous behavior. You can expand on this project by adding more sensors, improving obstacle avoidance algorithms, and even implementing remote control via Bluetooth or Wi-Fi.
#include <NewPing.h>
#define TRIGGER_PIN 2
#define ECHO_PIN 3
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int motor1Pin1 = 9;
int motor1Pin2 = 10;
int motor2Pin1 = 5;
int motor2Pin2 = 6;
void setup() {
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
int distance = sonar.ping_cm();
if (distance < 20) {
// Obstacle detected, turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else {
// No obstacle, move forward
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
}
Edited by
caa on 03-11-2023 15:40,
10 months agoProject: Arduino Obstacle-Avoiding Robot
Components Needed:
Arduino board (e.g., Arduino Uno)
Ultrasonic sensor (HC-SR04)
Motor driver module (L298N)
Two DC motors
Chassis and wheels
Battery pack (6xAA batteries) or a rechargeable battery pack
Jumper wires
Breadboard (optional)
Instructions:
Assemble the Robot:
Assemble the chassis, attach the DC motors to the wheels, and mount the wheels to the chassis.
Connect the motor driver module to the motors and the Arduino.
Connect the Ultrasonic Sensor:
Connect the VCC and GND pins of the ultrasonic sensor to the Arduino's 5V and GND pins, respectively.
Connect the Echo and Trig pins of the sensor to any two digital pins on the Arduino (e.g., D2 and D3).
Programming:
Write the Arduino code for the obstacle-avoiding behavior. This code will use the ultrasonic sensor to detect obstacles and make the robot move forward, backward, left, or right to avoid collisions.
Edited by
caa on 03-11-2023 15:38,
10 months ago