Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

University of Perpetual Help System Dalta

College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

Machine Problem No.5

Ultrasonic Sensor

I. OBJECTIVES

1. To be able to understand the use of ULTRASONIC Sensors and its


Pinouts.
2. Create a program that will display the variable changes in the distance
being sensed.
3. Create a program that will have an ultrasonic sensor as an input and will
determine the output function of the servo motor.

 Arduino or Genuino Board


 Ultrasonic Sensor
 Servo Motor
 hook-up wires

II. DISCUSSION

BASIC INFORMATION

Small low-cost ultrasonic distance measurement modules are an effective way to


sense the presence of nearby objects and the distance to them. Often Robots use
these to sense objects or collisions and take appropriate action.

They have two transducers, basically a speaker and a microphone. Ultrasound is


a high frequency sound (typically 40 KHz is used). A short burst of sound waves
(often only 8 cycles) is sent out the "Transmit" transducer. Then the "Receive"
transducer listens for an echo. Thus, the principle of ultrasonic distance
measurement is the same as with Radio-based radar. Distance is calculated as: L
= C × T/2 , where L is the length, C is the speed of sound in air, T is the time
difference from the transmission from the transmitter to the receiver. This is
divided by 2 for the two-directions the sound travels. Speed of sound is about: C
= 344m / s (20 degrees C room temperature).

Speed of sound in air velocity is affected by the air density, and for high accuracy
the temperature must be taken into account, either within the module electronics
(In the SRF-06 module we have) or in the Arduino software.

III. ACTIVITY

Machine Problem No.5 Ultrasonic Sensors 1


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

1. Encode the given program. Assign filename sam1.ino

const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.


// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
pulse
// whose duration is the time (in microseconds) from the sending of the
ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance


inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds) {


// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,

Machine Problem No.5 Ultrasonic Sensors 2


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

// so we divide by 2 to get the distance of the obstacle.


// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object
we
// take half of the distance travelled.
return microseconds / 29 / 2;
}

Ultrasonic Sensor with Servo Motor

#include <Servo.h>

// this constant won't change. It's the pin number of the sensor's output:
Servo myservo;
const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(9);
}

void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.


// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance


inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Machine Problem No.5 Ultrasonic Sensors 3


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);

if(inches<=20)
{
myservo.write(90);
delay(1000);

}
else
{
myservo.write(0);
delay(1000);
}
}

long microsecondsToInches(long microseconds) {


// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}

2. Wire the corresponding schematic diagram.

Ultrasonic Sensor

Machine Problem No.5 Ultrasonic Sensors 4


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

Ultrasonic Sensor with Servo Motor

3. Observe and gather the conclusion.

Machine Problem No.5 Ultrasonic Sensors 5


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

IV. PICTURE OF THE OUTPUT

Machine Problem No.5 Ultrasonic Sensors 6


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

Fig. 1 Ultrasonic Sensor with Servo Motor

Fig. 2 Ultrasonic Sensor with two Servo Motor using Tinkercad

V. SOURCE CODE

Machine Problem No.5 Ultrasonic Sensors 7


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

Ultrasonic Sensor with two Servo Motor using Tinkercad

#include <Servo.h>

Servo servoLeft;
Servo servoRight;

// this constant won't change. It's the pin number of the sensor's output:

const int pingPin = 7;

void setup() {

servoLeft.attach(6);
servoRight.attach(9);

void loop() {

long duration, inches, cm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);

if(inches<=20)

Machine Problem No.5 Ultrasonic Sensors 8


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

{
servoLeft.write(180);
servoRight.write(0);
delay(1000);

}
else
{
servoLeft.write(90);
servoRight.write(90);
delay(1000);
}
}

long microsecondsToInches(long microseconds) {


// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}

VI. OBSERVATION

In this laboratory activity, we are tasked to use ultrasonic senor with


servo motor. Using the given diagram and breadboard, we
connected the jumping wires to the ultrasonic senor and servo
motor. First, the 5v output of Arduino was connected to the sensors

Machine Problem No.5 Ultrasonic Sensors 9


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

VCC then, the trigger and echo was connected to the digital I/O and
lastly the sensor ground was connected to the Arduino ground.
When it comes to the servo motor it was easily installed since its
wire is color coded, the red wire was connected to the 5v, the brown
wire to the ground and orange wire to the PWM of the Arduino.
When it comes to the coding part the source code was already
given, but when it comes to the two servo motors the source code
needs a little editing. In the source code using two servo motors, we
declared that there’s two servo motors that will be used (Servo
servoLeft, Servo servoRight).

VII. CONCLUSION

In conclusion, ultrasonic sensors function when the sensor emits an


ultrasonic wave and receives the wave reflected back from the
target.

Machine Problem No.5 Ultrasonic Sensors 10


ENGR. CYD LAURENCE B. SANTOS
University of Perpetual Help System Dalta
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY

We tested this statement by making our own device, when we setup


the maximum and minimum range of the sensor on how it will
detect object, we used the servo motor as the output device. The
servo motor rotates to a certain angle when the sensor detects
object nearby. Ultrasonic sensors also have its own limitations like it
can detect false signals coming from the airwaves disturbed by an
air conditioning system and a pulse coming from a ceiling fan or it
cannot distinguish between different shapes and sizes.

Machine Problem No.5 Ultrasonic Sensors 11


ENGR. CYD LAURENCE B. SANTOS

You might also like