/
01_hall_3.py
executable file
·61 lines (45 loc) · 1.21 KB
/
01_hall_3.py
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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import ADC0832
import time
'''
Example how to use the hall-sensor with digital and analog output
While the loop is printing the analog values, an interrupt-method
is waiting for changes on the digital pin. In case of any change
the message "change detected" is displayed.
Example output:
res0 = 138, res1 = 0
res0 = 133, res1 = 0
Change detected
res0 = 116, res1 = 0
Change detected
res0 = 132, res1 = 0
res0 = 138, res1 = 0
'''
# Digital out of sensor connected to
# pin 16 (BOAR-Layout!)
HALL = 16 #
#The ADC is connected like described in the manual
GPIO.setmode(GPIO.BOARD) # using BOARD layout
GPIO.setwarnings(False)
# Setup pins
GPIO.setup(HALL,GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Callback if digital-out triggers
def change_detected(channel):
print 'Change detected'
def init():
ADC0832.setup()
# register event listener
GPIO.add_event_detect(HALL, GPIO.BOTH, change_detected, bouncetime=25)
def loop():
while True:
res0 = ADC0832.getResult(0)
res1 = ADC0832.getResult(1)
print 'res0 = %d, res1 = %d' % (res0,res1)
time.sleep(0.2)
if __name__ == '__main__':
init()
try:
loop()
except KeyboardInterrupt:
ADC0832.destroy()