You are on page 1of 11

Code untuk video:

/* HC-SR04 Ping distance sensor] VCC to arduino 5v GND to arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 More info at: http://goo.gl/kJ8Gl */ #define trigPin 12 #define echoPin 13 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { int duration, distance; digitalWrite(trigPin, HIGH); delayMicroseconds(1000); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance >= 200 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }

The Theory Behind It:


In the program above, we want to calculate the distance of an object in front of the ultrasonic sensor. This sensor can send a "ping" at a given moment and receive the ping bouncing back on an object at another given moment. A ping is nothing but a sound that is inaudible for the human hear and this is why this sensor is called "ultrasonic". The sensor send a ping at a time t1 and receive the bouncing ping at a time t2. Knowing the speed of sound, the time difference t = t2 - t1 can give us an idea of the distance of an object. Example, if t = 500 microseconds, we know it took 250 microseconds for the ping to hit an object and another 250 microseconds to come back. The approximate speed of sound in dry air is given by the formula: c = 331.5 + 0.6 * [air temperature in degrees Celsius] At 20C, c = 331.5 + 0.6 * 20 = 343.5 m/s If we convert the speed in centimetres per microseconds we get: c = 343.5 * 100 / 1000000 = 0.03435 cm/s

The distance is therefore, D = (t/2) * c or D = 250 * 0.03435 = 8.6 cm Instead of using the Speed of Sound, we can also use the "Pace of Sound". The Pace of Sound = 1 / Speed of Sound = 1 / 0.03435 = 29.1 s/cm In this case the equation to calculate the distance become: D = (t/2) / Pace of sound and for the example above: D = 250 / 29.1 = 8.6 cm

Sensor Ultrasonic Ping Parallax


SAHUURR...SAHUURR..SAHURR...!!! Sambil nunggu sahur mendingan ngeposting tentang sensor ping)))...

Sensor ultrasonic adalah sebuah sensor yang memanfaatkan pancaran gelombang ultrasonic. Sensor ultrasonic ini terdiri dari rangkaian pemancar ultrasonic yang disebut transmitter dan rangkaian penerima ultrasonic disebut receiver. Sensor ini dapat mengukur jarak antara 2 cm sampai 300 cm. keluaran dari sensor ini berupa pulsa yang lebarnya merepresentasikan jarak. Lebar pulsanya bervariasi dari 115 uS sampai 18,5 mS. Sensor ultrasonic ping parallax terdiri dari sebuah chip pembangkit sinyal 40KHz, sebuah speaker ultrasonik dan sebuah mikropon ultrasonik. Speaker ultrasonik mengubah sinyal 40 KHz menjadi suara sementara mikropon ultrasonik berfungsi untuk mendeteksi pantulan suaranya.

Berikut adalah cara kerja sensor ultrasonic ping parallax. 1. Pin yang digunakan sebagai jalur data sensor dijadikan output.

2. Mikrokontroler memberikan pulsa trigger (pulsa high dengan tOUT selama 2 s sampai 5 s). 3. Kemudian setelah memberikan trigger, pin tersebut dijadikan input. 4. Sensor memancarkan gelombang ultrasonic sebesar 40KHz selama 200 s (tBURST). 5. Gelombang ultrasonic ini akan merambat diudara dengan kecepatan 344.424 m/detik atau 1 cm setiap 29.034 s. 6. Gelombang tersebut akan mengenai objek kemudian terpantul kembali ke sensor. 7. Selama menunggu pantulan, sensor akan menghasilkan sebuah pulsa (high) 8. Pulsa ini akan berhenti (low) ketika gelombang suara pantulan terdeteksi oleh sensor. 9. Lebar pulsa tersebutlah yang yang dipresentasikan sebagai jarak antara sensor ping dengan objek. 10. Lebar pulsa high (tIN) akan sesuai dengan lama waktu tempuh gelombang ultrasonik untuk 2x jarak ukur dengan obyek yang kemudian dapat merepresentasikan jarak antara sensor ping dengan objek. 11. Sinyal yang diterima oleh rangkaian receiver dikirimkan ke rangkaian mikrokontroler untuk selanjutnya diolah untuk menghitung jarak terhadap benda. 12. Benda di sini adalah benda yang bersifat memantul, bukan benda yang bersifat meredam sinyal.

Untuk menghitung jarak yang terukur dari waktu terima sensor dapat menggunakan persamaan berikut ini.

Di mana: V = adalah kecepatan suara 344 m/s t = adalah waktu tempuh (s) s = adalah jarak (m) Contoh perhitungan jika diketahui kecepatan suara 344,424m/s, kemudian waktu tempuhnya adalah 115uS (tIN min sensor ping parallax) dan 18,5mS (tIN max sensor ping parallax), berapa jarak yang terukur ?

