Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alaudet committed Oct 18, 2014
0 parents commit 49e23b9
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.pyc
*.sw*
tests
tests/*
27 changes: 27 additions & 0 deletions README.md
@@ -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
20 changes: 20 additions & 0 deletions examples/depth.py
@@ -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))
17 changes: 17 additions & 0 deletions examples/distance.py
@@ -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 added hcsr04sensor/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions hcsr04sensor/pihcsr04.py
@@ -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)
27 changes: 27 additions & 0 deletions setup.py
@@ -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']
)

0 comments on commit 49e23b9

Please sign in to comment.