diff --git a/README.md b/README.md index 300a380..1c736d3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,43 @@ -# arduino-lib-hc-sr04 -Arduino library for HC-SR04 ultrasonic distance sensor. +# Arduino library for HC-SR04 ultrasonic distance sensor. + +HC-SR04 is an ultrasonic sensor that measures distances from 2 to 400cm. + +![HC-SR04](/hcsr04.jpg) + +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. + + +## 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 + +// 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); +} +``` diff --git a/examples/simple/simple.ino b/examples/simple/simple.ino new file mode 100644 index 0000000..ba0111a --- /dev/null +++ b/examples/simple/simple.ino @@ -0,0 +1,13 @@ +#include + +UltraSonicDistanceSensor distanceSensor(13, 12); // Initialize sensor that uses digital pins 13 and 12. + +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. + Serial.println(distanceSensor.measureDistanceCm()); + delay(500); +} diff --git a/hcsr04.jpg b/hcsr04.jpg new file mode 100644 index 0000000..ab66524 Binary files /dev/null and b/hcsr04.jpg differ diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..d02aa01 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,2 @@ +UltraSonicDistanceSensor KEYWORD1 +measureDistanceCm KEYWORD2 diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..1754e65 --- /dev/null +++ b/library.properties @@ -0,0 +1,7 @@ +name=HCSR04 +version=1.0.0 +author=Martin Sosic +maintainer=Martin Sosic +sentence=Library for HC-SR04 ultrasonic distance sensor. +paragraph=You can measure distance in centimeters. +category=Sensors diff --git a/src/HCSR04.cpp b/src/HCSR04.cpp new file mode 100644 index 0000000..d62e14c --- /dev/null +++ b/src/HCSR04.cpp @@ -0,0 +1,33 @@ +/* + HCSR04 - Library for arduino, for HC-SR04 ultrasonic distance sensor. + Created by Martin Sosic, June 11, 2016. +*/ + +#include "Arduino.h" +#include "HCSR04.h" + +UltraSonicDistanceSensor::UltraSonicDistanceSensor( + int triggerPin, int echoPin) { + this->triggerPin = triggerPin; + this->echoPin = echoPin; + pinMode(triggerPin, OUTPUT); + pinMode(echoPin, INPUT); +} + +double UltraSonicDistanceSensor::measureDistanceCm() { + // Make sure that trigger pin is LOW. + digitalWrite(triggerPin, LOW); + delayMicroseconds(2); + // Hold trigger for 10 microseconds, which is signal for sensor to measure distance. + digitalWrite(triggerPin, HIGH); + delayMicroseconds(10); + digitalWrite(triggerPin, LOW); + // Measure the length of echo signal, which is equal to the time needed for sound to go there and back. + unsigned long durationMicroSec = pulseIn(echoPin, HIGH); + double distanceCm = durationMicroSec / 2.0 * 0.0343; + if (distanceCm == 0 || distanceCm > 400) { + return -1.0 ; + } else { + return distanceCm; + } +} diff --git a/src/HCSR04.h b/src/HCSR04.h new file mode 100644 index 0000000..83edd7e --- /dev/null +++ b/src/HCSR04.h @@ -0,0 +1,29 @@ +/* + HCSR04 - Library for arduino, for HC-SR04 ultrasonic distance sensor. + Created by Martin Sosic, June 11, 2016. +*/ + +#ifndef HCSR04_H +#define HCSR04_H + +#include "Arduino.h" + +class UltraSonicDistanceSensor { + public: + /** + * @param triggerPin Digital pin that is used for controlling sensor (output). + * @param echoPin Digital pin that is used to get information from sensor (input). + */ + UltraSonicDistanceSensor(int triggerPin, int echoPin); + + /** + * Measures distance by sending ultrasonic waves and measuring time it takes them + * to return. + * @returns Distance in centimeters, or negative value if distance is greater than 400cm. + */ + double measureDistanceCm(); + private: + int triggerPin, echoPin; +}; + +#endif // HCSR04_H