Arduino UNO measures the distance data with the ultrasonic sensor. Simply point the sensor to the distance so it will measured. This sensor has high accuracy in measurement in certain distance. It's recomended for you to use this sensor for another aplication. In this experiment, I use Ultrasonic Sensor with type HC-SR04 and 2x16 LCD as display of sensor readings.
Hardware Requirement
- Ultrasonic Sensor HC-SR04 Module
- Arduino UNO
- LCD 2x16 Module
- Power supply +5 Volt
Ultrasonic Sensor HC-SR04 | Source
Block Diagram
Schematic
Arduino - LCD Wiring
Arduino - HC-SR04 Wiring
Source Code/Sketch
#define ECHOPIN 9
#define TRIGPIN 8
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
unsigned int jarak, timer;
void setup(){
lcd.begin(16, 2);
lcd.print(" ULTRASONIC");
lcd.setCursor(0, 1);
lcd.print(" RANGE METER");
delay(2000);
lcd.clear();
lcd.print("Range=");
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop(){
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
timer = pulseIn(ECHOPIN, HIGH);
jarak= timer/58;
lcd.setCursor(6, 0);
lcd.print(jarak);
lcd.print(" cm ");
delay(1000);
}
#define TRIGPIN 8
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
unsigned int jarak, timer;
void setup(){
lcd.begin(16, 2);
lcd.print(" ULTRASONIC");
lcd.setCursor(0, 1);
lcd.print(" RANGE METER");
delay(2000);
lcd.clear();
lcd.print("Range=");
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop(){
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
timer = pulseIn(ECHOPIN, HIGH);
jarak= timer/58;
lcd.setCursor(6, 0);
lcd.print(jarak);
lcd.print(" cm ");
delay(1000);
}
How it Works
1. Connect the Arduino with Peripherals needed
2. Plug in the Power Source on the device
3. Compile and upload the script program above to your arduino
2. Plug in the Power Source on the device
3. Compile and upload the script program above to your arduino
4. On the lcd you'll see the opening text like this
5. After 2 second, the measurement data will show up
6. Next you can give a barrier or board in front of the ultrasonic sensor (as reflective media) to result
different measurement
Video for Project II - 9. Distance Measurement using Ultrasonic Sensor (Arduino Based)
Download the required file.
No comments:
Post a Comment