SENSOR JARAK ULTRA SONIK HC-SR04
SENSOR JARAK ULTRA SONIK HC-SR04
1. ALAT DAN BAHAN :
a. Sensor Ultrasonik HC-SR04
b. Kabel Jumper
c. Arduno Mega 2560
d. Kabel USB Arduino
e. Bread Board
f. LCD 16x2 i2c
2. LIBRARY :
Link Library Ultra Sonik Di Sini
3. RANGKAIAN :
-LCD :
a. GND -> GND
b. VCC -> 5V
c. SDA -> SDA
d. SCL -> SCL
-Ultra Sonik :
a. GND -> GND
b. VCC -> 5V
c. Echo -> 8
d. Trig -> 9
4. CODING :
#include <NewPing.h>
#include <LiquidCrystal_I2C.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int triggerPin = 9, echoPin = 8;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Maksimum pembacaan sensor (400cm) //
const int measureLimit = 200;
int duration = 0, distance = 0;
void setup()
{
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin();
lcd.setCursor(6, 0);
lcd.println("Blog:");
lcd.setCursor(0, 1);
lcd.print("Mahirelektro.com");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Jarak = ");
}
void loop()
{
lcd.setCursor(8, 0);
readSensor();
lcd.print(distance);
lcd.println(" cm ");
Serial.print("jarak =");
Serial.print(distance);
Serial.print(" cm ");
}
void readSensor()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
distance = constrain(distance, 0, measureLimit);
}
5. HASIL FOTO :
6. HASIL VIDEO :
Tidak ada komentar