Untuk program (Arduino) pembacaan sensor ping parallax dari awal memberikan trigger pulsa high sampai didapatkan nilai jarak dari pembacaan sensor dapat dilihat dibawah ini.
void setup() { //inisialisasi komunikasi serial Serial.begin(9600); } void sensor_ping(const int pingPin) { //inisialisasi variabel float duration, cm; //pin arduino dijadikan output pinMode(pingPin, OUTPUT); //berikan pulsa low sebelumnya

//untuk memastikan pulsa high nanti, selama 2us digitalWrite(pingPin, LOW); delayMicroseconds(2); //memberikan trigger pulsa high selama 2-5 us digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); //pin arduino dijadikan input pinMode(pingPin, INPUT); //membaca nilai sinyal yang diberikan oleh sensor ping (pulsa high) //sampai sinyal tersebut berhenti (pulsa low) //durasi pulsa high tersebutlah waktu(uS) pemancaran gelombang //ultrasonik dari awal //sampai ping mendeteksigelombang tersebut lagi //membaca pulsa mengunakan fungsi pulseIn pada software arduino duration = pulseIn(pingPin, HIGH); //mengkonversi waktu tempuh ke jarak //kecepatan suara 344 m/s atau 29 us/cm //jarak perjalanan gelombang ultrasonic adalah dari memancarkan //sampai terpantul benda kemudian diterima kembali oleh sensor //kemudian di bagi 2 untuk mendapatkan jarak benda cm = duration / 29 / 2; //menampilkan data pembacaan sensor Serial.print(duration); Serial.print(" uS | "); Serial.print(cm); Serial.print(" cm"); Serial.println(); delay(200); } void loop() { //pemangilan fungsi sensor ping sensor_ping(7); }

NewPing Library for Arduino (Ultrasonic Sensors)


Background: When I first received an ultrasonic sensor I was not happy with how poorly it performed. I soon realized the problem wasn't the sensor, it was the available ping and ultrasonic libraries causing the problem. The NewPing library totally fixes these

problems, adds many new features, and breathes new life into these very affordable distance sensors. Features:

Works with many different ultrasonic sensor models: SR04, SRF05, SRF06, DYP-ME007 & Parallax PING))). Interface with all but the SRF06 sensor using only one Arduino pin. Doesn't lag for a full second if no ping/echo is received. Ping sensors consistently and reliably at up to 30 times per second. Timer interrupt method for event-driven sketches. Built-in digital filter method ping_median() for easy error correction. Uses port registers when accessing pins for faster execution and smaller code size. Allows setting of a maximum distance where pings beyond that distance are read as no ping "clear". Ease of using multiple sensors (example sketch with 15 sensors). More accurate distance calculation (cm, inches & uS). Doesn't use pulseIn, which is slow and gives incorrect results with some ultrasonic sensor models. Actively developed with features being added and bugs/issues addressed.

Download: Download NewPing v1.5 Example Sketch: Simple NewPing Example Sketch
// -------------------------------------------------------------------------// Example NewPing library sketch that does a ping about 20 times per second. // -------------------------------------------------------------------------#include <NewPing.h> #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } void loop() { delay(50); // Wait 50ms between pings (about

20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo) Serial.println("cm"); }

