Following is an attempt to create the Obstacle Avoiding robot using 2 gear motors. The front distance is being measured by the ultrasonic sensor.
Note: We aren't using a servo motor to measure the right and left distance here
Have Fun !
- 4x Resistor 220Ω
- HCSR04 ultrasonic sensor
- 2x gear motors
- 1x L293D motor driver for controlling the gear motors
- Jumper Wires
- Arduino UNO
- The circuit comprises of 4 different colour leds to let the user know what the robot is about to do be it move forward, backward, right or left
- Here we haven't used a servo motor so the robot will be always measuring the distance to an obstacle in front of it.
- The two gear motors are being controlled via the motor driver which is present since it can support max 2 motors.
- If you wish to build a 4 gear motor robot then make sure to use another motor drive for the control of the 2 new gear motors.
Motor Driver Configuration
8pins above and 8 pins down
Power 1 | Input 4 | Output 4 | GND | GND | Output 3 | Input 3 | Enable 3&4 |
---|---|---|---|---|---|---|---|
Enable 1&2 | Input 1 | Output 1 | GND | GND | Output 2 | Input 1 | Power 2 |
Important part of the code. Make sure to refer the .ino file provided !
//-------Configuration of Ultrasonic Sensor---------
long duration;
float Front_dist;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Front_dist = (duration * 0.03444)/2;
//inch = (Front_dist/2.54);
//---------------------------------------------------
analogWrite(enable_right, 255);
analogWrite(enable_left, 255);
if(Front_dist<50){
digitalWrite(forward_led,HIGH);
digitalWrite(backward_led,HIGH); //red led
stop();
delay(100);
digitalWrite(forward_led,LOW);
backward();
delay(100);
digitalWrite(right_led,HIGH); //blue led
right();
delay(100);
digitalWrite(right_led,LOW);
digitalWrite(left_led,HIGH); //orange led
}else{
digitalWrite(forward_led,HIGH); //green led
digitalWrite(backward_led,LOW);
digitalWrite(right_led,LOW);
digitalWrite(left_led,LOW);
forward();
}