Skip to content

Commit 671fc1e

Browse files
committed
additional sensor module with ultrasonic sensor HC-SR04
1 parent 769d96d commit 671fc1e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

pi_mqtt_gpio/modules/hcsr04.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from pi_mqtt_gpio.modules import GenericSensor
2+
import time
3+
import datetime
4+
5+
REQUIREMENTS = ("RPi.GPIO",)
6+
7+
# HC-SR04 ultrasonic sensor_inputs
8+
# for use at Raspberry Pi, the ECHO signal of the sensor must be reduced to 3.3V, since the sensor works with 5V!!
9+
# You can simply reduce with a 3.9kOhm and 6.8kOhm resistor voltage reduction
10+
#
11+
# further information at http://www.netzmafia.de/skripten/hardware/RasPi/Projekt-Ultraschall/
12+
# The code is based on the sample code of this webpage by Prof.Plate / University of Munich
13+
14+
CONFIG_SCHEMA = {
15+
"pin_echo": {"type": "integer", "required": True, "empty": False},
16+
"pin_trigger": {"type": "integer", "required": True, "empty": False},
17+
"burst": {"type": "integer", "required": True, "empty": False},
18+
}
19+
# duration trigger pulse
20+
PULSE = 0.00001
21+
# sonic speed/2
22+
SPEED_2 = 17015
23+
24+
class Sensor(GenericSensor):
25+
26+
def __init__(self, config):
27+
import RPi.GPIO as GPIO
28+
29+
self.gpio = GPIO
30+
self.pin_Trigger = config["pin_trigger"]
31+
self.pin_Echo = config["pin_echo"]
32+
self.burst = config["burst"]
33+
34+
def setup_sensor(self, config):
35+
# use BCM GPIO-references (instead of Pin-numbers)
36+
# and define GPIO-input/output
37+
self.gpio.setmode(self.gpio.BCM)
38+
self.gpio.setup(self.pin_Trigger,self.gpio.OUT)
39+
self.gpio.setup(self.pin_Echo,self.gpio.IN)
40+
self.gpio.remove_event_detect(self.pin_Echo)
41+
self.gpio.output(self.pin_Trigger, False)
42+
time.sleep(1) # Setup-time for Sensor
43+
self.gpio.add_event_detect(self.pin_Echo, self.gpio.BOTH, callback=self.measure) # create callback triggered by rising and falling edge
44+
45+
self.stopp = 0
46+
self.start = 0
47+
self.distance = 0
48+
49+
return True
50+
51+
def get_value(self, config):
52+
value = self.measure_range()
53+
return value
54+
55+
def pulse(self): # function to start measurement
56+
self.gpio.output(self.pin_Trigger, True) # create trigger pulse
57+
time.sleep(PULSE)
58+
self.gpio.output(self.pin_Trigger, False)
59+
self.stopp = 0
60+
self.start = 0
61+
self.distance = 0
62+
63+
def measure(self, x): # Callback-Function for echo signal
64+
if self.gpio.input(self.pin_Echo) == 1: # save time of rising echo signal
65+
self.start = time.time()
66+
else: # falling edge, save time
67+
self.stopp = time.time()
68+
delta = self.stopp - self.start # calculate time difference sending / receiving
69+
self.distance = delta * SPEED_2 # calculate distance by time and sonic speed
70+
71+
def measure_range(self): # start measures and calculate average of BURST measurements
72+
values = []
73+
sum = 0
74+
for i in range(0, self.burst):
75+
self.pulse() # start measurement
76+
time.sleep(0.040) # wait till end of measurement
77+
values.append(self.distance) # save value in array and sum up
78+
sum = sum + values[i]
79+
time.sleep(0.05)
80+
return sum/self.burst; # return average
81+
82+
def cleanup(self):
83+
self.gpio.cleanup()

0 commit comments

Comments
 (0)