Merhaba. Bu konumuzda HC-SR04 ultrasonic sensörünü tanıyıp cisim algılama ve de mesafe ölçümü yapacağız. Ses dalgaları sınıflandırılmasında 20 KHz-1 GHz arasındaki ses sinyalleri ultrasonic ses olarak tanımlanmıştır. Kullanacağımız sensörve bir çok ultrasonic sensör 40Khz frekansında ultrasonic ses üretmektedir.

scriptaccess" value="always">



HC-SR04 Mesafe Sensörü


Aslında HC-SR04 tüm haliyle bir transdüserdir. Transdüser ile sensör arasındaki fark ise sensör herhangi fiziksel bir değişimi algılayan bir eleman iken, transdüser bu değişimi algılayıp elektrik,ısı vs. bir çok enerji formuna dönüştürebilen bir elektronik devredir.

HC-SR04'te 2 adet araba farını andıran bir tanesi ses iletici diğeri de ses alıcı olan transdüser bulunmakta. Sol taraftan 40 KHz'de üretilen ses sinyal darbesi engele çarpıp sağ tarafa gelir ve biz bu gidiş-geliş arasındaki zamanı, cm'ye çevirerek uzaklığı ölçeceğiz.

T=0 zamanında darbe iletilir(ultrasonic ses sinyali), cisimden yansır, transdüser tarafından algılanır ve tekrar
gönderilir.Sonraki darbe ilk darbenin ultrasonik enerjisinin hepsi absorbe edildiğinde iletilmelidir.Bu yüzden sensöre bir pals gönderilir sensör okunur ve sensörün datasheetinde yazan süre kadar sensöre tekrar pals gönderilmez.Eğer bekleme yapmaksak sensör saçma değerler döndürür.Çünkü ilk yolladıgımız sinyal bir yerden yansıyarak sensöre geri dönmeye devam eder.

Sensör üzerinde 4 adet pin mevcut.Bunlar;vcc,gnd,trig,echo pinleri.Sensör kendi içerisinde 40khz frekansında bir sinyal üretip 8 pals verici transdüsere gönderiyor.Bu ses dalgası
havada, deniz seviyesinde ve 15 °C sıcaklıkta 340 m/s bir hızla yol alır.Bir cisme çarpar ve geri sensöre yansır.Cismin sensörden uzaklıgı ile doğru orantılı olarak echo pini bir süre lojik 1 seviyesinde kalır ve tekrar lojik 0 olur.Bizim bu mesafeyi ölçmekiçin tek yapmamız gereken echo pininin ne kadar lojik1 oldugunun süresini bulmaktır.Bu yapı yukarıdaki resimden daha iyi görülebilir.

Echo pininin lojik 1de kalma süresini mikrodenetleyici timerı ile ölçelim. Klasik yol=hız*zaman dan ve sesin 340m/sn lik hızınıda 34000cm/1000000us (340000/1000000=1/29) olarak düzenlersek formül x=t/29 olarak bulunur.Fakat burada önemli bir nokta sensörün bize döndürdüğü süre, ses sinyalinin gönderilmesi ile alınması arasındaki oluşan süre. Yani yol=zaman/29 ile aradaki mesafeyi iki ile çarpmış oluyoruz.Çünkü ses hem gitti hem geldi.Bu yüzden x=t/58 formulüyle aradaki mesafeyi ölçmüş olacağız . Bazı yerlerde 58 yerine 58.2 veya 59 yazanı da gördüm artık sizin kararınıza kalmış birşey.

Devre şeması:


Arduino kodları:
description">Kod:
/* Created by Raphango ~ 14/feb/2014
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
"Come to me, all you who are weary and burdened, and I will give you rest.
Take my yoke upon you and learn from me, for I am gentle and humble in heart,
and you will find rest for your souls.For my yoke is easy and my burden is light."
Matthew 11:28-30
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#########Breadboard circuit mounting:########
 * LCD VSS pin to ground
 * LCD VDD pin to +5V
 * 2K2Ohm resistor: ends to LCD VO pin and ground
 * LCD RS pin to digital pin 9
 * LCD R/W pin to ground
 * LCD Enable pin to digital pin 8
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD A pin to +5V
 * LCD K pin to ground
 * Buzzer + to 220Ohm Resistor leg and - to ground. The other resistor leg goes to pin 12.
 * HC-SCR GND to ground
 * HC-SCR Echo to pin 11
 * HC-SCR Trig to pin 10
 * HC-SCR Vcc to +5V
##############################################
*/

//Defining pins
#define trigPin 10
#define echoPin 11
#define buzPin 12
#define RS 9
#define Enable 8
#define D4 5
#define D5 4
#define D6 3
#define D7 2

//Including the LCD and sound library:
#include <LiquidCrystal.h>
#include "pitches.h"

// Initialize the LCD library
LiquidCrystal lcd(RS, Enable, D4, D5, D6, D7);

/////Definition of the LCD special characters///
uint8_t cc0[8] = {0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07};
uint8_t cc1[8] = {0x00,0x00,0x00,0x00,0x00,0x10,0x18,0x1C};
uint8_t cc2[8] = {0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00};
uint8_t cc3[8] = {0x1C,0x18,0x10,0x00,0x00,0x00,0x00,0x00};
uint8_t cc4[8] = {0x00,0x00,0x00,0x10,0x18,0x0C,0x04,0x06};
uint8_t cc5[8] = {0x06,0x04,0x0C,0x18,0x10,0x00,0x00,0x00};
uint8_t cc6[8] = {0x10,0x08,0x0C,0x06,0x02,0x03,0x03,0x03};
uint8_t cc7[8] = {0x03,0x03,0x03,0x02,0x06,0x0C,0x08,0x10};

////Sound notes setup
int melody0[] = {NOTE_B3, NOTE_B3,NOTE_B3, NOTE_G3, NOTE_B3,0, NOTE_D4, NOTE_D3};
int melody1[] = {NOTE_C6,NOTE_C6,NOTE_C6,NOTE_C6};
int melody2[] = {NOTE_C6,NOTE_C6,NOTE_C6};
int melody3[] = {NOTE_C6,NOTE_C6};
int melody4[] = {NOTE_C6};

//Add melodies here, and at the end of the "music" array.
int* music[6/*number of melodies + 1*/] = {melody0, melody1, melody2, melody3, melody4 /*,add here*/};

////Sound notes durations
int notesMelody0[] = {8,4,4,8,8,8,2,4};
int notesMelody1[] = {8,8,8,8};
int notesMelody2[] = {8,8,8};
int notesMelody3[] = {8,8};
int notesMelody4[] = {4};

//Add durations of the melodies here, and at the end of the "noteDurations" array.
int* noteDurations[6/*number of melodies + 1*/] = {notesMelody0, notesMelody1, notesMelody2 , notesMelody3 , notesMelody4  /*,add here*/};

////Sound sizes
int sizeMelody0 = sizeof(melody0)/sizeof(int);
int sizeMelody1 = sizeof(melody1)/sizeof(int);
int sizeMelody2 = sizeof(melody2)/sizeof(int);
int sizeMelody3 = sizeof(melody3)/sizeof(int);
int sizeMelody4 = sizeof(melody4)/sizeof(int);
//Add sizes here, and at the end of the "sizes" array.
int sizes[] = {sizeMelody0,sizeMelody1,sizeMelody2,sizeMelody3,sizeMelody4 /*,add here*/};

