In: Electrical Engineering
An arduino autonomous robot has four tracking modes forward, backward, left and right. You will need two DC motors attached to the two wheels of robot. These motor should be driven by L293d or any other motor driver. This motor driver is then should be connected to the Arduino pins. Arduino will control the movement of the robot by using these pins.
An autonomous robot should have a capabilty of sensing any object or obstacle. To perform this function, you can use an ultrasonic sensor like HC SR04 and attach it to Arduino.
You sense obstacle by HCSR04 and the arduino control the DC motors (wheel). Here is the code that I used in Automatic braking system. You can easily understand what the code is doing? and you can also modify the code to suits your application.
const int trigPin = 10;
const int echoPin = 12;
long duration;
int distance;
void forward();
void stop();
int motor1_pin1 = 4;
int motor1_pin2 = 3;
int motor2_pin1 = 9;
int motor2_pin2 = 8;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(12, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
pinMode(motor1_pin1, OUTPUT);
pinMode(motor1_pin2, OUTPUT);
pinMode(motor2_pin1, OUTPUT);
pinMode(motor2_pin2, OUTPUT);
forward();
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance < 25) {
stop();
}
else {
forward();
}
}
void forward() {
digitalWrite(motor2_pin1, HIGH);
digitalWrite(motor2_pin2, LOW);
digitalWrite(motor1_pin1, HIGH);
digitalWrite(motor1_pin2, LOW);
}
void backward() {
digitalWrite(motor2_pin1, LOW);
digitalWrite(motor2_pin2, HIGH);
digitalWrite(motor1_pin1, LOW);
digitalWrite(motor1_pin2, HIGH);
}
void right() {
digitalWrite(motor2_pin1, HIGH);
digitalWrite(motor2_pin2, LOW);
digitalWrite(motor1_pin1, LOW);
digitalWrite(motor1_pin2, LOW);
}
void left() {
digitalWrite(motor2_pin1, LOW);
digitalWrite(motor2_pin2, LOW);
digitalWrite(motor1_pin1, HIGH);
digitalWrite(motor1_pin2, LOW);
}
void stop() {
digitalWrite(motor2_pin1, LOW);
digitalWrite(motor2_pin2, LOW);
digitalWrite(motor1_pin1, LOW);
digitalWrite(motor1_pin2, LOW);
}