Berikut ini adalah video tutorial simulasi project Arduino Proteus mengukur jarak dengan menggunakan sensor ultrasonic HC-SR04.
Untuk source code (sketch) project Arduino ini sebagai berikut.
/*
How to Measure Distances
Arduino Uno Ultrasonic Sensor HC-SR04
Loki Lang
*/
#define echoPin 2
#define trigPin 3
long duration, distance, ultraSensor;
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
sensorUltrasonic(trigPin, echoPin);
ultraSensor = distance;
Serial.println(ultraSensor);
}
void sensorUltrasonic(int pinTrigger, int pinEcho)
{
digitalWrite(pinTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrigger, LOW);
duration = pulseIn(pinEcho, HIGH);
distance = (duration / 2) / 29.1;
delay(100);
}
How to Measure Distances
Arduino Uno Ultrasonic Sensor HC-SR04
Loki Lang
*/
#define echoPin 2
#define trigPin 3
long duration, distance, ultraSensor;
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
sensorUltrasonic(trigPin, echoPin);
ultraSensor = distance;
Serial.println(ultraSensor);
}
void sensorUltrasonic(int pinTrigger, int pinEcho)
{
digitalWrite(pinTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrigger, LOW);
duration = pulseIn(pinEcho, HIGH);
distance = (duration / 2) / 29.1;
delay(100);
}