////Tone Function - Plays the tone
int playSong(int track, float durConst){
    for (int thisNote = 0; thisNote < sizes[track]; thisNote++) {
    int noteDuration = 1000/noteDurations[track][thisNote];
    tone(buzPin, music[track][thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * durConst;
    delay(pauseBetweenNotes);
    noTone(buzPin);
  }
}

//Declaring a variable that will count the millis so the blinking is independent of "delays"
unsigned long millisCounter = 0;
int displaying = 0;

////////////////////////////////////
//////////////SETUP/////////////////
////////////////////////////////////
void setup(){
  // Set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Set up the LCD custom chars (8 maximum)
  lcd.createChar(0, cc0);
  lcd.createChar(1, cc1);
  lcd.createChar(2, cc2);
  lcd.createChar(3, cc3);
  lcd.createChar(4, cc4);
  lcd.createChar(5, cc5);
  lcd.createChar(6, cc6);
  lcd.createChar(7, cc7);
  //Initializes HC-SCR04 pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //Plays intro song
  playSong(0,1.30);
}

////////////////////////////////////
//////////////LOOP//////////////////
////////////////////////////////////

void loop()
{
  unsigned long millisNow = millis();
  long duration;
  int mesafe;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //Measures the mesafe
  duration = pulseIn(echoPin, HIGH);
  mesafe = (duration/2) / 29.1;

  if (mesafe >= 400 || mesafe <= 0){//Evaluate the mesafe value.
    lcd.setCursor(0, 0);
    lcd.print("Fora do alcance    ");
    lcd.setCursor(0, 1);
    lcd.print("                  ");
    }
  else {

    Serial.print(mesafe);
    Serial.println(" cm");
    lcd.setCursor(0, 0);
    lcd.print("Mesafe:");
    lcd.setCursor(2, 1);
    lcd.print(mesafe);
    lcd.print(" cm.  ");
    
    //mesafe animation
    if(mesafe <= 10)
    {
      if(millisNow-millisCounter>50 && displaying == 0){//This part is used for the blinking while at 10 cm less.
      //Each of these "lcd.setCursor" and "lcd.write" are responsible for the drawings.
      lcd.setCursor(10, 0);//This positionates the cursor on the desired slot
      lcd.write(byte(0));//This writes the prefab symbols
      lcd.write(byte(1));
      lcd.write(byte(4));
      lcd.write(" ");
      lcd.write(" ");
      lcd.write(" ");
      lcd.setCursor(10, 1);
      lcd.write(byte(2));
      lcd.write(byte(3));
      lcd.write(byte(5));
      lcd.write(" ");
      lcd.write(" ");
      lcd.write(" ");
      playSong(1,1.30);//This plays the "beep"
      millisCounter = millisNow;
      displaying = 1;
      } else {lcd.setCursor(10, 0);lcd.write("      ");lcd.setCursor(10, 1);lcd.write("      ");playSong(1,1.30);displaying=0;millisCounter = millisNow;}
    }
    else if(mesafe <= 30)
    {
    lcd.setCursor(10, 0);
    lcd.write(byte(0));
    lcd.write(byte(1));
    lcd.write(byte(4));
    lcd.write(byte(6));
    lcd.write(" ");
    lcd.write(" ");
    lcd.setCursor(10, 1);
    lcd.write(byte(2));
    lcd.write(byte(3));
    lcd.write(byte(5));
    lcd.write(byte(7));
    lcd.write(" ");
    lcd.write(" ");
    playSong(2,2.07);
    }
    else if(mesafe <= 70){
    lcd.setCursor(10, 0);
    lcd.write(byte(0));
    lcd.write(byte(1));
    lcd.write(byte(4));
    lcd.write(byte(6));
    lcd.write(byte(6));
    lcd.write(" ");
    lcd.setCursor(10, 1);
    lcd.write(byte(2));
    lcd.write(byte(3));
    lcd.write(byte(5));
    lcd.write(byte(7));
    lcd.write(byte(7));
    lcd.write(" ");
    playSong(3,3.60);
    }
    else if(mesafe <= 100){
    lcd.setCursor(10, 0);
    lcd.write(byte(0));
    lcd.write(byte(1));
    lcd.write(byte(4));
    lcd.write(byte(6));
    lcd.write(byte(6));
    lcd.write(byte(6));
    lcd.setCursor(10, 1);
    lcd.write(byte(2));
    lcd.write(byte(3));
    lcd.write(byte(5));
    lcd.write(byte(7));
    lcd.write(byte(7));
    lcd.write(byte(7));
    playSong(4,3.60);
    }
  }
}
Kodları derleyebilmek için ekteki sıkıştırılmış klasörün içinden çıkan Animated-Sonic-Ruler-master.zip isimli kütüphaneyi arduinoya ekleyiniz.

İndir: Eklenti 219

Kaynak: https://temrinlerim.org/index.php?topic=195.0