Skip to content

Arduino library for HC-SR04 ultrasonic distance sensor.

License

Notifications You must be signed in to change notification settings

Fairgrounds/arduino-lib-hc-sr04

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arduino library for HC-SR04 ultrasonic distance sensor.

HC-SR04 is an ultrasonic sensor that measures distances from 2 to 400cm.

HC-SR04

This is a simple library for it!

Usage

Sensor is initialized by creating instance of class UltraSonicDistanceSensor and providing trigger and echo pins.

UltraSonicDistanceSensor sensor(triggerPin, echoPin);

Then, to measure the distance, you just call measureDistanceCm(), which will return distance in centimeters (double). If distance is larger than 400cm, it will return negative value.

The calculation assumes a temperature of around 20°C. For improved accuracy you may also provide a temperature yourself, either an average for your location or directly measured from another sensor. The call for a temperature of 3.5°C would as such look like this: measureDistanceCm(3.5).

Example

In this simple example, we need to connect sensors pins like this:

  • vcc to 5V
  • trig to digital pin 13
  • echo to digital pin 12
  • gnd to gnd
#include <HCSR04.h>

// Initialize sensor that uses digital pins 13 and 12.
int triggerPin = 13;
int echoPin = 12;
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);

void setup () {
    Serial.begin(9600);  // We initialize serial connection so that we could print values from sensor.
}

void loop () {
    // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
    double distance = distanceSensor.measureDistanceCm();
    Serial.println(distance);
    delay(500);
}

About

Arduino library for HC-SR04 ultrasonic distance sensor.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%