Erriez HC-SR04 ultrasonic distance sensor library for Arduino  1.0.0
HC-SR04 ultrasonic distance sensor library for Arduino by Erriez.
HC-SR04 Ultrasonic Distance Sensor library for Arduino

This is a HC-SR04 I2C RTC library for Arduino.

HC-SR04 sensor

Library features

Hardware

Arduino UNO

Arduino Uno HC-SR04

Examples

Arduino IDE | Examples | Erriez HC-SR04:

Documentation

Example

{c++}
#include <ErriezHCSR04.h>
// Pin defines
#define TRIG_PIN 2
#define ECHO_PIN 3
// Create sensor object
ErriezHCSR04 hcsr04(TRIG_PIN, ECHO_PIN);
void setup()
{
// Initialize serial port
delay(500);
Serial.begin(115200);
while (!Serial) {
;
}
Serial.println(F("\nErriez HC-SR04 ultrasonic distance sensor example\n"));
// Initialize pins
hcsr04.begin();
}
void loop()
{
int16_t distance;
// Measure distance in centimeter
distance = hcsr04.getDistance();
// Print distance
Serial.print(F("Distance: "));
if (distance >= 0) {
Serial.print(distance);
Serial.println(F(" cm"));
} else {
Serial.println(F("-"));
}
// Wait
delay(250);
}

Measure distance in Inches

{c++}
void loop()
{
float distance;
// Measure distance in Inch
distance = hcsr04.getDistanceInch();
// Print distance
Serial.print(F("Distance: "));
if (distance >= 0) {
Serial.print(distance);
Serial.println(F("\""));
} else {
Serial.println(("-"));
}
// Wait
delay(250);
}

Library dependencies

Library installation

Please refer to the Wiki page.

More Arduino Libraries from Erriez