[go: up one dir, main page]

100% found this document useful (1 vote)
128 views16 pages

Ultrasonic Sensor HC-SR04 and Arduino Tutorial

This document provides a tutorial on using an ultrasonic sensor HC-SR04 with an Arduino board to measure distance. It explains how to connect the sensor to the Arduino, write code to activate the sensor and calculate the distance based on the echo pulse, and display the distance readings on the Arduino serial monitor or LCD screen. The code examples show how to pulse the trigger pin, read the echo pin, and convert the echo time to a distance measurement in both centimeters and inches.

Uploaded by

Miguel Zea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
128 views16 pages

Ultrasonic Sensor HC-SR04 and Arduino Tutorial

This document provides a tutorial on using an ultrasonic sensor HC-SR04 with an Arduino board to measure distance. It explains how to connect the sensor to the Arduino, write code to activate the sensor and calculate the distance based on the echo pulse, and display the distance readings on the Arduino serial monitor or LCD screen. The code examples show how to pulse the trigger pin, read the echo pin, and convert the echo time to a distance measurement in both centimeters and inches.

Uploaded by

Miguel Zea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.

com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

1 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/

// defines pins numbers


const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance


distance= duration*0.034/2;

// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
}

2 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 9;


const int echoPin = 10;

long duration;
int distanceCm, distanceInch;

void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}

3 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

4 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

5 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

6 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

7 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

8 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

9 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

10 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

11 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

12 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

13 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

S UB MI T

14 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

15 de 16 26/02/2018 07:44 a. m.
Ultrasonic Sensor HC-SR04 and Arduino Tutorial https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

16 de 16 26/02/2018 07:44 a. m.

You might also like