Professional Documents
Culture Documents
University of Perpetual Help System Dalta: Ultrasonic Sensor I. Objectives
University of Perpetual Help System Dalta: Ultrasonic Sensor I. Objectives
College of Engineering
CpE Department
CONTROL SYSTEMS LABORATORY
Ultrasonic Sensor
I. OBJECTIVES
II. DISCUSSION
BASIC INFORMATION
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
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 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);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
#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 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);
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);
}
}
Ultrasonic Sensor
V. SOURCE CODE
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
// this constant won't change. It's the pin number of the sensor's output:
void setup() {
servoLeft.attach(6);
servoRight.attach(9);
void loop() {
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)
{
servoLeft.write(180);
servoRight.write(0);
delay(1000);
}
else
{
servoLeft.write(90);
servoRight.write(90);
delay(1000);
}
}
VI. OBSERVATION
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