New in v1.5 - Released 8/15/2012: Added ping_median() method which does a user specified number of pings (default=5) and returns the median ping in microseconds (out of range pings ignored). This is a very effective digital filter. Optimized for smaller compiled size (even smaller than sketches that don't use a library). New in v1.4 - Released 7/14/2012: You can now interface with all but the SRF06 sensor using only one Arduino pin. Added support for the Parallax PING))) sensor. You can also interface with the SRF06 using one pin if you install a 0.1uf capacitor on the trigger and echo pins of the sensor then tie the trigger pin to the Arduino pin (doesn't work with Teensy). To use the same Arduino pin for trigger and echo, specify the same pin for both values. Various bug fixes. New in v1.3 - Released 6/8/2012: Big feature addition, event-driven ping! Uses Timer2 interrupt, so be mindful of PWM or timing conflicts messing with Timer2 may cause (namely PWM on pins 3 & 11 on

Arduino, PWM on pins 9 and 10 on Mega, and Tone library). Simple to use timer interrupt functions you can use in your sketches totaly unrelated to ultrasonic sensors (don't use if you're also using NewPing's ping_timer because both use Timer2 interrupts). Loop counting ping method deleted in favor of timing ping method after inconsistant results kept surfacing with the loop timing ping method. Conversion to cm and inches now rounds to the nearest cm or inch. Code optimized to save program space and fixed a couple minor bugs here and there. Many new comments added as well as line spacing to group code sections for better source readability. NOTE: For Teensy/Leonardo (ATmega32U4) the library uses Timer4 instead of Timer2. Also, only 16Mhz microcontrollers are supported with the timer methods, which means the ATmega8 and ATmega128 will not work with the timer methods. However, the standard ping method should work just fine on 8Mhz microcontrollers. New in v1.2 - Released 5/24/2012: Lots of code clean-up thanks to Adruino Forum members. Rebuilt the ping timing code from scratch, ditched the pulseIn code as it doesn't give correct results (at least with ping sensors). The NewPing library is now VERY accurate and the code was simplified as a bonus. Smaller and faster code as well. Fixed some issues with very close ping results when converting to inches. All functions now return 0 only when there's no ping echo (out of range) and a positive value for a successful ping. This can effectively be used to detect if something is out of range or in-range and at what distance. Now compatible with Arduino 0023. New in v1.1 - Released 5/16/2012: Changed all I/O functions to use low-level port registers for ultra-fast and lean code (saves from 174 to 394 bytes). Tested on both the Arduino Uno and Teensy 2.0 but should work on all Arduino-based platforms because it calls standard functions to retrieve port registers and bit masks. Also made a couple minor fixes to defines. v1.0 - Released 5/15/2012: Initial release.

Ultrasonic Range Sensor HC-SR04 dengan Library NewPing


November 29, 2012 Zerfani Yulias No comments

Wow, posting terakhir yang saya buat sudah hampir setahun yang lalu. Mohon maaf, karena kesibukan mengurus Toko Online Famosa Studio, saya tidak sempat membuat posting yang baru. Tapi mulai sekarang akan diusahakan untuk terus menambah posting di Blog ini lebih sering lagi. Ok, kali ini kita akan menjelaskan tentang Library Arduino yang baru untuk Sensor Jarak Ultrasonic HC-SR04, yaitu library NewPing. NewPing Library NewPing ini dibuat Tim Eckel dan dapat di-download di sini: NewPing. Tim merasa tidak puas dengan performa sensor ultrasonic yang dipakainya dan setelah dipelajari ternyata itu disebabkan karena library yang digunakan dan bukan karena sensornya. Jadilah Tim membuat library ini. Sampai saat ini library NewPing sudah sampai pada versi 1.5. Selain untuk HC-SR04, library ini dapat juga digunakan untuk sensor ultrasonic SRF05, SRF06, DYP-ME007 & Parallax PING))). HC-SR04 Menggunakan library NewPing ini dengan sensor HC-SR04 sangat mudah sekali. Pertama, download library-nya dari link di atas. Copy folder yang ada di dalam file zip ke dalam folder libraries yang ada di dalam folder aplikasi IDE Arduino anda. Kemudian hubungkan sensor ultrasonic HC-SR04 dengan Arduino. Cara koneksi antara Arduino dengan HC-SRo4 dapat juga dilihat pada link NewPing diatas, atau dapat dilihat di posting yang ini: Menggunakan Ultrasonic Range Sensor HC-SR04 dan SDM-IO.

HC-SR04 dan Arduino Uno R3

Buka aplikasi IDE Arduino anda, kemudian buka contoh kode program NewPing dari menu: File>Examples>NewPing>NewPingExamples. Nanti akan ada 2 tab NewPingExample yang muncul pada aplikasi IDE Arduino anda. Pilih salah satu tab, saya pilih tab yang kedua, lalu copy kode program ke dalam Sketch kosong. Contoh kode program pada tab kedua tersebut dapat dilihat di bawah ini. 1 // --------------------------------------------------------------------------2 // Example NewPing library sketch that does a ping about 20 times per second. 3 // --------------------------------------------------------------------------4 5 #include <NewPing.h> 6 7 #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic 8 sensor. 9 #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. 10#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in 11centimeters). Maximum sensor distance is rated at 400-500cm. 12 13NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing 14setup of pins and maximum distance. 15 16void setup() {

17 Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. 18} 19 20void loop() { 21 delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the 22shortest delay between pings. 23 int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters. Serial.print("Ping: "); Serial.print(cm); // Print the result (0 = outside the set distance range, no ping echo) Serial.println("cm"); }

Sesuaikan pin TRIGGER dan ECHO yang digunakan. Contoh kode program di atas menggunakan pin 12 untuk TRIGGER dan pin 11 untuk ECHO. Pada kode program di atas ada parameter MAX_DISTANCE, yaitu jarak terjauh (dalam cm) yang dapat diukur oleh sensor ultrasonic yang digunakan tersebut. Contoh di atas memakai nilai 200 (200 cm atau 2 meter) untuk jarak terjauhnya. Sesuaikan parameter ini dengan jarak terjauh yang dapat diukur oleh sensor ultrasonic anda. Lalu lakukan verfikasi jika perlu (menu Sketch>Verify/Compile atau tombol keyboard Ctrl+R). Kemudian upload kode program ke Arduino (File>Upload atau Ctrl+U). Kalau tidak ada pesan error maka sensor ultrasonic HC-SR04 akan langsung bekerja dan mulai mengukur jarak benda yang ada di depannya. Gunakan Serial Monitor (Tools>Serial Monitor atau Ctrl+Shift+M) pada aplikasi IDE Arduino untuk melihat hasil pengukuran. Sesuaikan baudrate serial monitor dengan yang ada di kode program (115200). Bagaimana, mudah sekali bukan? Anda sekarang dapat mulai berkreasi untuk menggunakan library NewPing ini dengan sensor ultrasonic HC-SR04 (atau sensor ultrasonic yang lain) pada proyek Arduino anda.

You might also like