Obstacle 31 40
Obstacle 31 40
Figure 3.7 L293D Motor Driver Shield (Adapted from Lelong, 2020)
19
It has Pull down resistors to disable motors during power up
The Arduino reset button is brought to the top
It is compatible with Mega, UNO & Duemilanove
Its dimensions are: 69mm x 53mm x 14.3mm
3.3 Methodology
3.3.1 Robot Construction
3.3.1.1 Robot Working Principle
The robot uses the Ultrasonic sensor to measure the distance in front of it then it moves. As the distance
reduces, the robot interprets it as the presence of an obstacle. As soon as the robot detects the obstacle,
it stops and moves back a few cm then looks left and right before moving to a free path.
20
Figure 3.8 Block diagram showing the Hardware Implementation of the project
21
3.3.1.4 Constructing the Robot car
Step 1. Connect the motor and wheels to the chassis by soldering the thick red and black wires to
the positive and negative terminals of the motors then attach them to the chassis [32].
Step 2. Attach the switch, battery clip, SG90 Servo Motor and Arduino UNO to the chassis. After
this, mount the L293D Motor Driver shield on the Arduino UNO R3 and the HC-SR04 to the
Servo motor.
22
Figure 3.11 Components attached to the chassis
23
3.3.2 Programming the Robot
The Arduino microcontroller communicates with the PC via the USB connection. Data is transferred
between the board and the PC bit by bit. An adaptor is used for power supply to the board and a USB
programmer is used to burn the hardware program (written in Arduino IDE) into the board.
24
3.3.2.1 Flowchart of the program
Step 2. We declare the pins that are connected to the ultrasonic sensor. We also declare variables
which will be used to store info in this code:
#define TRIG_PIN A0 /* Pin A0 on the Motor Drive Shield soldered to the ultrasonic sensor*/
#define ECHO_PIN A1 /* Pin A1 on the Motor Drive Shield soldered to the ultrasonic sensor*/
#define MAX_DISTANCE 300 /* sets maximum useable sensor measuring distance to 300cm*/
#define MAX_SPEED 160 /* sets speed of DC traction motors to 150/250 or about 70% of full speed
- to get power drain down.*/
#define MAX_SPEED_OFFSET 40 /* this sets offset to allow for differences between the two DC
traction motors*/
#define COLL_DIST 30 /* sets distance at which robot stops and reverses to 30cm*/
#define TURN_DIST COLL_DIST+20 /* sets distance at which robot veers away from object*/
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); /* sets up sensor library to use the
correct pins to measure distance.*/
Step 3. We initialize the servo motor by creating an object of the servo library. Also we initialize the
Motors to which the wheels are connected using the AF library:
AF_DCMotor leftMotor1(1, MOTOR12_1KHZ); /* create motor #1 using M1 output on Motor Drive
Shield, set to 1kHz PWM frequency*/
26
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ); /* create motor #2, using M2 output, set to 1kHz
PWM frequency*/
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);/* create motor #3, using M3 output, set to 1kHz
PWM frequency*/
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);/* create motor #4, using M4 output, set to 1kHz
PWM frequency*/
Servo myservo; // create servo object to control a servo
Step 5. We go into the void setup () function where we initialize the servo motor and set it to look
straight using a 90 degrees angle.
void setup() {
myservo.attach(10); /* attaches the servo on pin 10 (SERVO_1 on the Motor Drive Shield to the
servo object */
myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
delay(1000); // delay for one seconds
}
Step 6. We go into the loop function and measure the distance and set it to change direction if the
forward path is blocked
void loop() {
myservo.write(90); // move eyes forward
delay(90);
curDist = readPing(); // read distance
if (curDist < COLL_DIST) {changePath();} /* if forward is blocked change direction*/
27
delay(500);
}
Step 6. We instruct it on how it is to change path.
First the void changePath () function instructs it to stop forward movement, then check and store right
distance(by turning the servo motor 36 degrees), check and store left distance(by turning the servo
motor 144 degrees) and then return sensor to the centre position.
After this, we go into the void compareDistance () function and instruct it to follow the longer distance.
void changePath() {
moveStop(); // stop forward movement
myservo.write(36); // check distance to the right
delay(500);
rightDistance = readPing(); //set right distance
delay(500);
myservo.write(144); // check distace to the left
delay(700);
leftDistance = readPing(); //set left distance
delay(500);
myservo.write(90); //return to center
delay(100);
compareDistance();
}
28