Skip to content

Commit

Permalink
First version done and working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinsos committed Jun 11, 2016
1 parent 1dff653 commit 92e6e26
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 2 deletions.
45 changes: 43 additions & 2 deletions README.md
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);
}
```
13 changes: 13 additions & 0 deletions examples/simple/simple.ino
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);
}
Binary file added hcsr04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UltraSonicDistanceSensor KEYWORD1
measureDistanceCm KEYWORD2
7 changes: 7 additions & 0 deletions library.properties
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
33 changes: 33 additions & 0 deletions src/HCSR04.cpp
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;
}
}
29 changes: 29 additions & 0 deletions src/HCSR04.h
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

0 comments on commit 92e6e26

Please sign in to comment.