HC-SR04 ULTRASONIC SENSOR

Ultrasonic

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/ULTRASONIC%20SENSOR.zip

Sket 1

#define trigPin1 2

#define echoPin1 4

long duration, distance, UltraSensor;

void setup()

{

Serial.begin (9600);

pinMode(trigPin1, OUTPUT);

pinMode(echoPin1, INPUT);

}

void loop() {

SonarSensor(trigPin1, echoPin1);

UltraSensor = distance;

Serial.println(UltraSensor);

}

void SonarSensor(int trigPin,int echoPin)

{

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

delay(100);

}

Sket 2

const int trigPin = 2;

const int echoPin = 4;

void setup() {

// initialize serial communication:

Serial.begin(9600);

}

void loop()

{

// establish variables for duration of the ping,

// and the distance result in inches and centimeters:

long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);

Serial.print(inches);

Serial.print(“in, “);

Serial.print(cm);

Serial.print(“cm”);

Serial.println();

delay(100);

}

long microsecondsToInches(long microseconds)

{

// According to Parallax’s datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds)

{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

Sket 3

#include <NewPing.h>

/*—–( Declare Constants and Pin Numbers )—–*/

#define TRIGGER_PIN 2

#define ECHO_PIN 4

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).

//Maximum sensor distance is rated at 400-500cm.

/*—–( Declare objects )—–*/

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

/*—–( Declare Variables )—–*/

int DistanceIn;

int DistanceCm;

void setup()

{

Serial.begin(9600);

Serial.println(“UltraSonic Distance Measurement”);

}

void loop()

{

delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.

DistanceIn = sonar.ping_in();

Serial.print(“Ping: “);

Serial.print(DistanceIn); // Convert ping time to distance and print result

// (0 = outside set distance range, no ping echo)

Serial.print(” in “);

delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.

DistanceCm = sonar.ping_cm();

Serial.print(“Ping: “);

Serial.print(DistanceCm);

Serial.println(” cm”);

Sket 4

Sensor ultrasonic range juga dapat di pasang lebih dari satu, seperti sket di bawah.

int trigPin1 = 6;

int echoPin1 = 7;

int trigPin2 = 8;

int echoPin2 = 9;

void setup() {

Serial.begin (9600);

pinMode(trigPin1, OUTPUT);

pinMode(echoPin1, INPUT);

pinMode(trigPin2, OUTPUT);

pinMode(echoPin2, INPUT);

}

void firstsensor(){ // This function is for first sensor.

int duration1, distance1;

digitalWrite (trigPin1, HIGH);

delayMicroseconds (10);

digitalWrite (trigPin1, LOW);

duration1 = pulseIn (echoPin1, HIGH);

distance1 = (duration1/2) / 29.1;

Serial.print(“1st Sensor: “);

Serial.print(distance1);

Serial.print(“cm “);

}

void secondsensor(){ // This function is for second sensor.

int duration2, distance2;

digitalWrite (trigPin2, HIGH);

delayMicroseconds (10);

digitalWrite (trigPin2, LOW);

duration2 = pulseIn (echoPin2, HIGH);

distance2 = (duration2/2) / 29.1;

Serial.print(“2nd Sensor: “);

Serial.print(distance2);

Serial.print(“cm “);

}

void loop() {

Serial.println(“\n”);

firstsensor();

secondsensor();

delay(100);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s