This circuit is designed to control a small autonomous robot using an Arduino UNO as the central processing unit. The robot utilizes two hobby gearmotors for movement, an HC-SR04 ultrasonic sensor for distance measurement, and an L293D motor driver IC to control the gearmotors. The circuit is powered by a 9V battery, which supplies power to both the Arduino and the motor driver.
#define Input_1 9 // control pin for motor 1 - input 1 to H-bridge controlling motor 1
#define Input_2 8 // control pin 2 for motor 1 - input 2 to H-bridge controlling motor 1
#define Input_3 13 // control pin 1 for motor 2 - input 1 to H-bridge controlling motor 2
#define Input_4 12 // control pin 2 for motor 2 - input 2 to H-bridge controlling motor 2
#define Enable_1 11 // Power Supply pin 2 for motor 1 - enable 1 to H-bridge powering motor 1
#define Enable_2 10 // Power Supply pin 2 for motor 2 - enable 2 to H-bridge powering motor 1
const int trigPin = 6;
const int echoPin = 5;
long duration;
int distance;
void setup() {
pinMode(Input_1, OUTPUT);
pinMode(Input_2, OUTPUT);
pinMode(Input_3, OUTPUT);
pinMode(Input_4, OUTPUT);
pinMode(Enable_1, OUTPUT);
pinMode(Enable_2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void goForward() {
digitalWrite(Enable_1, 100);
digitalWrite(Enable_2, 100);
digitalWrite(Input_1, HIGH);
digitalWrite(Input_2, LOW);
digitalWrite(Input_3, HIGH);
digitalWrite(Input_4, LOW);
}
void goBackward() {
digitalWrite(Enable_1, 100);
digitalWrite(Enable_2, 100);
digitalWrite(Input_1, LOW);
digitalWrite(Input_2, HIGH);
digitalWrite(Input_3, LOW);
digitalWrite(Input_4, HIGH);
}
void turnLeft() {
digitalWrite(Enable_1, 120);
digitalWrite(Enable_2, 120);
digitalWrite(Input_1, LOW);
digitalWrite(Input_2, HIGH);
digitalWrite(Input_3, HIGH);
digitalWrite(Input_4, LOW);
}
void stopMoving() {
digitalWrite(Enable_1, 0);
digitalWrite(Enable_2, 0);
}
void loop() {
digitalWrite(Enable_1, HIGH);
digitalWrite(Enable_2, HIGH);
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 < 40) {
Serial.println("HEY HEY WAIT A MINUTE MR. POSTMAN!");
delay(10);
} else {
Serial.println("Don't stop me now");
delay(10);
}
if (distance < 30) {
stopMoving();
delay(2000);
goBackward();
delay(5000);
turnLeft();
delay(5000);
} else {
goForward();
delay(10000);
}
}
This code is designed to control the autonomous robot's movement based on the distance measurements from the HC-SR04 ultrasonic sensor. The robot will move forward continuously until an object is detected within 40 cm. If the object is closer than 30 cm, the robot will stop, move backward, and then turn left before continuing forward. The motor speeds and directions are controlled by the L293D motor driver, which is interfaced with the Arduino UNO.