Ini adalah tutorial untuk membantu Anda memahami ultrasonik, buzzer dan mempelajari lebih dalam Arduino. Skema ini dibangun untuk mendeteksi gerakan rintangan dan menimbulkan peringatan dengan sensor Ultrasonik.


Alat :

1. Test Board

2. Ultrasonic sensor

3. Arduino cable

4. +5V buzzer

5. Male to male pins

6. Arduino uno board


Hubungkan Sirkuit :

Hubungkan terminal positif Buzzer ke pin Arduino 2 dan terminal negatif ke Gnd. 

Hubungkan pin VCC ultrasonik ke pin +5v dan Gnd ke ground.

Hubungkan pin trigonometri ke pin 10 dan pin echo ke pin 9.

Gambar koneksi dibawah ini.




Koding Arduino :

// Define pins for ultrasonic and buzzer

int const trigPin = 10;

int const echoPin = 9;

int const buzzPin = 2;


void setup()

{

pinMode(trigPin, OUTPUT); // trig pin will have pulses output

pinMode(echoPin, INPUT); // echo pin should be input to get pulse width

pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering

}

void loop()

{

// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters

int duration, distance;

// Output pulse with 1ms width on trigPin

digitalWrite(trigPin, HIGH); 

delay(1);

digitalWrite(trigPin, LOW);

// Measure the pulse input in echo pin

duration = pulseIn(echoPin, HIGH);

// Distance is half the duration devided by 29.1 (from datasheet)

distance = (duration/2) / 29.1;

// if distance less than 0.5 meter and more than 0 (0 or less means over range) 

    if (distance <= 50 && distance >= 0) {

    // Buzz

    digitalWrite(buzzPin, HIGH);

    } else {

    // Don't buzz

    digitalWrite(buzzPin, LOW);

    }

    // Waiting 60 ms won't hurt any one

    delay(60);

}

/* This code by a_atef45@yahoo.com */