Professional Documents
Culture Documents
Digital Electronics - Arduino Robot Project
Digital Electronics - Arduino Robot Project
Building
An
Arduino Robot
Prepared by
Submitted to
The main component, which is the Arduino Board is a low-cost, flexible, and
easy-to-use programmable open-source microcontroller board that can be
integrated into a variety of electronic projects. One of the main advantages
of using Arduino is that we get to build our own projects. At the same time,
We get to see what works and what doesn’t work and how to problem-solve
in the process.
2. Motor Driver
3. Distance Sensor
Distance sensors send an ultrasonic signal forward and then
wait to receive a bounced signal. Depending on how long
the signal takes to bounce back the approximated distance
to an obstacle can be calculated. I will use this little device
to prevent the robot from hitting walls or other obstacles
in its way.
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
6. USB Cable
8. DC Gears Motors
9. Battery Pack
Step 3: Wiring
Connect the wires according to the circuit diagram.
Connect the power and motor wires as well as the Ultrasonic Sensor,
Servo, and L298N to the Arduino Sensor Shield to their designated part.
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
The VCC of the Arduino shield is connected to the switch and its GND
is connected to the battery.
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
The above code shows the connection between the Arduino and
motor driver L298N control pins.
//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2
This sensor includes four pins and the pin configuration of this sensor is
discussed below.
Pin Vcc: This pin provides a +5V power supply to the sensor.
Pin Trig: This is an input pin, used to initialize measurement by
transmitting ultrasonic waves by keeping this pin high for 10us.
Pin Echo: This is an output pin, which goes high for a specific time
period and it will be equivalent to the duration of the time for the
wave to return back to the sensor.
Pin GND: This is a ground pin used to connect to the GND of the
system.
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
void setup(){
Serial.begin(9600);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
Attach a Servo variable to a pin. Note that in Arduino 0016 and
earlier, the Servo library supports only servos on only two pins: 9 and 10.
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
The int for right and left distance is set to 0 to loop the code
effectively.
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
lookRight() instruction will let the robot look right and go where it is clear.
Then we use write() function to update the servo's position to the angle
selected by the potentiometer.
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
DON MARIANO MARCOS MEMORIAL STATE UNIVERSITY
MID-LA UNION CAMPUS
COLLEGE OF INFORMATION TECHNOLOGY
ITPE 101-DIGITAL ELECTRONICS
lookLeft() instruction will let the robot look left and go where it is clear.
Then we use write() function to update the servo's position to the angle
selected by the potentiometer.
int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
If the right and left side are clear it will go forward, then we used
sonar.ping_cm() it sends a ping, returns the distance in centimeters or 0
(zero) if no ping echo within set distance limit. There are other functions
(also called Methods in some cases) that return the distance in inches, or
the time it took for the eco to return.
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
if(!goesForward){
Serial.println("==moveForward==");
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward(){
goesForward=false;
Serial.println("==moveBackward==");
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
Serial.println("==turnRight==");
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
Serial.println("==turnLeft==");
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
As shown in the pictures below, the robot has displayed the necessary
function of obstacle avoiding that was programmed into the Arduino. It can
detect object a centimeter away from robot itself and finds another way to
avoid it.