-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhall.py
More file actions
66 lines (56 loc) · 1.5 KB
/
Copy pathhall.py
File metadata and controls
66 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
#--------------------------------------
# ___ ___ _ ____
# / _ \/ _ \(_) __/__ __ __
# / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/ /_/___/ .__/\_, /
# /_/ /___/
#
# Hall Effect Sensor
#
# This script tests the sensor on GPIO17.
#
# Author : Matt Hawkins
# Date : 08/05/2018
#
# https://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
# Import required libraries
import time
import datetime
import RPi.GPIO as GPIO
def sensorCallback(channel):
# Called if sensor output changes
timestamp = time.time()
stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
if GPIO.input(channel):
# No magnet
print("Sensor HIGH " + stamp)
else:
# Magnet
print("Sensor LOW " + stamp)
def main():
# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
# Get initial reading
sensorCallback(17)
try:
# Loop until users quits with CTRL-C
while True :
time.sleep(0.1)
except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
print("Setup GPIO pin as input on GPIO17")
# Set Switch GPIO as input
# Pull high by default
GPIO.setup(17 , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorCallback, bouncetime=200)
if __name__=="__main__":
main()