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
0 parents
commit 49e23b9
Showing
7 changed files
with
134 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.pyc | ||
*.sw* | ||
tests | ||
tests/* |
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,27 @@ | ||
HC-SR04 Ultrasonic Sensor on Raspberry Pi | ||
========================================= | ||
|
||
A simple module for calculating distance in centimeters, using an HC-SR04 sensor and a | ||
Raspberry Pi. | ||
|
||
|
||
Install | ||
======= | ||
|
||
pip install hcsr04sensor | ||
|
||
Description | ||
=========== | ||
The module does the following; | ||
|
||
* Returns an error corrected distance by using the median reading of a sorted | ||
sample. | ||
|
||
* Rounds the value to a specified decimal place. | ||
|
||
* Adjusts the reading based on temperature by adjusting the speed of sound. | ||
|
||
Usage | ||
===== | ||
|
||
See example scripts in https://github.com/alaudet/hcsr04sensor.git/examples |
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,20 @@ | ||
#!/usr/bin/python | ||
|
||
|
||
import hcsr04sensor.pihcsr04 as sensor | ||
|
||
|
||
def depth(hole_depth): | ||
'''measure the distance in cm's and return a rounded float''' | ||
trig_pin = 17 | ||
echo_pin = 27 | ||
rounded_to = 1 | ||
temperature = 20 | ||
value = sensor.Measurement(trig_pin, echo_pin, rounded_to, temperature) | ||
distance = value.distance() | ||
return hole_depth - distance | ||
|
||
if __name__ == "__main__": | ||
hole_depth = 100 | ||
level = depth(float(hole_depth)) | ||
print "The water is {} centimeters deep".format(round(level, 1)) |
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,17 @@ | ||
#!/usr/bin/python | ||
|
||
import hcsr04sensor.pihcsr04 as sensor | ||
|
||
|
||
def distance(): | ||
'''measure the distance in cm's and return a rounded float''' | ||
trig_pin = 17 | ||
echo_pin = 27 | ||
rounded_to = 1 | ||
temperature = 20 | ||
value = sensor.Measurement(trig_pin, echo_pin, rounded_to, temperature) | ||
return value.distance() | ||
|
||
if __name__ == "__main__": | ||
distance = distance() | ||
print "The measured distance is {} centimeters".format(distance) |
Empty file.
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,39 @@ | ||
import time | ||
import math | ||
import RPi.GPIO as GPIO | ||
|
||
|
||
class Measurement(object): | ||
'''Create a distance measurement using an HC-SR04 Ultrasonic Sensor''' | ||
def __init__(self, trig_pin, echo_pin, rounded_to, temperature): | ||
self.trig_pin = trig_pin | ||
self.echo_pin = echo_pin | ||
self.rounded_to = rounded_to | ||
self.temperature = temperature | ||
|
||
def distance(self): | ||
"""Return the distance in cm of an object, accounting for | ||
temperature in Celcius.""" | ||
GPIO.setwarnings(False) | ||
GPIO.setmode(GPIO.BCM) | ||
speed_of_sound = 331.3 * math.sqrt((1+(self.temperature / 273.15)))*100 | ||
sample = [] | ||
for distance_reading in range(11): | ||
GPIO.setup(self.trig_pin, GPIO.OUT) | ||
GPIO.setup(self.echo_pin, GPIO.IN) | ||
GPIO.output(self.trig_pin, GPIO.LOW) | ||
time.sleep(0.3) | ||
GPIO.output(self.trig_pin, True) | ||
time.sleep(0.00001) | ||
GPIO.output(self.trig_pin, False) | ||
while GPIO.input(self.echo_pin) == 0: | ||
sonar_signal_off = time.time() | ||
while GPIO.input(self.echo_pin) == 1: | ||
sonar_signal_on = time.time() | ||
time_passed = sonar_signal_on - sonar_signal_off | ||
distance_cm = time_passed * (speed_of_sound / 2) | ||
sample.append(distance_cm) | ||
GPIO.cleanup() | ||
sorted_sample = sorted(sample) | ||
sensor_distance = sorted_sample[5] | ||
return round(sensor_distance, self.rounded_to) |
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,27 @@ | ||
from setuptools import setup | ||
import os | ||
version = '0.0.1' | ||
|
||
|
||
setup(name='hcsr04sensor', | ||
version=version, | ||
description='Simple module to access HCSR04 sensor on RaspiPi', | ||
long_description=open("./README.md", "r").read(), | ||
classifiers=[ | ||
"Development Status :: 4 - Beta", | ||
"Environment :: Console", | ||
"Intended Audience :: End Users/Desktop", | ||
"Natural Language :: English", | ||
"Operating System :: POSIX :: Linux", | ||
"Programming Language :: Python :: 2.7", | ||
"Topic :: Home Automation", | ||
"License :: OSI Approved :: MIT License", | ||
], | ||
author='Al Audet', | ||
author_email='alaudet@linuxnorth.org', | ||
url='http://www.linuxnorth.org/', | ||
download_url='https://github.com/alaudet/', | ||
license='MIT License', | ||
packages=['hcsr04sensor'], | ||
install_requires=['RPi.GPIO'] | ||
) |