Segilmez06 / DistanceSensor

HC-SR04 Distance Sensor Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DistanceSensor

GitHub Workflow Status (with branch) GitHub Workflow Status (with branch) GitHub release (latest by date) GitHub repo size GitHub all releases arduino-library-badge

A simple library for the HC-SR04 ultrasonic distance sensor. It measures the time between the sensor's trigger and echo pins. The library is designed to be used with the Arduino IDE. Compitable with most Arduino and ESP boards.

Installation

With Arduino Library Manager

  1. Open the Arduino IDE
  2. Go to Sketch > Include Library > Manage Libraries...
  3. Search for DistanceSensor
  4. Click on the library and click Install

With Zip file

  1. Download the library from the Releases page.
  2. Open the Arduino IDE
  3. Go to Sketch > Include Library > Add .ZIP Library...
  4. Select the downloaded file

Manual Installation

  1. Download the library from the Releases page.
  2. Unzip the file
  3. Go to Documents/Arduino/libraries
  4. Copy contents of the zip file to Documents/Arduino/libraries

Usage

#include <DistanceSensor.h>

// Define pins
const int echoPin = 12;
const int trigPin = 13;

// Start the sensor
DistanceSensor sensor(trigPin, echoPin);

void setup () {
    // Start serial port
    Serial.begin(115200);
}

void loop () {
    // Get distance in cm
    int distance = sensor.getCM();

    // Write values to serial port
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println("cm");

    // Wait a bit
    delay(500);
}

Credits

Original library from Afstandssensor authors.

I just translated it to English and added some examples.

About

HC-SR04 Distance Sensor Library

License:GNU General Public License v2.0


Languages

Language:C++ 100.0%