-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <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); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <HCSR04.h> | ||
|
||
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); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
UltraSonicDistanceSensor KEYWORD1 | ||
measureDistanceCm KEYWORD2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name=HCSR04 | ||
version=1.0.0 | ||
author=Martin Sosic <[email protected]> | ||
maintainer=Martin Sosic <[email protected]> | ||
sentence=Library for HC-SR04 ultrasonic distance sensor. | ||
paragraph=You can measure distance in centimeters. | ||
category=Sensors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |