33 Pengujian rangkaian tersebut yaitu dengan memprogram arduino
kemudian data akan ditampilkan pada PC, berikut adalah program yang akand didownload pada arduino.
void setup { initialize serial at 9600 bits per second:
Serial.begin9600; }
the loop routine runs over and over again forever: void loop {
Serial.println“tes serial”; delay1;
delay in between reads for stability }
4.5. Pengujian keseluruhan
Program arduino include Servo.h
Servo leftRightServo; set a variable to map the servo
int leftRightPos = 0; set a variable to store the servo position
const int numReadings = 5; set a variable for the number of readings to take
int index = 0; the index of the current reading
Universitas Sumatera Utara
34 int total = 0; the total of all readings
int average = 0; the average int echoPin = 8; the SRF05s echo pin
int initPin = 10; the SRF05s init pin unsigned long pulseTime = 0;
variable for reading the pulse unsigned long distance = 0;
variable for storing distance
void setup { leftRightServo.attach9;
make the init pin an output: pinModeinitPin, OUTPUT;
make the echo pin an input: pinModeechoPin, INPUT;
initialize the serial port: Serial.begin9600;
} void loop {
for leftRightPos
= 0;
leftRightPos 180;
leftRightPos++ { going left to right.
leftRightServo.writeleftRightPos;
Universitas Sumatera Utara
35 for index = 0; index = numReadings; index++ {
take x number of readings from the sensor and average them
digitalWriteinitPin, LOW; delayMicroseconds10;
digitalWriteinitPin, HIGH;
send signal delayMicroseconds10;
wait 50 microseconds for it to return digitalWriteinitPin, LOW;
close signal pulseTime = pulseInechoPin, HIGH;
calculate time for signal to return distance = pulseTime 58;
convert to centimetres total = total + distance;
update total delay1;
} average = total numReadings;
create average reading if index = numReadings {
reset the counts when at the last item of the array index = 0;
total = 0;
Universitas Sumatera Utara
36 }
if average 300 {average = 300;} Serial.printX;
print leading X to mark the following value as degrees
Serial.printleftRightPos; current servo position
Serial.printV; preceeding character to separate values
Serial.printlnaverage; average of sensor readings
}
for leftRightPos
= 180;
leftRightPos 0;
leftRightPos-- { going right to left
leftRightServo.writeleftRightPos; for index = 0; index = numReadings; index++ {
digitalWriteinitPin, LOW; delayMicroseconds10;
digitalWriteinitPin, HIGH; delayMicroseconds10;
digitalWriteinitPin, LOW; pulseTime = pulseInechoPin, HIGH;
distance = pulseTime 58;
Universitas Sumatera Utara
37 total = total + distance;
delay1; }
average = total numReadings; if index = numReadings {
index = 0; total = 0;
} if average 300 {average = 300;}
Serial.printX; Serial.printleftRightPos;
Serial.printV; Serial.printlnaverage;
} }
Program processing import processing.serial.; import serial library
Serial arduinoport; declare a serial port float x, y; variable to store x and y co-
ordinates for vertices int radius = 350; set the radius of objects
int w = 300; set an arbitary width value int degree = 0; servo position in degrees
int value = 0; value from sensor
Universitas Sumatera Utara
38 int motion = 0;
value to store which way the servo is panning int[] newValue = new int[181];
create an array to store each new sensor value for each servo position
int[] oldValue = new int[181]; create an array to store the previous values.
PFont myFont; setup fonts in Processing
int radarDist = 0; set value to configure Radar distance labels
int firstRun = 0; value to ignore triggering motion on the first 2
servo sweeps create background and serial buffer
void setup{ setup the background size, colour and font.
size750, 450; background 0; 0 = black
myFont = createFontverdana, 12; textFontmyFont;
setup the serial port and buffer arduinoport = new Serialthis, Serial.list[0], 9600;
} draw the screen
Universitas Sumatera Utara
39 void draw{
fill0; set the following shapes to be black
noStroke; set the following shapes to have no outline
ellipseradius, radius, 750, 750; draw a circle with a width height = 750 with its
center position x and y set by the radius rectModeCENTER;
set the following rectangle to be drawn around its center
rect350,402,800,100; draw rectangle x, y, width, height
if degree = 179 { if at the far right then set motion = 1 true were
about to go right to left motion = 1;
this changes the animation to run right to left }
if degree = 1 { if servo at 0 degrees then were about to go left to
right motion = 0;
this sets the animation to run left to right }
Universitas Sumatera Utara
40 strokeWeight7;
set the thickness of the lines if motion == 0 {
if going left to right for int i = 0; i = 20; i++ {
draw 20 lines with fading colour each 1 degree stroke0, 10i, 0;
set the stroke colour Red, Green, Blue base lineradius,radius,
radius +
cosradiansdegree+180+iw, radius
+ sinradiansdegree+180+iw;
linestart x, start y, end x, end y }
} else { if going right to left
for int i = 20; i = 0; i-- { draw 20 lines with fading colour
stroke0,200-10i, 0; using standard RGB values, each between 0 and 255
lineradius,radius,radius+cosradiansdegree+180+i w,radius + sinradiansdegree+180+iw;
} }
noStroke; no outline
Universitas Sumatera Utara
41 fill0,50,0;
set the fill colour of the shape Red, Green, Blue beginShape;
start drawing shape for int i = 0; i 180; i++ {
for each degree in the array x = radius + cosradians180+ioldValue[i];
create x coordinate y = radius + sinradians180+ioldValue[i];
create y coordinate vertexx, y;
plot vertices }
endShape; end shape
fill0,110,0; beginShape;
for int i = 0; i 180; i++ { x = radius + cosradians180+inewValue[i];
y = radius + sinradians180+inewValue[i]; vertexx, y;
} endShape;
average fill0,170,0;
Universitas Sumatera Utara
42 beginShape;
for int i = 0; i 180; i++ { x=radius+cosradians180+inewValue[i]+oldValue[i
]2; create average
y=radius+sinradians180+inewValue[i]+oldValue[i ]2;
vertexx, y; }
endShape; if firstRun = 360 {
stroke150,0,0; strokeWeight1;
noFill; for int i = 0; i 180; i++ {
if oldValue[i] - newValue[i] 35 || newValue[i] - oldValue[i] 35 {
x = radius + cosradians180+inewValue[i]; y = radius + sinradians180+inewValue[i];
ellipsex, y, 10, 10; }
} }
for int i = 0; i =6; i++{ noFill;
Universitas Sumatera Utara
43 strokeWeight1;
stroke0, 255-30i, 0; ellipseradius, radius, 100i, 100i;
fill0, 100, 0; noStroke;
textInteger.toStringradarDist+50, 380,
305- radarDist, 50, 50;
radarDist+=50; }
radarDist = 0; draw the grid lines on the radar every 30 degrees
and write their values 180, 210, 240 etc.. for int i = 0; i = 6; i++ {
strokeWeight1; stroke0, 55, 0;
lineradius, radius,
radius +
cosradians180+30iw, radius
+ sinradians180+30iw;
fill0, 55, 0; noStroke;
if 180+30i = 300 { textInteger.toString180+30i,
radius+10 +
cosradians180+30iw+10, radius+10
+ sinradians180+30iw+10, 25,50;
} else {
Universitas Sumatera Utara
44 textInteger.toString180+30i,
radius +
cosradians180+30iw, radius
+ sinradians180+30iw, 60,40;
} }
Write information text and values. noStroke;
fill0; rect350,402,800,100;
fill0, 100, 0; textDegrees: +Integer.toStringdegree, 100, 380,
100, 50; use Integet.toString to convert numeric to string as text only outputs strings
textDistance: +Integer.toStringvalue, 100, 400, 100, 50; textstring, x, y, width, height
textRadar screen code , 540, 380, 250, 50; fill0;
rect70,60,150,100; fill0, 100, 0;
textScreen Key:, 100, 50, 150, 50; fill0,50,0;
rect30,53,10,10; textFirst sweep, 115, 70, 150, 50;
fill0,110,0; rect30,73,10,10;
Universitas Sumatera Utara
45 textSecond sweep, 115, 90, 150, 50;
fill0,170,0; rect30,93,10,10;
textAverage, 115, 110, 150, 50; noFill;
stroke150,0,0; strokeWeight1;
ellipse29, 113, 10, 10; fill150,0,0;
textMotion, 115, 130, 150, 50; }
get values from serial port void serialEvent Serial arduinoport {
String xString = arduinoport.readStringUntil\n; read the serial port until a new line
if xString = null { if theres data in between the new lines
xString = trimxString; get rid of any whitespace just in case
String getX
= xString.substring1,
xString.indexOfV; get the value of the servo position
String getV = xString.substringxString.indexOfV+1, xString.length; get the value of the sensor
reading
Universitas Sumatera Utara
46 degree = Integer.parseIntgetX; set the values to
variables value = Integer.parseIntgetV;
oldValue[degree] = newValue[degree]; store the values in the arrays.
newValue[degree] = value; sets a counter to allow for the first 2 sweeps of
the servo firstRun++;
if firstRun 360 { firstRun = 360; keep the value at 360
} }
}
Universitas Sumatera Utara
47 Gambar 4.2 Hasil radar
Universitas Sumatera Utara
48
KESIMPULAN DAN SARAN
Prinsip kerja daripada sensor ultrasonic
gelombang ultrasonik dibangkitkan melalui sebuah alat yang disebut dengan piezoelektrik dengan frekuensi tertentu. Piezoelektrik ini akan
menghasilkan gelombang ultrasonik umumnya berfrekuensi 40kHz ketika sebuah osilator diterapkan pada benda tersebut. Secara umum, alat ini akan
menembakkan gelombang ultrasonik menuju suatu area atau suatu target. Setelah gelombang menyentuh permukaan target, maka target akan memantulkan
kembali gelombang tersebut. Gelombang pantulan dari target akan ditangkap oleh sensor, kemudian sensor
menghitung selisih antara waktu pengiriman gelombang dan waktu gelombang pantul diterima.
Secara detail, cara kerja sensor ultrasonik adalah sebagai berikut:
Sinyal dipancarkan oleh pemancar ultrasonik dengan frekuensi tertentu dan dengan durasi waktu tertentu. Sinyal tersebut berfrekuensi diatas
20kHz. Untuk mengukur jarak benda sensor jarak, frekuensi yang umum digunakan adalah 40kHz.
Sinyal yang dipancarkan akan merambat sebagai gelombang bunyi dengan kecepatan sekitar 340 ms. Ketika menumbuk suatu benda, maka sinyal
tersebut akan dipantulkan oleh benda tersebut.
Setelah gelombang pantulan sampai di alat penerima, maka sinyal tersebut akan diproses untuk menghitung jarak benda tersebut. Jarak benda
dihitung berdasarkan rumus :
S = 340.t2
dimana S merupakan jarak antara sensor ultrasonik dengan benda bidang pantul, dan t adalah selisih antara waktu pemancaran gelombang oleh transmitter dan
waktu ketika gelombang pantul diterima receiver.
Universitas Sumatera Utara
49
Hasil dari visualisasi Radar di PC.
Pengaplikasian Sensor Ultrasonik dalam kehidupan sehari-hari.
Dalam bidang kesehatan, gelombang ultrasonik bisa digunakan untuk melihat organ-organ dalam tubuh manusia seperti untuk mendeteksi tumor, liver,
otak dan menghancurkan batu ginjal. Gelombang ultrasonik juga dimanfaatkan pada alat USG ultrasonografi yang biasa digunakan oleh dokter kandungan.
Dalam bidang industri, gelombang ultrasonik digunakan untuk mendeteksi keretakan pada logam, meratakan campuran besi dan timah, meratakan campuran
susu agar homogen, mensterilkan makanan yang diawetkan dalam kaleng, dan membersihkan benda benda yang sangat halus. Gelombang ultrasonik juga bisa
digunakan untuk mendeteksi keberadaan mineral maupun minyak bumi yang tersimpan di dalam perut bumi.
Dalam bidang pertahanan, gelombang ultrasonik digunakan sebagai radar atau navigasi, di darat maupun di dalam air. Gelombang ultrasonik digunakan oleh
kapal pemburu untuk mengetahui keberadaan kapal selam, dipasang pada kapal selam untuk mengetahui keberadaan kapal yang berada di atas permukaan air,
mengukur kedalaman palung laut, mendeteksi ranjau, dan menentukan posisi sekelompok ikan.
Universitas Sumatera Utara
4
BAB 2 Landasan Teori