Ultrasonic sensor is one kind of sensors used to measure distance
to an object. As the name implies, the sensor works at ultra high frequency of
sound which is around 40 KHz frequency range. As comparison, the human audible
frequency is 20 Hz-20 KHz. Ultrasonic sensor consists of two transducers, one
of them serves as an ultrasonic transmitter, while another acts as an
ultrasonic frequency receiver. The receiver measures object distance with
calculating reflection time (echo return) ultrasonic waves from the object.
Because ultrasonic wave is fired from the transmitter, the distance in millimeter
(or centimeter) is obtained by a formula that stated round trip time of the ultrasonic waves from transmitter to the receiver, divided by two.
Arduino HC-SR04
Sounds complicated? Not really, because all will be
facilitated by the sensor that has been shaped as a compact module and a
ready-made Arduino library. The library will help you do all things easier. I
will show, you will able to build a simple
distance meter with Arduino and
ultrasonic sensor in 5 minutes only J
. First of course prepare the ‘ingredients’, you'll need HC SR-04 ultrasonic
sensor module, an Arduino board (Uno or others) and some jumper cables.
Second step, wiring distance measurement circuit according
to the image below
Arduino HC SR-04 Sketch Handler
Third step, copy the sketch below to your arduino IDE
(download at arduino.cc) and upload to your Arduino board.
#include <NewPing.h> #define TRIGGER_PIN 8 // jumper TRIG pin on sensor arduino pin 8 #define ECHO_PIN 7 // jumper ECHO pin on sensor arduino pin 7 #define MAX_DISTANCE 200 // max distance in mm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // create new class void setup() { Serial.begin(9600); // open serial monitor with baudrate 9600 bps } void loop() { delay(50); // delay for every measurement unsigned int uS = sonar.ping(); // send a 'ping' and save the result in variable 'uS' (micro second) Serial.print("Ping: "); //send 'Ping' text ke serial monitor Serial.print(uS / US_ROUNDTRIP_CM); // convert ping time distance (cm) Serial.println("cm"); //send 'cm' text to serial monitor }
You will need library 'NewPing.h' as used in sketch above to be compiled properly in your laptop. You must download it first (here) and copy it in your arduino libraries folder (usually in c:/program files/arduino/libraries)
Done. Easy enough,
right? Even you who do not have basic microcontroller, you can make a simple distance meter with Arduino and ultrasonic sensor less
than five minutes. But it would be even better if you keep studying 'how to do
it’ so you can enhance to more complex systems or just in case you must
integrated it into another wide system.
ConversionConversion EmoticonEmoticon